Skip to content

Commit

Permalink
Move middlewares to web/middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 27, 2021
1 parent 41c0776 commit 8e845df
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 117 deletions.
4 changes: 2 additions & 2 deletions modules/auth/sso/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/session"
"code.gitea.io/gitea/modules/web/middleware"
)

// DataStore represents a data store
type DataStore middlewares.DataStore
type DataStore middleware.DataStore

// SessionStore represents a session store
type SessionStore session.Store
Expand Down
8 changes: 4 additions & 4 deletions modules/auth/sso/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"
)

// ssoMethods contains the list of SSO authentication plugins in the order they are expected to be
Expand Down Expand Up @@ -131,16 +131,16 @@ func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore
// Language setting of the user overwrites the one previously set
// If the user does not have a locale set, we save the current one.
if len(user.Language) == 0 {
lc := middlewares.Locale(resp, req)
lc := middleware.Locale(resp, req)
user.Language = lc.Language()
if err := models.UpdateUserCols(user, "language"); err != nil {
log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
return
}
}

middlewares.SetCookie(resp, "lang", user.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
middleware.SetCookie(resp, "lang", user.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)

// Clear whatever CSRF has right now, force to generate a new one
middlewares.SetCookie(resp, setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
middleware.SetCookie(resp, setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
}
4 changes: 2 additions & 2 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"code.gitea.io/gitea/modules/auth/sso"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/session"
)
Expand Down Expand Up @@ -224,7 +224,7 @@ func APIContexter() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var locale = middlewares.Locale(w, req)
var locale = middleware.Locale(w, req)
var ctx = APIContext{
Context: &Context{
Resp: NewResponse(w),
Expand Down
32 changes: 16 additions & 16 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"code.gitea.io/gitea/modules/base"
mc "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/cache"
"gitea.com/go-chi/session"
Expand All @@ -55,7 +55,7 @@ type Context struct {
translation.Locale
Cache cache.Cache
csrf CSRF
Flash *middlewares.Flash
Flash *middleware.Flash
Session session.Store

Link string // current request URL
Expand Down Expand Up @@ -197,7 +197,7 @@ func (ctx *Context) HTMLString(name string, data interface{}) (string, error) {
// RenderWithErr used for page has form validation but need to prompt error to users.
func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}) {
if form != nil {
middlewares.AssignForm(form, ctx.Data)
middleware.AssignForm(form, ctx.Data)
}
ctx.Flash.ErrorMsg = msg
ctx.Data["Flash"] = ctx.Flash
Expand Down Expand Up @@ -375,12 +375,12 @@ func (ctx *Context) Redirect(location string, status ...int) {

// SetCookie set cookies to web browser
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
middlewares.SetCookie(ctx.Resp, name, value, others...)
middleware.SetCookie(ctx.Resp, name, value, others...)
}

// GetCookie returns given cookie value from request header.
func (ctx *Context) GetCookie(name string) string {
return middlewares.GetCookie(ctx.Req, name)
return middleware.GetCookie(ctx.Req, name)
}

// GetSuperSecureCookie returns given cookie value from request header with secret string.
Expand Down Expand Up @@ -505,7 +505,7 @@ func Contexter() func(next http.Handler) http.Handler {

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
var locale = middlewares.Locale(resp, req)
var locale = middleware.Locale(resp, req)
var startTime = time.Now()
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
var ctx = Context{
Expand Down Expand Up @@ -536,7 +536,7 @@ func Contexter() func(next http.Handler) http.Handler {
flashCookie := ctx.GetCookie("macaron_flash")
vals, _ := url.ParseQuery(flashCookie)
if len(vals) > 0 {
f := &middlewares.Flash{
f := &middleware.Flash{
DataStore: &ctx,
Values: vals,
ErrorMsg: vals.Get("error"),
Expand All @@ -547,7 +547,7 @@ func Contexter() func(next http.Handler) http.Handler {
ctx.Data["Flash"] = f
}

f := &middlewares.Flash{
f := &middleware.Flash{
DataStore: &ctx,
Values: url.Values{},
ErrorMsg: "",
Expand All @@ -557,22 +557,22 @@ func Contexter() func(next http.Handler) http.Handler {
}
ctx.Resp.Before(func(resp ResponseWriter) {
if flash := f.Encode(); len(flash) > 0 {
middlewares.SetCookie(resp, "macaron_flash", flash, 0,
middleware.SetCookie(resp, "macaron_flash", flash, 0,
setting.SessionConfig.CookiePath,
middlewares.Domain(setting.SessionConfig.Domain),
middlewares.HTTPOnly(true),
middlewares.Secure(setting.SessionConfig.Secure),
middleware.Domain(setting.SessionConfig.Domain),
middleware.HTTPOnly(true),
middleware.Secure(setting.SessionConfig.Secure),
//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
)
return
}

ctx.SetCookie("macaron_flash", "", -1,
setting.SessionConfig.CookiePath,
middlewares.Domain(setting.SessionConfig.Domain),
middlewares.HTTPOnly(true),
middlewares.Secure(setting.SessionConfig.Secure),
//middlewares.SameSite(), FIXME: we need a samesite config
middleware.Domain(setting.SessionConfig.Domain),
middleware.HTTPOnly(true),
middleware.Secure(setting.SessionConfig.Secure),
//middleware.SameSite(), FIXME: we need a samesite config
)
})

Expand Down
8 changes: 4 additions & 4 deletions modules/forms/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/binding"
)
Expand All @@ -27,7 +27,7 @@ type AdminCreateUserForm struct {
// Validate validates form fields
func (f *AdminCreateUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

// AdminEditUserForm form for admin to create user
Expand All @@ -54,7 +54,7 @@ type AdminEditUserForm struct {
// Validate validates form fields
func (f *AdminEditUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

// AdminDashboardForm form for admin dashboard operations
Expand All @@ -66,5 +66,5 @@ type AdminDashboardForm struct {
// Validate validates form fields
func (f *AdminDashboardForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
4 changes: 2 additions & 2 deletions modules/forms/auth_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/binding"
)
Expand Down Expand Up @@ -71,5 +71,5 @@ type AuthenticationForm struct {
// Validate validates fields
func (f *AuthenticationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
8 changes: 4 additions & 4 deletions modules/forms/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/binding"
)
Expand All @@ -33,7 +33,7 @@ type CreateOrgForm struct {
// Validate validates the fields
func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

// UpdateOrgSettingForm form for updating organization settings
Expand All @@ -51,7 +51,7 @@ type UpdateOrgSettingForm struct {
// Validate validates the fields
func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

// ___________
Expand All @@ -74,5 +74,5 @@ type CreateTeamForm struct {
// Validate validates the fields
func (f *CreateTeamForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
4 changes: 2 additions & 2 deletions modules/forms/repo_branch_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/middlewares"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/binding"
)
Expand All @@ -21,5 +21,5 @@ type NewBranchForm struct {
// Validate validates the fields
func (f *NewBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middlewares.Validate(errs, ctx.Data, f, ctx.Locale)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
Loading

0 comments on commit 8e845df

Please sign in to comment.