@ -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,10 +156,16 @@ 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
}
func setPort ( port string ) error {
setting . AppURL = strings . Replace ( setting . AppURL , setting . HTTPPort , port , 1 )
setting . HTTPPort = port
switch setting . Protocol {
case setting . UnixSocket :
@ -154,8 +195,10 @@ func runWeb(ctx *cli.Context) error {
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 :
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 handleRedirector {
if setting . RedirectOtherPort {
go runHTTPRedirector ( )
} else {
NoHTTPRedirector ( )
}
}
err = runHTTPS ( "tcp" , listenAddr , setting . CertFile , setting . KeyFile , context2 . ClearHandler ( m ) )
case setting . FCGI :
if handleRedirector {
NoHTTPRedirector ( )
}
err = runFCGI ( "tcp" , listenAddr , context2 . ClearHandler ( m ) )
case setting . UnixSocket :
if handleRedirector {
NoHTTPRedirector ( )
}
err = runHTTP ( "unix" , listenAddr , context2 . ClearHandler ( m ) )
case setting . FCGIUnix :
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
}