@@ -40,7 +40,7 @@ type Server struct {
4040 options * serverOptions
4141 httpSvr atomic.Pointer [http.Server ]
4242 log * slog.Logger
43- manager * fs.FunctionManager
43+ Manager * fs.FunctionManager
4444}
4545
4646type serverOptions struct {
@@ -59,7 +59,7 @@ func (f serverOptionFunc) apply(c *serverOptions) (*serverOptions, error) {
5959 return f (c )
6060}
6161
62- // WithFunctionManager sets the function manager for the server.
62+ // WithFunctionManager sets the function Manager for the server.
6363func WithFunctionManager (opts ... fs.ManagerOption ) ServerOption {
6464 return serverOptionFunc (func (o * serverOptions ) (* serverOptions , error ) {
6565 o .managerOpts = append (o .managerOpts , opts ... )
@@ -114,7 +114,7 @@ func NewServer(opts ...ServerOption) (*Server, error) {
114114 }
115115 return & Server {
116116 options : options ,
117- manager : manager ,
117+ Manager : manager ,
118118 log : slog .With (),
119119 }, nil
120120}
@@ -167,8 +167,29 @@ func (s *Server) Run(context context.Context) {
167167 }
168168}
169169
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+
170185func (s * Server ) startRESTHandlers () error {
171186 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+
172193 r .HandleFunc ("/api/v1/status" , func (w http.ResponseWriter , r * http.Request ) {
173194 w .WriteHeader (http .StatusOK )
174195 }).Methods ("GET" )
@@ -208,7 +229,7 @@ func (s *Server) startRESTHandlers() error {
208229 return
209230 }
210231
211- err = s .manager .StartFunction (f )
232+ err = s .Manager .StartFunction (f )
212233 if err != nil {
213234 log .Error ("Failed to start function" , "error" , err )
214235 http .Error (w , err .Error (), http .StatusBadRequest )
@@ -222,7 +243,7 @@ func (s *Server) startRESTHandlers() error {
222243 functionName := vars ["function_name" ]
223244 log := s .log .With (slog .String ("name" , functionName ), slog .String ("phase" , "deleting" ))
224245
225- err := s .manager .DeleteFunction (functionName )
246+ err := s .Manager .DeleteFunction (functionName )
226247 if errors .Is (err , common .ErrorFunctionNotFound ) {
227248 log .Error ("Function not found" , "error" , err )
228249 http .Error (w , err .Error (), http .StatusNotFound )
@@ -234,7 +255,7 @@ func (s *Server) startRESTHandlers() error {
234255 r .HandleFunc ("/api/v1/functions" , func (w http.ResponseWriter , r * http.Request ) {
235256 log := s .log .With ()
236257 log .Info ("Listing functions" )
237- functions := s .manager .ListFunctions ()
258+ functions := s .Manager .ListFunctions ()
238259 w .Header ().Set ("Content-Type" , "application/json" )
239260 err := json .NewEncoder (w ).Encode (functions )
240261 if err != nil {
@@ -255,7 +276,7 @@ func (s *Server) startRESTHandlers() error {
255276 http .Error (w , errors .Wrap (err , "Failed to read body" ).Error (), http .StatusBadRequest )
256277 return
257278 }
258- err = s .manager .ProduceEvent (queueName , contube .NewRecordImpl (content , func () {}))
279+ err = s .Manager .ProduceEvent (queueName , contube .NewRecordImpl (content , func () {}))
259280 if err != nil {
260281 log .Error ("Failed to produce event" , "error" , err )
261282 http .Error (w , err .Error (), http .StatusInternalServerError )
@@ -269,7 +290,7 @@ func (s *Server) startRESTHandlers() error {
269290 queueName := vars ["queue_name" ]
270291 log := s .log .With (slog .String ("queue_name" , queueName ))
271292 log .Info ("Consuming event from queue" )
272- event , err := s .manager .ConsumeEvent (queueName )
293+ event , err := s .Manager .ConsumeEvent (queueName )
273294 if err != nil {
274295 log .Error ("Failed to consume event" , "error" , err )
275296 http .Error (w , err .Error (), http .StatusInternalServerError )
@@ -304,7 +325,7 @@ func (s *Server) startRESTHandlers() error {
304325 }
305326 log := s .log .With (slog .String ("key" , key ))
306327 log .Info ("Getting state" )
307- state := s .manager .GetStateStore ()
328+ state := s .Manager .GetStateStore ()
308329 if state == nil {
309330 log .Error ("No state store configured" )
310331 http .Error (w , "No state store configured" , http .StatusBadRequest )
@@ -333,7 +354,7 @@ func (s *Server) startRESTHandlers() error {
333354 }
334355 log := s .log .With (slog .String ("key" , key ))
335356 log .Info ("Getting state" )
336- state := s .manager .GetStateStore ()
357+ state := s .Manager .GetStateStore ()
337358 if state == nil {
338359 log .Error ("No state store configured" )
339360 http .Error (w , "No state store configured" , http .StatusBadRequest )
@@ -411,8 +432,8 @@ func (s *Server) Close() error {
411432 return err
412433 }
413434 }
414- if s .manager != nil {
415- err := s .manager .Close ()
435+ if s .Manager != nil {
436+ err := s .Manager .Close ()
416437 if err != nil {
417438 return err
418439 }
0 commit comments