github.com/blend/go-sdk@v1.20220411.3/examples/web/api/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"log"
    12  	"sync"
    13  
    14  	"github.com/blend/go-sdk/graceful"
    15  	"github.com/blend/go-sdk/logger"
    16  	"github.com/blend/go-sdk/web"
    17  )
    18  
    19  // Any is a type alias for interface{}
    20  type Any = interface{}
    21  
    22  // APIController implements a simple controller.
    23  type APIController struct {
    24  	sync.Mutex
    25  	db map[string]Any
    26  }
    27  
    28  // Register adds routes for the controller to the app.
    29  func (ac *APIController) Register(app *web.App) {
    30  	app.GET("/", ac.index)
    31  	app.GET("/api", ac.all)
    32  	app.GET("/api/:key", ac.get)
    33  	app.POST("/api/:key", ac.post)
    34  	app.PUT("/api/:key", ac.put)
    35  	app.DELETE("/api/:key", ac.delete)
    36  }
    37  
    38  func (ac *APIController) ensureDB() {
    39  	if ac.db == nil {
    40  		ac.db = map[string]Any{}
    41  	}
    42  }
    43  
    44  func (ac *APIController) index(r *web.Ctx) web.Result {
    45  	return web.JSON.OK()
    46  }
    47  
    48  func (ac *APIController) all(r *web.Ctx) web.Result {
    49  	ac.Lock()
    50  	defer ac.Unlock()
    51  	ac.ensureDB()
    52  
    53  	return web.JSON.Result(ac.db)
    54  }
    55  
    56  func (ac *APIController) get(r *web.Ctx) web.Result {
    57  	ac.Lock()
    58  	defer ac.Unlock()
    59  	ac.ensureDB()
    60  
    61  	key, err := r.Param("key")
    62  	if err != nil {
    63  		return web.JSON.BadRequest(err)
    64  	}
    65  
    66  	value, hasValue := ac.db[key]
    67  	if !hasValue {
    68  		return web.JSON.NotFound()
    69  	}
    70  	return web.JSON.Result(value)
    71  }
    72  
    73  func (ac *APIController) post(r *web.Ctx) web.Result {
    74  	ac.Lock()
    75  	defer ac.Unlock()
    76  	ac.ensureDB()
    77  
    78  	body, err := r.PostBody()
    79  	if err != nil {
    80  		return web.JSON.InternalError(err)
    81  	}
    82  
    83  	key, err := r.Param("key")
    84  	if err != nil {
    85  		return web.JSON.BadRequest(err)
    86  	}
    87  	ac.db[key] = string(body)
    88  	return web.JSON.OK()
    89  }
    90  
    91  func (ac *APIController) put(r *web.Ctx) web.Result {
    92  	ac.Lock()
    93  	defer ac.Unlock()
    94  	ac.ensureDB()
    95  
    96  	key, err := r.Param("key")
    97  	if err != nil {
    98  		return web.JSON.BadRequest(err)
    99  	}
   100  
   101  	_, hasValue := ac.db[key]
   102  	if !hasValue {
   103  		return web.JSON.NotFound()
   104  	}
   105  
   106  	body, err := r.PostBody()
   107  	if err != nil {
   108  		return web.JSON.InternalError(err)
   109  	}
   110  	ac.db[key] = string(body)
   111  
   112  	return web.JSON.OK()
   113  }
   114  
   115  func (ac *APIController) delete(r *web.Ctx) web.Result {
   116  	ac.Lock()
   117  	defer ac.Unlock()
   118  	ac.ensureDB()
   119  
   120  	key, err := r.Param("key")
   121  	if err != nil {
   122  		return web.JSON.BadRequest(err)
   123  	}
   124  
   125  	_, hasValue := ac.db[key]
   126  	if !hasValue {
   127  		return web.JSON.NotFound()
   128  	}
   129  	delete(ac.db, key)
   130  	return web.JSON.OK()
   131  }
   132  
   133  func main() {
   134  	app := web.MustNew(web.OptLog(logger.All()))
   135  	app.Register(new(APIController))
   136  	if err := graceful.Shutdown(app); err != nil {
   137  		log.Fatal(err)
   138  	}
   139  }