Skip to content

Commit

Permalink
获取视频文件的请求允许CORS (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
kira1928 authored May 5, 2024
1 parent ba3ca83 commit 1a55764
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 1a55764

Please sign in to comment.