@ -19,6 +19,8 @@ import (
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"
"code.gitea.io/gitea/routers/routes"
"gitea.com/macaron/macaron"
context2 "github.com/gorilla/context"
context2 "github.com/gorilla/context"
"github.com/unknwon/com"
"github.com/unknwon/com"
"github.com/urfave/cli"
"github.com/urfave/cli"
@ -114,6 +116,39 @@ func runWeb(ctx *cli.Context) error {
setting . WritePIDFile = true
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
// Perform global initialization
routers . GlobalInit ( graceful . GetManager ( ) . HammerContext ( ) )
routers . GlobalInit ( graceful . GetManager ( ) . HammerContext ( ) )
@ -121,41 +156,49 @@ func runWeb(ctx *cli.Context) error {
m := routes . NewMacaron ( )
m := routes . NewMacaron ( )
routes . RegisterRoutes ( m )
routes . RegisterRoutes ( m )
// Flag for port number in case first time run conflict.
err := listen ( m , true )
if ctx . IsSet ( "port" ) {
<- graceful . GetManager ( ) . Done ( )
setting . AppURL = strings . Replace ( setting . AppURL , setting . HTTPPort , ctx . String ( "port" ) , 1 )
log . Info ( "PID: %d Gitea Web Finished" , os . Getpid ( ) )
setting . HTTPPort = ctx . String ( "port" )
log . Close ( )
return err
}
switch setting . Protocol {
func setPort ( port string ) error {
case setting . UnixSocket :
setting . AppURL = strings . Replace ( setting . AppURL , setting . HTTPPort , port , 1 )
case setting . FCGI :
setting . HTTPPort = port
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 := string ( setting . Protocol ) + "://"
switch setting . Protocol {
if setting . HTTPAddr == "0.0.0.0" {
case setting . UnixSocket :
defaultLocalURL += "localhost"
case setting . FCGI :
} else {
case setting . FCGIUnix :
defaultLocalURL += setting . HTTPAddr
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 {
if err := cfg . SaveTo ( setting . CustomConf ) ; err != nil {
return fmt . Errorf ( "Error saving generated JWT Secret to custom config: %v" , err )
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
listenAddr := setting . HTTPAddr
if setting . Protocol != setting . UnixSocket && setting . Protocol != setting . FCGIUnix {
if setting . Protocol != setting . UnixSocket && setting . Protocol != setting . FCGIUnix {
listenAddr = net . JoinHostPort ( listenAddr , setting . HTTPPort )
listenAddr = net . JoinHostPort ( listenAddr , setting . HTTPPort )
@ -166,37 +209,40 @@ func runWeb(ctx *cli.Context) error {
log . Info ( "LFS server enabled" )
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
var err error
switch setting . Protocol {
switch setting . Protocol {
case setting . HTTP :
case setting . HTTP :
NoHTTPRedirector ( )
if handleRedirector {
NoHTTPRedirector ( )
}
err = runHTTP ( "tcp" , listenAddr , context2 . ClearHandler ( m ) )
err = runHTTP ( "tcp" , listenAddr , context2 . ClearHandler ( m ) )
case setting . HTTPS :
case setting . HTTPS :
if setting . EnableLetsEncrypt {
if setting . EnableLetsEncrypt {
err = runLetsEncrypt ( listenAddr , setting . Domain , setting . LetsEncryptDirectory , setting . LetsEncryptEmail , context2 . ClearHandler ( m ) )
err = runLetsEncrypt ( listenAddr , setting . Domain , setting . LetsEncryptDirectory , setting . LetsEncryptEmail , context2 . ClearHandler ( m ) )
break
break
}
}
if setting . RedirectOtherPort {
if handleRedirector {
go runHTTPRedirector ( )
if setting . RedirectOtherPort {
} else {
go runHTTPRedirector ( )
NoHTTPRedirector ( )
} else {
NoHTTPRedirector ( )
}
}
}
err = runHTTPS ( "tcp" , listenAddr , setting . CertFile , setting . KeyFile , context2 . ClearHandler ( m ) )
err = runHTTPS ( "tcp" , listenAddr , setting . CertFile , setting . KeyFile , context2 . ClearHandler ( m ) )
case setting . FCGI :
case setting . FCGI :
NoHTTPRedirector ( )
if handleRedirector {
NoHTTPRedirector ( )
}
err = runFCGI ( "tcp" , listenAddr , context2 . ClearHandler ( m ) )
err = runFCGI ( "tcp" , listenAddr , context2 . ClearHandler ( m ) )
case setting . UnixSocket :
case setting . UnixSocket :
NoHTTPRedirector ( )
if handleRedirector {
NoHTTPRedirector ( )
}
err = runHTTP ( "unix" , listenAddr , context2 . ClearHandler ( m ) )
err = runHTTP ( "unix" , listenAddr , context2 . ClearHandler ( m ) )
case setting . FCGIUnix :
case setting . FCGIUnix :
NoHTTPRedirector ( )
if handleRedirector {
NoHTTPRedirector ( )
}
err = runFCGI ( "unix" , listenAddr , context2 . ClearHandler ( m ) )
err = runFCGI ( "unix" , listenAddr , context2 . ClearHandler ( m ) )
default :
default :
log . Fatal ( "Invalid protocol: %s" , setting . Protocol )
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 . Critical ( "Failed to start server: %v" , err )
}
}
log . Info ( "HTTP Listener: %s Closed" , listenAddr )
log . Info ( "HTTP Listener: %s Closed" , listenAddr )
<- graceful . GetManager ( ) . Done ( )
return err
log . Info ( "PID: %d Gitea Web Finished" , os . Getpid ( ) )
log . Close ( )
return nil
}
}