Decouple the different contexts from each other (#24786)
Replace #16455 Close #21803 Mixing different Gitea contexts together causes some problems: 1. Unable to respond proper content when error occurs, eg: Web should respond HTML while API should respond JSON 2. Unclear dependency, eg: it's unclear when Context is used in APIContext, which fields should be initialized, which methods are necessary. To make things clear, this PR introduces a Base context, it only provides basic Req/Resp/Data features. This PR mainly moves code. There are still many legacy problems and TODOs in code, leave unrelated changes to future PRs.
This commit is contained in:
@ -58,15 +58,14 @@ func Contexter() func(next http.Handler) http.Handler {
|
||||
dbTypeNames := getSupportedDbTypeNames()
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
||||
base, baseCleanUp := context.NewBaseContext(resp, req)
|
||||
ctx := context.Context{
|
||||
Resp: context.NewResponse(resp),
|
||||
Base: base,
|
||||
Flash: &middleware.Flash{},
|
||||
Locale: middleware.Locale(resp, req),
|
||||
Render: rnd,
|
||||
Data: middleware.GetContextData(req.Context()),
|
||||
Session: session.GetSession(req),
|
||||
}
|
||||
defer ctx.Close()
|
||||
defer baseCleanUp()
|
||||
|
||||
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
|
||||
ctx.Data.MergeFrom(middleware.ContextData{
|
||||
@ -78,7 +77,6 @@ func Contexter() func(next http.Handler) http.Handler {
|
||||
|
||||
"PasswordHashAlgorithms": hash.RecommendedHashAlgorithms,
|
||||
})
|
||||
ctx.Req = context.WithContext(req, &ctx)
|
||||
next.ServeHTTP(resp, ctx.Req)
|
||||
})
|
||||
}
|
||||
@ -249,15 +247,8 @@ func SubmitInstall(ctx *context.Context) {
|
||||
ctx.Data["CurDbType"] = form.DbType
|
||||
|
||||
if ctx.HasError() {
|
||||
if ctx.HasValue("Err_SMTPUser") {
|
||||
ctx.Data["Err_SMTP"] = true
|
||||
}
|
||||
if ctx.HasValue("Err_AdminName") ||
|
||||
ctx.HasValue("Err_AdminPasswd") ||
|
||||
ctx.HasValue("Err_AdminEmail") {
|
||||
ctx.Data["Err_Admin"] = true
|
||||
}
|
||||
|
||||
ctx.Data["Err_SMTP"] = ctx.Data["Err_SMTPUser"] != nil
|
||||
ctx.Data["Err_Admin"] = ctx.Data["Err_AdminName"] != nil || ctx.Data["Err_AdminPasswd"] != nil || ctx.Data["Err_AdminEmail"] != nil
|
||||
ctx.HTML(http.StatusOK, tplInstall)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user