diff options
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 } |