-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_computer_management.go
228 lines (188 loc) · 8.85 KB
/
configure_computer_management.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file is safe to edit. Once it exists it will not be overwritten
package restapi
import (
"crypto/tls"
goerrors "errors"
"net/http"
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/swag"
"github.com/marius-go/computer-management-service/gen/controller/rest/models"
"github.com/marius-go/computer-management-service/gen/controller/rest/restapi/operations"
"github.com/marius-go/computer-management-service/gen/controller/rest/restapi/operations/computer"
"github.com/marius-go/computer-management-service/internal/controller/rest"
"github.com/marius-go/computer-management-service/internal/core/domain"
computersrv "github.com/marius-go/computer-management-service/internal/core/service/computer"
"github.com/marius-go/computer-management-service/internal/infrastructure/adminnotifier"
"github.com/marius-go/computer-management-service/internal/infrastructure/memstorage"
)
//go:generate swagger generate server --target ../../gen --name ComputerManagement --spec ../../api/v1/computer-management.yaml --principal interface{}
var configurationFlags = struct {
NotificationServiceAddress string `long:"notification-service" description:"optional: address of the admin notifications service" default:"http://localhost:8080"`
}{}
func configureFlags(api *operations.ComputerManagementAPI) {
api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{
{
ShortDescription: "Computer Service Flags",
LongDescription: "",
Options: &configurationFlags,
},
}
}
func configureAPI(api *operations.ComputerManagementAPI) http.Handler {
// configure the api here
api.ServeError = errors.ServeError
// Set your custom logger if needed. Default one is log.Printf
// Expected interface func(string, ...interface{})
//
// Example:
// api.Logger = log.Printf
api.UseSwaggerUI()
// To continue using redoc as your UI, uncomment the following line
// api.UseRedoc()
api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()
storage := memstorage.New()
adminNotifier := adminnotifier.New(&http.Client{}, configurationFlags.NotificationServiceAddress)
computerService := computersrv.New(storage, adminNotifier)
api.ComputerCreateComputerHandler = computer.CreateComputerHandlerFunc(
func(params computer.CreateComputerParams) middleware.Responder {
comp := rest.NewComputerRestModelToDomain(*params.Computer)
insertedComputer, err := computerService.CreateComputer(comp)
if err != nil {
errorMessage := err.Error()
errPayload := models.Error{Message: &errorMessage}
var errConflict domain.ErrConflict
var errValidation domain.ErrValidation
switch {
case goerrors.As(err, &errConflict):
return computer.NewCreateComputerConflict().WithPayload(&errPayload)
case goerrors.As(err, &errValidation):
return computer.NewCreateComputerBadRequest().WithPayload(&errPayload)
default:
return computer.NewCreateComputerDefault(500).WithPayload(&errPayload)
}
}
responsePayload := rest.ComputerRestModelFromDomain(insertedComputer)
return computer.NewCreateComputerCreated().WithPayload(&responsePayload)
},
)
api.ComputerDeleteComputerHandler = computer.DeleteComputerHandlerFunc(
func(params computer.DeleteComputerParams) middleware.Responder {
err := computerService.DeleteComputer(params.ComputerName)
if err != nil {
errorMessage := err.Error()
errPayload := models.Error{Message: &errorMessage}
switch {
case goerrors.Is(err, domain.ErrNotFound):
return computer.NewDeleteComputerNotFound().WithPayload(&errPayload)
default:
return computer.NewDeleteComputerDefault(500).WithPayload(&errPayload)
}
}
return computer.NewDeleteComputerNoContent()
},
)
api.ComputerGetComputerHandler = computer.GetComputerHandlerFunc(
func(params computer.GetComputerParams) middleware.Responder {
comp, err := computerService.GetComputer(params.ComputerName)
if err != nil {
errorMessage := err.Error()
errPayload := models.Error{Message: &errorMessage}
switch {
case goerrors.Is(err, domain.ErrNotFound):
return computer.NewGetComputerNotFound().WithPayload(&errPayload)
default:
return computer.NewGetComputerDefault(500).WithPayload(&errPayload)
}
}
responsePayload := rest.ComputerRestModelFromDomain(comp)
return computer.NewGetComputerOK().WithPayload(&responsePayload)
})
api.ComputerListComputersHandler = computer.ListComputersHandlerFunc(func(params computer.ListComputersParams) middleware.Responder {
employeeAbbreviation := params.EmployeeAbbreviation
if employeeAbbreviation != nil && (*employeeAbbreviation == "\"\"" || *employeeAbbreviation == "''") { // "" and '' should be interpreted as empty string
*employeeAbbreviation = ""
}
computers, err := computerService.ListComputers(employeeAbbreviation)
if err != nil {
errorMessage := err.Error()
errPayload := models.Error{Message: &errorMessage}
computer.NewCreateComputerDefault(500).WithPayload(&errPayload)
}
responsePayload := make([]*models.Computer, 0, len(computers))
for _, comp := range computers {
restModel := rest.ComputerRestModelFromDomain(comp)
responsePayload = append(responsePayload, &restModel)
}
return computer.NewListComputersOK().WithPayload(responsePayload)
})
api.ComputerUpdateComputerHandler = computer.UpdateComputerHandlerFunc(func(params computer.UpdateComputerParams) middleware.Responder {
comp := rest.ComputerRestModelToDomain(*params.Computer)
updatedComputer, err := computerService.UpdateComputer(comp)
if err != nil {
errorMessage := err.Error()
errPayload := models.Error{Message: &errorMessage}
var errValidation domain.ErrValidation
switch {
case goerrors.As(err, &errValidation):
return computer.NewUpdateComputerBadRequest().WithPayload(&errPayload)
case goerrors.Is(err, domain.ErrNotFound):
return computer.NewUpdateComputerNotFound().WithPayload(&errPayload)
default:
return computer.NewUpdateComputerDefault(500).WithPayload(&errPayload)
}
}
responsePayload := rest.ComputerRestModelFromDomain(updatedComputer)
return computer.NewUpdateComputerOK().WithPayload(&responsePayload)
})
if api.ComputerCreateComputerHandler == nil {
api.ComputerCreateComputerHandler = computer.CreateComputerHandlerFunc(func(params computer.CreateComputerParams) middleware.Responder {
return middleware.NotImplemented("operation computer.CreateComputer has not yet been implemented")
})
}
if api.ComputerDeleteComputerHandler == nil {
api.ComputerDeleteComputerHandler = computer.DeleteComputerHandlerFunc(func(params computer.DeleteComputerParams) middleware.Responder {
return middleware.NotImplemented("operation computer.DeleteComputer has not yet been implemented")
})
}
if api.ComputerGetComputerHandler == nil {
api.ComputerGetComputerHandler = computer.GetComputerHandlerFunc(func(params computer.GetComputerParams) middleware.Responder {
return middleware.NotImplemented("operation computer.GetComputer has not yet been implemented")
})
}
if api.ComputerListComputersHandler == nil {
api.ComputerListComputersHandler = computer.ListComputersHandlerFunc(func(params computer.ListComputersParams) middleware.Responder {
return middleware.NotImplemented("operation computer.ListComputers has not yet been implemented")
})
}
if api.ComputerUpdateComputerHandler == nil {
api.ComputerUpdateComputerHandler = computer.UpdateComputerHandlerFunc(func(params computer.UpdateComputerParams) middleware.Responder {
return middleware.NotImplemented("operation computer.UpdateComputer has not yet been implemented")
})
}
api.PreServerShutdown = func() {}
api.ServerShutdown = func() {}
return setupGlobalMiddleware(api.Serve(setupMiddlewares))
}
// The TLS configuration before HTTPS server starts.
func configureTLS(tlsConfig *tls.Config) {
// Make all necessary changes to the TLS configuration here.
}
// As soon as server is initialized but not run yet, this function will be called.
// If you need to modify a config, store server instance to stop it individually later, this is the place.
// This function can be called multiple times, depending on the number of serving schemes.
// scheme value will be set accordingly: "http", "https" or "unix".
func configureServer(s *http.Server, scheme, addr string) {
}
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
// The middleware executes after routing but before authentication, binding and validation.
func setupMiddlewares(handler http.Handler) http.Handler {
return handler
}
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
// So this is a good place to plug in a panic handling middleware, logging and metrics.
func setupGlobalMiddleware(handler http.Handler) http.Handler {
return handler
}