diff --git a/http.go b/http.go index e2fddb8..83f4beb 100644 --- a/http.go +++ b/http.go @@ -14,6 +14,16 @@ type Router struct { Mux *mux.Router } +// Middleware function to manipulate our request and response. +func imgdHandler(router http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET") + w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding") + router.ServeHTTP(w, r) + }) +} + type NotFoundHandler struct{} // Handles 404 errors diff --git a/main.go b/main.go index 736142b..59822e8 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func setupLog(logBackend *logging.LogBackend) { func startServer() { r := Router{Mux: mux.NewRouter()} r.Bind() - http.Handle("/", r.Mux) + http.Handle("/", imgdHandler(r.Mux)) log.Notice("imgd %s starting on %s", ImgdVersion, config.Server.Address) err := http.ListenAndServe(config.Server.Address, nil) if err != nil {