Skip to content

Commit 140337a

Browse files
6543lunny
authored andcommitted
Revert "move CorsHandler to common"
This reverts commit 1bbc7aa6fe89df015e65d6794d2f1d6c5e8ca7ce.
1 parent f4c98a4 commit 140337a

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

routers/common/cors.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

routers/web/web.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"gitea.com/go-chi/session"
4343
"github.com/NYTimes/gziphandler"
4444
"github.com/go-chi/chi/middleware"
45+
"github.com/go-chi/cors"
4546
"github.com/prometheus/client_golang/prometheus"
4647
"github.com/tstranex/u2f"
4748
)
@@ -51,14 +52,32 @@ const (
5152
GzipMinSize = 1400
5253
)
5354

55+
// CorsHandler return a http handler who set CORS options if enabled by config
56+
func CorsHandler() func(next http.Handler) http.Handler {
57+
if setting.CORSConfig.Enabled {
58+
return cors.Handler(cors.Options{
59+
//Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
60+
AllowedOrigins: setting.CORSConfig.AllowDomain,
61+
//setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
62+
AllowedMethods: setting.CORSConfig.Methods,
63+
AllowCredentials: setting.CORSConfig.AllowCredentials,
64+
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),
65+
})
66+
}
67+
68+
return func(next http.Handler) http.Handler {
69+
return next
70+
}
71+
}
72+
5473
// Routes returns all web routes
5574
func Routes() *web.Route {
5675
routes := web.NewRoute()
5776

5877
routes.Use(public.AssetsHandler(&public.Options{
5978
Directory: path.Join(setting.StaticRootPath, "public"),
6079
Prefix: "/assets",
61-
CorsHandler: common.CorsHandler(),
80+
CorsHandler: CorsHandler(),
6281
}))
6382

6483
routes.Use(session.Sessioner(session.Options{
@@ -271,7 +290,7 @@ func RegisterRoutes(m *web.Route) {
271290
m.Post("/authorize", bindIgnErr(forms.AuthorizationForm{}), user.AuthorizeOAuth)
272291
}, ignSignInAndCsrf, reqSignIn)
273292
m.Get("/login/oauth/userinfo", ignSignInAndCsrf, user.InfoOAuth)
274-
m.Post("/login/oauth/access_token", common.CorsHandler(), bindIgnErr(forms.AccessTokenForm{}), ignSignInAndCsrf, user.AccessTokenOAuth)
293+
m.Post("/login/oauth/access_token", CorsHandler(), bindIgnErr(forms.AccessTokenForm{}), ignSignInAndCsrf, user.AccessTokenOAuth)
275294

276295
m.Group("/user/settings", func() {
277296
m.Get("", userSetting.Profile)

0 commit comments

Comments
 (0)