Skip to content

Commit

Permalink
Revert "move CorsHandler to common"
Browse files Browse the repository at this point in the history
This reverts commit 1bbc7aa6fe89df015e65d6794d2f1d6c5e8ca7ce.
  • Loading branch information
6543 authored and lunny committed Jun 8, 2021
1 parent f4c98a4 commit 140337a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
31 changes: 0 additions & 31 deletions routers/common/cors.go

This file was deleted.

23 changes: 21 additions & 2 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"gitea.com/go-chi/session"
"github.com/NYTimes/gziphandler"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/prometheus/client_golang/prometheus"
"github.com/tstranex/u2f"
)
Expand All @@ -51,14 +52,32 @@ const (
GzipMinSize = 1400
)

// CorsHandler return a http handler who set CORS options if enabled by config
func CorsHandler() func(next http.Handler) http.Handler {
if setting.CORSConfig.Enabled {
return cors.Handler(cors.Options{
//Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
AllowedOrigins: setting.CORSConfig.AllowDomain,
//setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
AllowedMethods: setting.CORSConfig.Methods,
AllowCredentials: setting.CORSConfig.AllowCredentials,
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),
})
}

return func(next http.Handler) http.Handler {
return next
}
}

// Routes returns all web routes
func Routes() *web.Route {
routes := web.NewRoute()

routes.Use(public.AssetsHandler(&public.Options{
Directory: path.Join(setting.StaticRootPath, "public"),
Prefix: "/assets",
CorsHandler: common.CorsHandler(),
CorsHandler: CorsHandler(),
}))

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

m.Group("/user/settings", func() {
m.Get("", userSetting.Profile)
Expand Down

0 comments on commit 140337a

Please sign in to comment.