From af552596cfd7f6fd05dfc38abaaffad1d7fed654 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 10 Apr 2014 14:37:43 -0400 Subject: [PATCH] Work on form resubmit --- gogs.go | 2 +- modules/middleware/context.go | 36 ++++++++++++++++- routers/install.go | 76 ++++++++++++++++++++--------------- templates/base/alert.tmpl | 1 + templates/install.tmpl | 2 +- web.go | 5 ++- 6 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 templates/base/alert.tmpl diff --git a/gogs.go b/gogs.go index 297100715..228fe89f4 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.3.0409 Alpha" +const APP_VER = "0.2.3.0410 Alpha" func init() { base.AppVer = APP_VER diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 8129b13b7..272af3305 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -11,6 +11,7 @@ import ( "fmt" "html/template" "net/http" + "net/url" "strconv" "strings" "time" @@ -34,6 +35,7 @@ type Context struct { p martini.Params Req *http.Request Res http.ResponseWriter + Flash *Flash Session session.SessionStore Cache cache.Cache User *models.User @@ -78,6 +80,7 @@ func (ctx *Context) HasError() bool { if !ok { return false } + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) return hasErr.(bool) } @@ -88,8 +91,7 @@ func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) { // RenderWithErr used for page has form validation but need to prompt error to users. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { - ctx.Data["HasError"] = true - ctx.Data["ErrorMsg"] = msg + ctx.Flash.Error(msg) if form != nil { auth.AssignForm(form, ctx.Data) } @@ -239,6 +241,21 @@ func (ctx *Context) CsrfTokenValid() bool { return true } +type Flash struct { + url.Values + ErrorMsg, SuccessMsg string +} + +func (f *Flash) Error(msg string) { + f.Set("error", msg) + f.ErrorMsg = msg +} + +func (f *Flash) Success(msg string) { + f.Set("success", msg) + f.SuccessMsg = msg +} + // InitContext initializes a classic context for a request. func InitContext() martini.Handler { return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) { @@ -256,9 +273,24 @@ func InitContext() martini.Handler { // start session ctx.Session = base.SessionManager.SessionStart(res, r) + + ctx.Flash = &Flash{} + // Get flash. + values, err := url.ParseQuery(ctx.GetCookie("gogs_flash")) + if err != nil { + log.Error("InitContext.ParseQuery(flash): %v", err) + } else { + ctx.Flash.Values = values + ctx.Data["Flash"] = ctx.Flash + } + rw := res.(martini.ResponseWriter) rw.Before(func(martini.ResponseWriter) { ctx.Session.SessionRelease(res) + + if flash := ctx.Flash.Encode(); len(flash) > 0 { + ctx.SetCookie("gogs_flash", ctx.Flash.Encode(), -1) + } }) // Get user from session if logined. diff --git a/routers/install.go b/routers/install.go index 5d6c65ef9..d3686053b 100644 --- a/routers/install.go +++ b/routers/install.go @@ -23,6 +23,10 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +type installRouter int + +var InstallRouter installRouter = 1 + // Check run mode(Default of martini is Dev). func checkRunMode() { switch base.Cfg.MustValue("", "RUN_MODE") { @@ -54,7 +58,7 @@ func GlobalInit() { checkRunMode() } -func Install(ctx *middleware.Context, form auth.InstallForm) { +func (r installRouter) Get(ctx *middleware.Context, form auth.InstallForm) { if base.InstallLock { ctx.Handle(404, "install.Install", errors.New("Installation is prohibited")) return @@ -63,42 +67,49 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { ctx.Data["Title"] = "Install" ctx.Data["PageIsInstall"] = true - if ctx.Req.Method == "GET" { - // Get and assign value to install form. - if len(form.Host) == 0 { - form.Host = models.DbCfg.Host - } - if len(form.User) == 0 { - form.User = models.DbCfg.User - } - if len(form.Passwd) == 0 { - form.Passwd = models.DbCfg.Pwd - } - if len(form.DatabaseName) == 0 { - form.DatabaseName = models.DbCfg.Name - } - if len(form.DatabasePath) == 0 { - form.DatabasePath = models.DbCfg.Path - } + // Get and assign value to install form. + if len(form.Host) == 0 { + form.Host = models.DbCfg.Host + } + if len(form.User) == 0 { + form.User = models.DbCfg.User + } + if len(form.Passwd) == 0 { + form.Passwd = models.DbCfg.Pwd + } + if len(form.DatabaseName) == 0 { + form.DatabaseName = models.DbCfg.Name + } + if len(form.DatabasePath) == 0 { + form.DatabasePath = models.DbCfg.Path + } - if len(form.RepoRootPath) == 0 { - form.RepoRootPath = base.RepoRootPath - } - if len(form.RunUser) == 0 { - form.RunUser = base.RunUser - } - if len(form.Domain) == 0 { - form.Domain = base.Domain - } - if len(form.AppUrl) == 0 { - form.AppUrl = base.AppUrl - } + if len(form.RepoRootPath) == 0 { + form.RepoRootPath = base.RepoRootPath + } + if len(form.RunUser) == 0 { + form.RunUser = base.RunUser + } + if len(form.Domain) == 0 { + form.Domain = base.Domain + } + if len(form.AppUrl) == 0 { + form.AppUrl = base.AppUrl + } - auth.AssignForm(form, ctx.Data) - ctx.HTML(200, "install") + auth.AssignForm(form, ctx.Data) + ctx.HTML(200, "install") +} + +func (r installRouter) Post(ctx *middleware.Context, form auth.InstallForm) { + if base.InstallLock { + ctx.Handle(404, "install.Install", errors.New("Installation is prohibited")) return } + ctx.Data["Title"] = "Install" + ctx.Data["PageIsInstall"] = true + if ctx.HasError() { ctx.HTML(200, "install") return @@ -197,5 +208,6 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { } log.Info("First-time run install finished!") + ctx.Flash.Success("Welcome! We're glad that you choose Gogs, have fun and take care.") ctx.Redirect("/user/login") } diff --git a/templates/base/alert.tmpl b/templates/base/alert.tmpl new file mode 100644 index 000000000..699314ace --- /dev/null +++ b/templates/base/alert.tmpl @@ -0,0 +1 @@ +{{if .Flash.ErrorMsg}}
{{.Flash.ErrorMsg}}
{{end}} \ No newline at end of file diff --git a/templates/install.tmpl b/templates/install.tmpl index c70cfa3e6..3aa64ccd4 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -3,7 +3,7 @@
{{.CsrfTokenHtml}}

Install Steps For First-time Run

-
{{.ErrorMsg}}
+ {{template "base/alert" .}}

Gogs requires MySQL or PostgreSQL, SQLite3 only available for official binary version

diff --git a/web.go b/web.go index 1a9c292f3..0f61bc214 100644 --- a/web.go +++ b/web.go @@ -74,9 +74,12 @@ func runWeb(*cli.Context) { ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView}) reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true}) + bindIgnErr := binding.BindIgnErr + // Routers. m.Get("/", ignSignIn, routers.Home) - m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install) + m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.InstallRouter.Get) + m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallRouter.Post) m.Get("/issues", reqSignIn, user.Issues) m.Get("/pulls", reqSignIn, user.Pulls) m.Get("/stars", reqSignIn, user.Stars)