add enviroment variable for golang beckend close #6
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"backend/env"
|
||||
"backend/models"
|
||||
"backend/server"
|
||||
"backend/utils"
|
||||
@@ -27,20 +28,27 @@ func main() {
|
||||
|
||||
flag.Var(&allowOrigins, "allowOrigin", "Allowed origin (can repeat this flag)")
|
||||
|
||||
spa := flag.String("spa", "./dist/spa", "quasar spa files")
|
||||
workingDir := flag.String("workingDirectory", ".", "quasar spa files")
|
||||
ip := flag.String("ip", "0.0.0.0", "server listening ip")
|
||||
envFile := flag.String("env", ".env", "enviroment file")
|
||||
organization := flag.String("organization", "", "self signed ciertificate organization")
|
||||
port := flag.Uint("port", 9500, "server listening port")
|
||||
https := flag.Bool("https", false, "serves as https needs flag -cert and -chain")
|
||||
sslKey := flag.String("privkey", "", "ssl private key path")
|
||||
sslFullchain := flag.String("fullchain", "", "ssl fullchain path")
|
||||
debug := flag.Bool("debug", false, "log debug")
|
||||
flag.Parse()
|
||||
|
||||
// load enviroment file if exists
|
||||
if err := env.Load(*envFile); err != nil {
|
||||
fmt.Println("no .env found path: ", *envFile)
|
||||
}
|
||||
|
||||
devMode := env.InDevelopmentMode()
|
||||
// set gin mode
|
||||
if !devMode {
|
||||
gin.SetMode(env.GinMode.GetValue())
|
||||
}
|
||||
|
||||
workingDir := env.WorkingDir.GetValue()
|
||||
spa := env.Spa.GetValue()
|
||||
|
||||
//change working directory only if value is given
|
||||
if *workingDir != "." && *workingDir != "" {
|
||||
os.Chdir(*workingDir)
|
||||
if workingDir != "." && workingDir != "" {
|
||||
os.Chdir(workingDir)
|
||||
}
|
||||
|
||||
wd, err := os.Getwd()
|
||||
@@ -55,7 +63,7 @@ func main() {
|
||||
MaxSize: 1,
|
||||
MaxBackup: 3,
|
||||
MaxAge: 28,
|
||||
Debug: *debug,
|
||||
Debug: env.InDebugMode(),
|
||||
TerminalOut: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -82,11 +90,11 @@ func main() {
|
||||
|
||||
//get local ip
|
||||
httpString := "http://"
|
||||
if *https {
|
||||
if env.Https.GetBoolValue() {
|
||||
httpString = "https://"
|
||||
|
||||
}
|
||||
allowOrigins = append(allowOrigins, httpString+"localhost:9000", httpString+"localhost:9500", httpString+"127.0.0.1:9500", httpString+"0.0.0.0:9500")
|
||||
allowOrigins = append(allowOrigins, httpString+"localhost:9000", httpString+"localhost:9500", httpString+"127.0.0.1:9000", httpString+"0.0.0.0:9500")
|
||||
|
||||
localIP, err := utils.GetLocalIP()
|
||||
if err != nil {
|
||||
@@ -153,7 +161,7 @@ func main() {
|
||||
api.POST("/login/refresh", accessHandler.Refresh)
|
||||
|
||||
// Serve static files
|
||||
s.Routes.StaticFS("/assets", gin.Dir(filepath.Join(*spa, "assets"), true))
|
||||
s.Routes.StaticFS("/assets", gin.Dir(filepath.Join(spa, "assets"), true))
|
||||
s.Routes.NoRoute(func(c *gin.Context) {
|
||||
// Disallow fallback for /api paths
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
@@ -161,44 +169,44 @@ func main() {
|
||||
return
|
||||
}
|
||||
// Try to serve file from SPA directory
|
||||
filePath := filepath.Join(*spa, c.Request.URL.Path)
|
||||
filePath := filepath.Join(spa, c.Request.URL.Path)
|
||||
if _, err := os.Stat(filePath); err == nil {
|
||||
c.File(filePath)
|
||||
return
|
||||
}
|
||||
// Fallback to index.html for SPA routing
|
||||
c.File(filepath.Join(*spa, "index.html"))
|
||||
c.File(filepath.Join(spa, "index.html"))
|
||||
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
if err := utils.OpenBrowser(fmt.Sprintf("%slocalhost:%d", httpString, *port), logger); err != nil {
|
||||
if err := utils.OpenBrowser(fmt.Sprintf("%slocalhost:%s", httpString, env.Port.GetValue()), logger); err != nil {
|
||||
logger.Error("main", fmt.Sprintf("starting browser error : %s", err))
|
||||
}
|
||||
}()
|
||||
|
||||
if *https {
|
||||
if *sslFullchain == "" {
|
||||
if env.Https.GetBoolValue() {
|
||||
if env.Fullchain.GetValue() == "" {
|
||||
logger.Error("ssl certificate", "-cert flag not given for https server")
|
||||
log.Fatal("-cert flag not given for https server")
|
||||
}
|
||||
if *sslKey == "" {
|
||||
if env.PrivKey.GetValue() == "" {
|
||||
logger.Error("ssl key", "-chain flag not given for https server")
|
||||
log.Fatal("-chain flag not given for https server")
|
||||
}
|
||||
|
||||
// start https server
|
||||
logger.Info("main", fmt.Sprintf("https listen on ip: %s port: %d", *ip, *port))
|
||||
if err := s.ServeHttps(*ip, *port, cert.Cert{Organization: *organization, CertFile: *sslFullchain, KeyFile: *sslKey}); err != nil {
|
||||
logger.Info("main", fmt.Sprintf("https listen on ip: %s port: %s", env.Url.GetValue(), env.Port.GetValue()))
|
||||
if err := s.ServeHttps(env.Url.GetValue(), env.Port.GetUIntValue(), cert.Cert{Organization: *organization, CertFile: env.Fullchain.GetValue(), KeyFile: env.PrivKey.GetValue()}); err != nil {
|
||||
logger.Error("main", "error https server "+err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// start http server
|
||||
logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %d", *ip, *port))
|
||||
if err := s.ServeHttp(*ip, *port); err != nil {
|
||||
logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %s", env.Url.GetValue(), env.Port.GetValue()))
|
||||
if err := s.ServeHttp(env.Url.GetValue(), env.Port.GetUIntValue()); err != nil {
|
||||
logger.Error("main", "error http server "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user