diff --git a/README.md b/README.md index e5679f9b..faa22571 100644 --- a/README.md +++ b/README.md @@ -71,18 +71,16 @@ server { ssl_certificate /path/to/ssl_cert; ssl_certificate_key /path/to/ssl_cert_key; - root /path/to/nginx-ui/frontend/dist; - location / { - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; - proxy_pass http://127.0.0.1:9000/; - } + proxy_pass http://127.0.0.1:9000/; + } } ``` diff --git a/api/api.go b/api/api.go index 66fddf39..930c84db 100644 --- a/api/api.go +++ b/api/api.go @@ -1,8 +1,6 @@ package api import ( - "bytes" - "encoding/json" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/zh" @@ -12,30 +10,8 @@ import ( "log" "net/http" "reflect" - "regexp" ) -type JsonSnakeCase struct { - Value interface{} -} - -func (c JsonSnakeCase) MarshalJSON() ([]byte, error) { - // Regexp definitions - var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`) - var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`) - marshalled, err := json.Marshal(c.Value) - converted := keyMatchRegex.ReplaceAllFunc( - marshalled, - func(match []byte) []byte { - return bytes.ToLower(wordBarrierRegex.ReplaceAll( - match, - []byte(`${1}_${2}`), - )) - }, - ) - return converted, err -} - func ErrHandler(c *gin.Context, err error) { log.Println(err) c.JSON(http.StatusInternalServerError, gin.H{ diff --git a/frontend/version.json b/frontend/version.json index d0047d32..67c23b80 100644 --- a/frontend/version.json +++ b/frontend/version.json @@ -1 +1 @@ -{"version":"1.1.0","build_id":3,"total_build":20} \ No newline at end of file +{"version":"1.1.0","build_id":4,"total_build":21} \ No newline at end of file diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 0fa8c9bc..d4f07f49 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -3032,15 +3032,10 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001251: - version "1.0.30001252" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001252.tgz#cb16e4e3dafe948fc4a9bb3307aea054b912019a" - integrity sha512-I56jhWDGMtdILQORdusxBOH+Nl/KgQSdDmpJezYddnAkVOmnoU8zwjTV9xAjMIYxr0iPreEAVylCGcmHCjfaOw== - -caniuse-lite@^1.0.30001286: - version "1.0.30001302" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001302.tgz#da57ce61c51177ef3661eeed7faef392d3790aaa" - integrity sha512-YYTMO+tfwvgUN+1ZnRViE53Ma1S/oETg+J2lISsqi/ZTNThj3ZYBOKP2rHwJc37oCsPqAzJ3w2puZHn0xlLPPw== +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001251, caniuse-lite@^1.0.30001286: + version "1.0.30001312" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001312.tgz" + integrity sha512-Wiz1Psk2MEK0pX3rUzWaunLTZzqS2JYZFzNKqAiJGiuxIjRPLgV6+VDPOg6lQOUxmDwhTlh198JsTTi8Hzw6aQ== case-sensitive-paths-webpack-plugin@^2.3.0: version "2.4.0" diff --git a/main.go b/main.go index 429fdbf3..2f1332dd 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,24 @@ package main import ( + "context" "flag" "github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/router" "github.com/0xJacky/Nginx-UI/settings" "github.com/0xJacky/Nginx-UI/tool" "log" + "net/http" + "os/signal" + "syscall" + "time" ) func main() { + // Create context that listens for the interrupt signal from the OS. + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + var dataDir string flag.StringVar(&dataDir, "d", ".", "Specify the data dir") flag.Parse() @@ -17,16 +26,38 @@ func main() { settings.Init(dataDir) model.Init() - r := router.InitRouter() + srv := &http.Server{ + Addr: ":" + settings.ServerSettings.HttpPort, + Handler: router.InitRouter(), + } log.Printf("nginx config dir path: %s", tool.GetNginxConfPath("")) go tool.AutoCert() - err := r.Run(":" + settings.ServerSettings.HttpPort) + // Initializing the server in a goroutine so that + // it won't block the graceful shutdown handling below + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("listen: %s\n", err) + } + }() - if err != nil { - log.Fatal(err) + // Listen for the interrupt signal. + <-ctx.Done() + + // Restore default behavior on the interrupt signal and notify user of shutdown. + stop() + log.Println("shutting down gracefully, press Ctrl+C again to force") + + // The context is used to inform the server it has 5 seconds to finish + // the request it is currently handling + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Fatal("Server forced to shutdown: ", err) } + log.Println("Server exiting") + }