diff options
author | zeripath <art27@cantab.net> | 2020-10-19 22:03:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-19 17:03:08 -0400 |
commit | 2f1353a2f33762e10a304cbebf3a6a8d0381d316 (patch) | |
tree | b345bf060a107341c64447f7132d9e2b8d37a7b3 /cmd/web.go | |
parent | 3ddf3f93d6346ac9440a7a571faea4b5c1c329be (diff) | |
download | gitea-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 'cmd/web.go')
-rw-r--r-- | cmd/web.go | 135 |
1 files changed, 89 insertions, 46 deletions
diff --git a/cmd/web.go b/cmd/web.go index 92d4b11b69..e16d1afb53 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -19,6 +19,8 @@ import ( "code.gitea.io/gitea/routers" "code.gitea.io/gitea/routers/routes" + "gitea.com/macaron/macaron" + context2 "github.com/gorilla/context" "github.com/unknwon/com" "github.com/urfave/cli" @@ -114,6 +116,39 @@ func runWeb(ctx *cli.Context) error { setting.WritePIDFile = true } + // Flag for port number in case first time run conflict. + if ctx.IsSet("port") { + if err := setPort(ctx.String("port")); err != nil { + return err + } + } + + // Perform pre-initialization + needsInstall := routers.PreInstallInit(graceful.GetManager().HammerContext()) + if needsInstall { + m := routes.NewMacaron() + routes.RegisterInstallRoute(m) + err := listen(m, false) + select { + case <-graceful.GetManager().IsShutdown(): + <-graceful.GetManager().Done() + log.Info("PID: %d Gitea Web Finished", os.Getpid()) + log.Close() + return err + default: + } + } else { + NoInstallListener() + } + + if setting.EnablePprof { + go func() { + log.Info("Starting pprof server on localhost:6060") + log.Info("%v", http.ListenAndServe("localhost:6060", nil)) + }() + } + + log.Info("Global init") // Perform global initialization routers.GlobalInit(graceful.GetManager().HammerContext()) @@ -121,41 +156,49 @@ func runWeb(ctx *cli.Context) error { m := routes.NewMacaron() routes.RegisterRoutes(m) - // Flag for port number in case first time run conflict. - if ctx.IsSet("port") { - setting.AppURL = strings.Replace(setting.AppURL, setting.HTTPPort, ctx.String("port"), 1) - setting.HTTPPort = ctx.String("port") + err := listen(m, true) + <-graceful.GetManager().Done() + log.Info("PID: %d Gitea Web Finished", os.Getpid()) + log.Close() + return err +} - switch setting.Protocol { - case setting.UnixSocket: - case setting.FCGI: - case setting.FCGIUnix: - default: - // Save LOCAL_ROOT_URL if port changed - cfg := ini.Empty() - if com.IsFile(setting.CustomConf) { - // Keeps custom settings if there is already something. - if err := cfg.Append(setting.CustomConf); err != nil { - return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err) - } - } +func setPort(port string) error { + setting.AppURL = strings.Replace(setting.AppURL, setting.HTTPPort, port, 1) + setting.HTTPPort = port - defaultLocalURL := string(setting.Protocol) + "://" - if setting.HTTPAddr == "0.0.0.0" { - defaultLocalURL += "localhost" - } else { - defaultLocalURL += setting.HTTPAddr + switch setting.Protocol { + case setting.UnixSocket: + case setting.FCGI: + case setting.FCGIUnix: + default: + // Save LOCAL_ROOT_URL if port changed + cfg := ini.Empty() + if com.IsFile(setting.CustomConf) { + // Keeps custom settings if there is already something. + if err := cfg.Append(setting.CustomConf); err != nil { + return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err) } - defaultLocalURL += ":" + setting.HTTPPort + "/" + } + + defaultLocalURL := string(setting.Protocol) + "://" + if setting.HTTPAddr == "0.0.0.0" { + defaultLocalURL += "localhost" + } else { + defaultLocalURL += setting.HTTPAddr + } + defaultLocalURL += ":" + setting.HTTPPort + "/" - cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL) + cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL) - if err := cfg.SaveTo(setting.CustomConf); err != nil { - return fmt.Errorf("Error saving generated JWT Secret to custom config: %v", err) - } + if err := cfg.SaveTo(setting.CustomConf); err != nil { + return fmt.Errorf("Error saving generated JWT Secret to custom config: %v", err) } } + return nil +} +func listen(m *macaron.Macaron, handleRedirector bool) error { listenAddr := setting.HTTPAddr if setting.Protocol != setting.UnixSocket && setting.Protocol != setting.FCGIUnix { listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort) @@ -166,37 +209,40 @@ func runWeb(ctx *cli.Context) error { log.Info("LFS server enabled") } - if setting.EnablePprof { - go func() { - log.Info("Starting pprof server on localhost:6060") - log.Info("%v", http.ListenAndServe("localhost:6060", nil)) - }() - } - var err error switch setting.Protocol { case setting.HTTP: - NoHTTPRedirector() + if handleRedirector { + NoHTTPRedirector() + } err = runHTTP("tcp", listenAddr, context2.ClearHandler(m)) case setting.HTTPS: if setting.EnableLetsEncrypt { err = runLetsEncrypt(listenAddr, setting.Domain, setting.LetsEncryptDirectory, setting.LetsEncryptEmail, context2.ClearHandler(m)) break } - if setting.RedirectOtherPort { - go runHTTPRedirector() - } else { - NoHTTPRedirector() + if handleRedirector { + if setting.RedirectOtherPort { + go runHTTPRedirector() + } else { + NoHTTPRedirector() + } } err = runHTTPS("tcp", listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m)) case setting.FCGI: - NoHTTPRedirector() + if handleRedirector { + NoHTTPRedirector() + } err = runFCGI("tcp", listenAddr, context2.ClearHandler(m)) case setting.UnixSocket: - NoHTTPRedirector() + if handleRedirector { + NoHTTPRedirector() + } err = runHTTP("unix", listenAddr, context2.ClearHandler(m)) case setting.FCGIUnix: - NoHTTPRedirector() + if handleRedirector { + NoHTTPRedirector() + } err = runFCGI("unix", listenAddr, context2.ClearHandler(m)) default: log.Fatal("Invalid protocol: %s", setting.Protocol) @@ -206,8 +252,5 @@ func runWeb(ctx *cli.Context) error { log.Critical("Failed to start server: %v", err) } log.Info("HTTP Listener: %s Closed", listenAddr) - <-graceful.GetManager().Done() - log.Info("PID: %d Gitea Web Finished", os.Getpid()) - log.Close() - return nil + return err } |