github.com/djarvur/go-swagger@v0.18.0/examples/tutorials/todo-list/server-complete/restapi/configure_todo_list.go (about) 1 // This file is safe to edit. Once it exists it will not be overwritten 2 3 package restapi 4 5 import ( 6 "crypto/tls" 7 "net/http" 8 "sync" 9 "sync/atomic" 10 11 errors "github.com/go-openapi/errors" 12 runtime "github.com/go-openapi/runtime" 13 middleware "github.com/go-openapi/runtime/middleware" 14 "github.com/go-openapi/swag" 15 16 "github.com/go-swagger/go-swagger/examples/tutorials/todo-list/server-complete/models" 17 "github.com/go-swagger/go-swagger/examples/tutorials/todo-list/server-complete/restapi/operations" 18 "github.com/go-swagger/go-swagger/examples/tutorials/todo-list/server-complete/restapi/operations/todos" 19 ) 20 21 //go:generate swagger generate server --target .. --name TodoList --spec ../swagger.yml 22 23 var items = make(map[int64]*models.Item) 24 var lastID int64 25 26 var itemsLock = &sync.Mutex{} 27 28 func newItemID() int64 { 29 return atomic.AddInt64(&lastID, 1) 30 } 31 32 func addItem(item *models.Item) error { 33 if item == nil { 34 return errors.New(500, "item must be present") 35 } 36 37 itemsLock.Lock() 38 defer itemsLock.Unlock() 39 40 newID := newItemID() 41 item.ID = newID 42 items[newID] = item 43 44 return nil 45 } 46 47 func updateItem(id int64, item *models.Item) error { 48 if item == nil { 49 return errors.New(500, "item must be present") 50 } 51 52 itemsLock.Lock() 53 defer itemsLock.Unlock() 54 55 _, exists := items[id] 56 if !exists { 57 return errors.NotFound("not found: item %d", id) 58 } 59 60 item.ID = id 61 items[id] = item 62 return nil 63 } 64 65 func deleteItem(id int64) error { 66 itemsLock.Lock() 67 defer itemsLock.Unlock() 68 69 _, exists := items[id] 70 if !exists { 71 return errors.NotFound("not found: item %d", id) 72 } 73 74 delete(items, id) 75 return nil 76 } 77 78 func allItems(since int64, limit int32) (result []*models.Item) { 79 result = make([]*models.Item, 0) 80 for id, item := range items { 81 if len(result) >= int(limit) { 82 return 83 } 84 if since == 0 || id > since { 85 result = append(result, item) 86 } 87 } 88 return 89 } 90 91 func configureFlags(api *operations.TodoListAPI) { 92 // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } 93 } 94 95 func configureAPI(api *operations.TodoListAPI) http.Handler { 96 // configure the api here 97 api.ServeError = errors.ServeError 98 99 // Set your custom logger if needed. Default one is log.Printf 100 // Expected interface func(string, ...interface{}) 101 // 102 // Example: 103 // api.Logger = log.Printf 104 105 api.JSONConsumer = runtime.JSONConsumer() 106 107 api.JSONProducer = runtime.JSONProducer() 108 109 api.TodosAddOneHandler = todos.AddOneHandlerFunc(func(params todos.AddOneParams) middleware.Responder { 110 if err := addItem(params.Body); err != nil { 111 return todos.NewAddOneDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())}) 112 } 113 return todos.NewAddOneCreated().WithPayload(params.Body) 114 }) 115 api.TodosDestroyOneHandler = todos.DestroyOneHandlerFunc(func(params todos.DestroyOneParams) middleware.Responder { 116 if err := deleteItem(params.ID); err != nil { 117 return todos.NewDestroyOneDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())}) 118 } 119 return todos.NewDestroyOneNoContent() 120 }) 121 api.TodosFindTodosHandler = todos.FindTodosHandlerFunc(func(params todos.FindTodosParams) middleware.Responder { 122 mergedParams := todos.NewFindTodosParams() 123 mergedParams.Since = swag.Int64(0) 124 if params.Since != nil { 125 mergedParams.Since = params.Since 126 } 127 if params.Limit != nil { 128 mergedParams.Limit = params.Limit 129 } 130 return todos.NewFindTodosOK().WithPayload(allItems(*mergedParams.Since, *mergedParams.Limit)) 131 }) 132 api.TodosUpdateOneHandler = todos.UpdateOneHandlerFunc(func(params todos.UpdateOneParams) middleware.Responder { 133 if err := updateItem(params.ID, params.Body); err != nil { 134 return todos.NewUpdateOneDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())}) 135 } 136 return todos.NewUpdateOneOK().WithPayload(params.Body) 137 }) 138 139 api.ServerShutdown = func() {} 140 141 return setupGlobalMiddleware(api.Serve(setupMiddlewares)) 142 } 143 144 // The TLS configuration before HTTPS server starts. 145 func configureTLS(tlsConfig *tls.Config) { 146 // Make all necessary changes to the TLS configuration here. 147 } 148 149 // As soon as server is initialized but not run yet, this function will be called. 150 // If you need to modify a config, store server instance to stop it individually later, this is the place. 151 // This function can be called multiple times, depending on the number of serving schemes. 152 // scheme value will be set accordingly: "http", "https" or "unix" 153 func configureServer(s *http.Server, scheme, addr string) { 154 } 155 156 // The middleware configuration is for the handler executors. These do not apply to the swagger.json document. 157 // The middleware executes after routing but before authentication, binding and validation 158 func setupMiddlewares(handler http.Handler) http.Handler { 159 return handler 160 } 161 162 // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document. 163 // So this is a good place to plug in a panic handling middleware, logging and metrics 164 func setupGlobalMiddleware(handler http.Handler) http.Handler { 165 return handler 166 }