Skip to content

Commit 178989d

Browse files
committed
Add Bind function
1 parent 0ebd775 commit 178989d

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

modules/middlewares/cookie.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func NewCookie(name, value string, maxAge int) *http.Cookie {
2626
}
2727

2828
// SetCookie set the cookies
29+
// TODO: Copied from gitea.com/macaron/macaron and should be improved after macaron removed.
2930
func SetCookie(resp http.ResponseWriter, name string, value string, others ...interface{}) {
3031
cookie := http.Cookie{}
3132
cookie.Name = name

modules/options/static.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func AssetNames() []string {
133133
realFS := Assets.(vfsgen۰FS)
134134
var results = make([]string, 0, len(realFS))
135135
for k := range realFS {
136-
results = append(results, "templates/"+k[1:])
136+
results = append(results, k[1:])
137137
}
138138
return results
139139
}

routers/routes/chi.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,16 @@ func Wrap(f func(ctx *gitea_context.DefaultContext)) http.HandlerFunc {
188188
}
189189

190190
// Bind binding an obj to a handler
191-
func Bind(obj interface{}, handler http.HandlerFunc) http.HandlerFunc {
192-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
193-
errs := binding.Bind(req, obj)
191+
func Bind(obj interface{}, handler func(ctx *gitea_context.DefaultContext)) http.HandlerFunc {
192+
return Wrap(func(ctx *gitea_context.DefaultContext) {
193+
errs := binding.Bind(ctx.Req, obj)
194194
if errs.Len() > 0 {
195-
ctx := gitea_context.GetDefaultContext(req)
196195
ctx.Data["HasError"] = true
197-
auth.AssignForm(obj, ctx.Data)
198-
// FIXME:
199-
//ctx.Flash(ErrorFlash, msg)
196+
ctx.Flash(gitea_context.ErrorFlash, errs[0].Error())
200197
}
201-
202-
handler(resp, req)
198+
auth.AssignForm(obj, ctx.Data)
199+
ctx.Data["form"] = obj
200+
handler(ctx)
203201
})
204202
}
205203

routers/routes/install.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func InstallRoutes() http.Handler {
4444
r := chi.NewRouter()
4545
r.Use(InstallInit)
4646
r.Get("/", Wrap(Install))
47-
r.Post("/", Bind(&forms.InstallForm{}, Wrap(InstallPost)))
47+
r.Post("/", Bind(&forms.InstallForm{}, InstallPost))
4848
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
4949
http.Redirect(w, req, setting.AppURL, 302)
5050
})
@@ -88,9 +88,8 @@ func InstallInit(next http.Handler) http.Handler {
8888
Session: session.GetSession(req),
8989
}
9090

91-
req = context.WithDefaultContext(req, &ctx)
92-
ctx.Req = req
93-
next.ServeHTTP(resp, req)
91+
ctx.Req = context.WithDefaultContext(req, &ctx)
92+
next.ServeHTTP(resp, ctx.Req)
9493
})
9594
}
9695

@@ -170,12 +169,9 @@ func Install(ctx *context.DefaultContext) {
170169

171170
// InstallPost response for submit install items
172171
func InstallPost(ctx *context.DefaultContext) {
173-
var form forms.InstallForm
174-
_ = ctx.Bind(&form)
172+
form := ctx.Data["form"].(*forms.InstallForm)
175173

176-
var err error
177174
ctx.Data["CurDbOption"] = form.DbType
178-
179175
if ctx.HasError() {
180176
if ctx.HasValue("Err_SMTPUser") {
181177
ctx.Data["Err_SMTP"] = true
@@ -190,14 +186,14 @@ func InstallPost(ctx *context.DefaultContext) {
190186
return
191187
}
192188

189+
var err error
193190
if _, err = exec.LookPath("git"); err != nil {
194191
ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), tplInstall, &form)
195192
return
196193
}
197194

198195
// Pass basic check, now test configuration.
199196
// Test database setting.
200-
201197
setting.Database.Type = setting.GetDBTypeByName(form.DbType)
202198
setting.Database.Host = form.DbHost
203199
setting.Database.User = form.DbUser

0 commit comments

Comments
 (0)