From 8f729820f535d7dfe352fa21a567f67f495baa9c Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani Date: Wed, 5 May 2021 22:31:56 +0000 Subject: [PATCH] controller: add default headers Signed-off-by: Ramkumar Chinchani --- pkg/api/controller.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/api/controller.go b/pkg/api/controller.go index c06c1fcb..2bf6066b 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -34,6 +34,19 @@ func NewController(config *Config) *Controller { return &Controller{Config: config, Log: log.NewLogger(config.Log.Level, config.Log.Output)} } +func DefaultHeaders() mux.MiddlewareFunc { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // CORS + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS") + + // handle the request + next.ServeHTTP(w, r) + }) + } +} + func (c *Controller) Run() error { // validate configuration if err := c.Config.Validate(c.Log); err != nil { @@ -45,8 +58,10 @@ func (c *Controller) Run() error { c.Log.Info().Interface("params", c.Config.Sanitize()).Msg("configuration settings") engine := mux.NewRouter() - engine.Use(log.SessionLogger(c.Log), handlers.RecoveryHandler(handlers.RecoveryLogger(c.Log), - handlers.PrintRecoveryStack(false))) + engine.Use(DefaultHeaders(), + log.SessionLogger(c.Log), + handlers.RecoveryHandler(handlers.RecoveryLogger(c.Log), + handlers.PrintRecoveryStack(false))) c.ImageStore = storage.NewImageStore(c.Config.Storage.RootDirectory, c.Config.Storage.GC, c.Config.Storage.Dedupe, c.Log)