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