From 1a5576470eaee090366cc9cf8223794a04e4bb1c Mon Sep 17 00:00:00 2001 From: kira1928 Date: Sun, 5 May 2024 22:50:04 +0900 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=A7=86=E9=A2=91=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E8=AF=B7=E6=B1=82=E5=85=81=E8=AE=B8CORS=20(#?= =?UTF-8?q?711)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/servers/server.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/servers/server.go b/src/servers/server.go index c01e00d5..94b7a306 100644 --- a/src/servers/server.go +++ b/src/servers/server.go @@ -52,7 +52,18 @@ func initMux(ctx context.Context) *mux.Router { apiRoute.HandleFunc("/file/{path:.*}", getFileInfo).Methods("GET") apiRoute.Handle("/metrics", promhttp.Handler()) - m.PathPrefix("/files/").Handler(http.StripPrefix("/files/", http.FileServer(http.Dir(instance.GetInstance(ctx).Config.OutPutPath)))) + m.PathPrefix("/files/").Handler( + CORSMiddleware( + http.StripPrefix( + "/files/", + http.FileServer( + http.Dir( + instance.GetInstance(ctx).Config.OutPutPath, + ), + ), + ), + ), + ) fs, err := webapp.FS() if err != nil { @@ -67,6 +78,15 @@ func initMux(ctx context.Context) *mux.Router { return m } +func CORSMiddleware(h 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", "POST, GET, OPTIONS, PUT, DELETE") + w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") + h.ServeHTTP(w, r) + }) +} + func NewServer(ctx context.Context) *Server { inst := instance.GetInstance(ctx) config := inst.Config