@@ -40,7 +40,7 @@ type Server struct {
40
40
options * serverOptions
41
41
httpSvr atomic.Pointer [http.Server ]
42
42
log * slog.Logger
43
- manager * fs.FunctionManager
43
+ Manager * fs.FunctionManager
44
44
}
45
45
46
46
type serverOptions struct {
@@ -59,7 +59,7 @@ func (f serverOptionFunc) apply(c *serverOptions) (*serverOptions, error) {
59
59
return f (c )
60
60
}
61
61
62
- // WithFunctionManager sets the function manager for the server.
62
+ // WithFunctionManager sets the function Manager for the server.
63
63
func WithFunctionManager (opts ... fs.ManagerOption ) ServerOption {
64
64
return serverOptionFunc (func (o * serverOptions ) (* serverOptions , error ) {
65
65
o .managerOpts = append (o .managerOpts , opts ... )
@@ -114,7 +114,7 @@ func NewServer(opts ...ServerOption) (*Server, error) {
114
114
}
115
115
return & Server {
116
116
options : options ,
117
- manager : manager ,
117
+ Manager : manager ,
118
118
log : slog .With (),
119
119
}, nil
120
120
}
@@ -167,8 +167,29 @@ func (s *Server) Run(context context.Context) {
167
167
}
168
168
}
169
169
170
+ func corsMiddleware (next http.Handler ) http.Handler {
171
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
172
+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
173
+ w .Header ().Set ("Access-Control-Allow-Methods" , "POST, GET, OPTIONS, PUT, DELETE" )
174
+ w .Header ().Set ("Access-Control-Allow-Headers" , "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization" )
175
+
176
+ if r .Method == "OPTIONS" {
177
+ w .Header ().Set ("Access-Control-Max-Age" , "86400" )
178
+ w .WriteHeader (http .StatusOK )
179
+ return
180
+ }
181
+ next .ServeHTTP (w , r )
182
+ })
183
+ }
184
+
170
185
func (s * Server ) startRESTHandlers () error {
171
186
r := mux .NewRouter ()
187
+
188
+ r .PathPrefix ("/" ).Handler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
189
+ })).Methods ("OPTIONS" )
190
+
191
+ r .Use (corsMiddleware )
192
+
172
193
r .HandleFunc ("/api/v1/status" , func (w http.ResponseWriter , r * http.Request ) {
173
194
w .WriteHeader (http .StatusOK )
174
195
}).Methods ("GET" )
@@ -208,7 +229,7 @@ func (s *Server) startRESTHandlers() error {
208
229
return
209
230
}
210
231
211
- err = s .manager .StartFunction (f )
232
+ err = s .Manager .StartFunction (f )
212
233
if err != nil {
213
234
log .Error ("Failed to start function" , "error" , err )
214
235
http .Error (w , err .Error (), http .StatusBadRequest )
@@ -222,7 +243,7 @@ func (s *Server) startRESTHandlers() error {
222
243
functionName := vars ["function_name" ]
223
244
log := s .log .With (slog .String ("name" , functionName ), slog .String ("phase" , "deleting" ))
224
245
225
- err := s .manager .DeleteFunction (functionName )
246
+ err := s .Manager .DeleteFunction (functionName )
226
247
if errors .Is (err , common .ErrorFunctionNotFound ) {
227
248
log .Error ("Function not found" , "error" , err )
228
249
http .Error (w , err .Error (), http .StatusNotFound )
@@ -234,7 +255,7 @@ func (s *Server) startRESTHandlers() error {
234
255
r .HandleFunc ("/api/v1/functions" , func (w http.ResponseWriter , r * http.Request ) {
235
256
log := s .log .With ()
236
257
log .Info ("Listing functions" )
237
- functions := s .manager .ListFunctions ()
258
+ functions := s .Manager .ListFunctions ()
238
259
w .Header ().Set ("Content-Type" , "application/json" )
239
260
err := json .NewEncoder (w ).Encode (functions )
240
261
if err != nil {
@@ -255,7 +276,7 @@ func (s *Server) startRESTHandlers() error {
255
276
http .Error (w , errors .Wrap (err , "Failed to read body" ).Error (), http .StatusBadRequest )
256
277
return
257
278
}
258
- err = s .manager .ProduceEvent (queueName , contube .NewRecordImpl (content , func () {}))
279
+ err = s .Manager .ProduceEvent (queueName , contube .NewRecordImpl (content , func () {}))
259
280
if err != nil {
260
281
log .Error ("Failed to produce event" , "error" , err )
261
282
http .Error (w , err .Error (), http .StatusInternalServerError )
@@ -269,7 +290,7 @@ func (s *Server) startRESTHandlers() error {
269
290
queueName := vars ["queue_name" ]
270
291
log := s .log .With (slog .String ("queue_name" , queueName ))
271
292
log .Info ("Consuming event from queue" )
272
- event , err := s .manager .ConsumeEvent (queueName )
293
+ event , err := s .Manager .ConsumeEvent (queueName )
273
294
if err != nil {
274
295
log .Error ("Failed to consume event" , "error" , err )
275
296
http .Error (w , err .Error (), http .StatusInternalServerError )
@@ -304,7 +325,7 @@ func (s *Server) startRESTHandlers() error {
304
325
}
305
326
log := s .log .With (slog .String ("key" , key ))
306
327
log .Info ("Getting state" )
307
- state := s .manager .GetStateStore ()
328
+ state := s .Manager .GetStateStore ()
308
329
if state == nil {
309
330
log .Error ("No state store configured" )
310
331
http .Error (w , "No state store configured" , http .StatusBadRequest )
@@ -333,7 +354,7 @@ func (s *Server) startRESTHandlers() error {
333
354
}
334
355
log := s .log .With (slog .String ("key" , key ))
335
356
log .Info ("Getting state" )
336
- state := s .manager .GetStateStore ()
357
+ state := s .Manager .GetStateStore ()
337
358
if state == nil {
338
359
log .Error ("No state store configured" )
339
360
http .Error (w , "No state store configured" , http .StatusBadRequest )
@@ -411,8 +432,8 @@ func (s *Server) Close() error {
411
432
return err
412
433
}
413
434
}
414
- if s .manager != nil {
415
- err := s .manager .Close ()
435
+ if s .Manager != nil {
436
+ err := s .Manager .Close ()
416
437
if err != nil {
417
438
return err
418
439
}
0 commit comments