aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-10-19 22:03:08 +0100
committerGitHub <noreply@github.com>2020-10-19 17:03:08 -0400
commit2f1353a2f33762e10a304cbebf3a6a8d0381d316 (patch)
treeb345bf060a107341c64447f7132d9e2b8d37a7b3 /routers
parent3ddf3f93d6346ac9440a7a571faea4b5c1c329be (diff)
downloadgitea-2f1353a2f33762e10a304cbebf3a6a8d0381d316.tar.gz
gitea-2f1353a2f33762e10a304cbebf3a6a8d0381d316.zip
Move install pages out of main macaron routes (#13195)
* Move install pages out of main macaron loop Signed-off-by: Andrew Thornton <art27@cantab.net> * Update templates/post-install.tmpl Co-authored-by: Lauris BH <lauris@nix.lv> * remove prefetch Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers')
-rw-r--r--routers/init.go114
-rw-r--r--routers/install.go36
-rw-r--r--routers/routes/routes.go9
3 files changed, 105 insertions, 54 deletions
diff --git a/routers/init.go b/routers/init.go
index 793033f4a4..702acb7260 100644
--- a/routers/init.go
+++ b/routers/init.go
@@ -117,9 +117,46 @@ func InitLocales() {
})
}
+// PreInstallInit preloads the configuration to check if we need to run install
+func PreInstallInit(ctx context.Context) bool {
+ setting.NewContext()
+ if !setting.InstallLock {
+ log.Trace("AppPath: %s", setting.AppPath)
+ log.Trace("AppWorkPath: %s", setting.AppWorkPath)
+ log.Trace("Custom path: %s", setting.CustomPath)
+ log.Trace("Log path: %s", setting.LogRootPath)
+ log.Trace("Preparing to run install page")
+ InitLocales()
+ if setting.EnableSQLite3 {
+ log.Info("SQLite3 Supported")
+ }
+ setting.InitDBConfig()
+ svg.Init()
+ }
+
+ return !setting.InstallLock
+}
+
+// PostInstallInit rereads the settings and starts up the database
+func PostInstallInit(ctx context.Context) {
+ setting.NewContext()
+ setting.InitDBConfig()
+ if setting.InstallLock {
+ if err := initDBEngine(ctx); err == nil {
+ log.Info("ORM engine initialization successful!")
+ } else {
+ log.Fatal("ORM engine initialization failed: %v", err)
+ }
+ svg.Init()
+ }
+}
+
// GlobalInit is for global configuration reload-able.
func GlobalInit(ctx context.Context) {
setting.NewContext()
+ if !setting.InstallLock {
+ log.Fatal("Gitea is not installed")
+ }
if err := git.Init(ctx); err != nil {
log.Fatal("Git module init failed: %v", err)
}
@@ -134,59 +171,50 @@ func GlobalInit(ctx context.Context) {
NewServices()
- if setting.InstallLock {
- highlight.NewContext()
- external.RegisterParsers()
- markup.Init()
- if err := initDBEngine(ctx); err == nil {
- log.Info("ORM engine initialization successful!")
- } else {
- log.Fatal("ORM engine initialization failed: %v", err)
- }
+ highlight.NewContext()
+ external.RegisterParsers()
+ markup.Init()
+ if err := initDBEngine(ctx); err == nil {
+ log.Info("ORM engine initialization successful!")
+ } else {
+ log.Fatal("ORM engine initialization failed: %v", err)
+ }
- if err := models.InitOAuth2(); err != nil {
- log.Fatal("Failed to initialize OAuth2 support: %v", err)
- }
+ if err := models.InitOAuth2(); err != nil {
+ log.Fatal("Failed to initialize OAuth2 support: %v", err)
+ }
- models.NewRepoContext()
+ models.NewRepoContext()
- // Booting long running goroutines.
- cron.NewContext()
- issue_indexer.InitIssueIndexer(false)
- code_indexer.Init()
- if err := stats_indexer.Init(); err != nil {
- log.Fatal("Failed to initialize repository stats indexer queue: %v", err)
- }
- mirror_service.InitSyncMirrors()
- webhook.InitDeliverHooks()
- if err := pull_service.Init(); err != nil {
- log.Fatal("Failed to initialize test pull requests queue: %v", err)
- }
- if err := task.Init(); err != nil {
- log.Fatal("Failed to initialize task scheduler: %v", err)
- }
- eventsource.GetManager().Init()
+ // Booting long running goroutines.
+ cron.NewContext()
+ issue_indexer.InitIssueIndexer(false)
+ code_indexer.Init()
+ if err := stats_indexer.Init(); err != nil {
+ log.Fatal("Failed to initialize repository stats indexer queue: %v", err)
}
+ mirror_service.InitSyncMirrors()
+ webhook.InitDeliverHooks()
+ if err := pull_service.Init(); err != nil {
+ log.Fatal("Failed to initialize test pull requests queue: %v", err)
+ }
+ if err := task.Init(); err != nil {
+ log.Fatal("Failed to initialize task scheduler: %v", err)
+ }
+ eventsource.GetManager().Init()
+
if setting.EnableSQLite3 {
log.Info("SQLite3 Supported")
}
checkRunMode()
- // Now because Install will re-run GlobalInit once it has set InstallLock
- // we can't tell if the ssh port will remain unused until that's done.
- // However, see FIXME comment in install.go
- if setting.InstallLock {
- if setting.SSH.StartBuiltinServer {
- ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
- log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
- } else {
- ssh.Unused()
- }
- }
-
- if setting.InstallLock {
- sso.Init()
+ if setting.SSH.StartBuiltinServer {
+ ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
+ log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
+ } else {
+ ssh.Unused()
}
+ sso.Init()
svg.Init()
}
diff --git a/routers/install.go b/routers/install.go
index a7fe557748..5d0d089dc0 100644
--- a/routers/install.go
+++ b/routers/install.go
@@ -5,7 +5,7 @@
package routers
import (
- "errors"
+ "net/http"
"os"
"os/exec"
"path/filepath"
@@ -27,13 +27,15 @@ import (
const (
// tplInstall template for installation page
- tplInstall base.TplName = "install"
+ tplInstall base.TplName = "install"
+ tplPostInstall base.TplName = "post-install"
)
// InstallInit prepare for rendering installation page
func InstallInit(ctx *context.Context) {
if setting.InstallLock {
- ctx.NotFound("Install", errors.New("Installation is prohibited"))
+ ctx.Header().Add("Refresh", "1; url="+setting.AppURL+"user/login")
+ ctx.HTML(200, tplPostInstall)
return
}
@@ -357,7 +359,8 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
return
}
- GlobalInit(graceful.GetManager().HammerContext())
+ // Re-read settings
+ PostInstallInit(ctx.Req.Context())
// Create admin account
if len(form.AdminName) > 0 {
@@ -380,6 +383,11 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
u, _ = models.GetUserByName(u.Name)
}
+ days := 86400 * setting.LogInRememberDays
+ ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
+ ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
+ setting.CookieRememberName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
+
// Auto-login for admin
if err = ctx.Session.Set("uid", u.ID); err != nil {
ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
@@ -397,12 +405,18 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
}
log.Info("First-time run install finished!")
- // FIXME: This isn't really enough to completely take account of new configuration
- // We should really be restarting:
- // - On windows this is probably just a simple restart
- // - On linux we can't just use graceful.RestartProcess() everything that was passed in on LISTEN_FDS
- // (active or not) needs to be passed out and everything new passed out too.
- // This means we need to prevent the cleanup goroutine from running prior to the second GlobalInit
+
ctx.Flash.Success(ctx.Tr("install.install_success"))
- ctx.Redirect(form.AppURL + "user/login")
+
+ ctx.Header().Add("Refresh", "1; url="+setting.AppURL+"user/login")
+ ctx.HTML(200, tplPostInstall)
+
+ // Now get the http.Server from this request and shut it down
+ // NB: This is not our hammerable graceful shutdown this is http.Server.Shutdown
+ srv := ctx.Req.Context().Value(http.ServerContextKey).(*http.Server)
+ go func() {
+ if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil {
+ log.Error("Unable to shutdown the install server! Error: %v", err)
+ }
+ }()
}
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index 9f7ff277cf..7f43b3b2b1 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -301,6 +301,15 @@ func NewMacaron() *macaron.Macaron {
return m
}
+// RegisterInstallRoute registers the install routes
+func RegisterInstallRoute(m *macaron.Macaron) {
+ m.Combo("/", routers.InstallInit).Get(routers.Install).
+ Post(binding.BindIgnErr(auth.InstallForm{}), routers.InstallPost)
+ m.NotFound(func(ctx *context.Context) {
+ ctx.Redirect(setting.AppURL, 302)
+ })
+}
+
// RegisterRoutes routes routes to Macaron
func RegisterRoutes(m *macaron.Macaron) {
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})