From 4239a89d66b5073903386cefeb523f548cbb9ce5 Mon Sep 17 00:00:00 2001 From: Hintay Date: Mon, 3 Feb 2025 00:19:02 +0900 Subject: [PATCH 1/5] feat: new command line options --- go.mod | 1 + go.sum | 2 ++ internal/cmd/main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ main.go | 15 +++++++-------- 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 internal/cmd/main.go diff --git a/go.mod b/go.mod index 8b0c24cb..2dd6bf80 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/tufanbarisyildirim/gonginx v0.0.0-20250120210832-12a9c7ae0c8a github.com/uozi-tech/cosy v1.14.3 github.com/uozi-tech/cosy-driver-sqlite v0.2.0 + github.com/urfave/cli/v3 v3.0.0-beta1 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.32.0 golang.org/x/net v0.34.0 diff --git a/go.sum b/go.sum index 8792c735..da38b3c2 100644 --- a/go.sum +++ b/go.sum @@ -1782,6 +1782,8 @@ github.com/uozi-tech/cosy-driver-postgres v0.2.1/go.mod h1:eAy1A89yHbAEfjkhNAifa github.com/uozi-tech/cosy-driver-sqlite v0.2.0 h1:eTpIMyGoFUK4JcaiKfJHD5AyiM6vtCwN98c7Bz5n25o= github.com/uozi-tech/cosy-driver-sqlite v0.2.0/go.mod h1:87a6mzn5IuEtIR4z7U4Ey8eKLGfNEOSkv7kPQlbNQgM= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg= +github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y= github.com/vinyldns/go-vinyldns v0.9.16 h1:GZJStDkcCk1F1AcRc64LuuMh+ENL8pHA0CVd4ulRMcQ= github.com/vinyldns/go-vinyldns v0.9.16/go.mod h1:5qIJOdmzAnatKjurI+Tl4uTus7GJKJxb+zitufjHs3Q= github.com/volcengine/volc-sdk-golang v1.0.194 h1:3o0INQzdtYJWvdGrtX02booCqPL5TsWSq2W1Ur7Bzlo= diff --git a/internal/cmd/main.go b/internal/cmd/main.go new file mode 100644 index 00000000..14f601dd --- /dev/null +++ b/internal/cmd/main.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "context" + "log" + "os" + + "github.com/urfave/cli/v3" +) + +func NewAppCmd() *cli.Command { + serve := false + + cmd := &cli.Command{ + Name: "nginx-ui", + Usage: "Yet another Nginx Web UI", + Commands: []*cli.Command{ + { + Name: "serve", + Usage: "Start the Nginx-UI server", + Action: func(ctx context.Context, command *cli.Command) error { + serve = true + return nil + }, + }, + }, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "app.ini", + Usage: "configuration file path", + }, + }, + DefaultCommand: "serve", + } + + if err := cmd.Run(context.Background(), os.Args); err != nil { + log.Fatal(err) + } else if !serve { + os.Exit(0) + } + return cmd +} diff --git a/main.go b/main.go index 6afec97f..5daa7806 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,23 @@ package main import ( - "flag" + "errors" "fmt" + "net/http" + "time" + + "github.com/0xJacky/Nginx-UI/internal/cmd" "github.com/0xJacky/Nginx-UI/internal/kernel" "github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/router" "github.com/0xJacky/Nginx-UI/settings" "github.com/gin-gonic/gin" "github.com/jpillora/overseer" - "errors" "github.com/uozi-tech/cosy" cKernel "github.com/uozi-tech/cosy/kernel" "github.com/uozi-tech/cosy/logger" cRouter "github.com/uozi-tech/cosy/router" cSettings "github.com/uozi-tech/cosy/settings" - "net/http" - "time" ) func Program(confPath string) func(state overseer.State) { @@ -58,12 +59,10 @@ func Program(confPath string) func(state overseer.State) { } func main() { - var confPath string - flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") - flag.Parse() + appCmd := cmd.NewAppCmd() + confPath := appCmd.String("config") settings.Init(confPath) - overseer.Run(overseer.Config{ Program: Program(confPath), Address: fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port), From f213bdf7d6ad0fa6e409292107cf4da0391665c6 Mon Sep 17 00:00:00 2001 From: Hintay Date: Mon, 3 Feb 2025 21:56:08 +0900 Subject: [PATCH 2/5] feat: add version command --- .gitignore | 1 + cmd/version/generate.go | 83 +++++++++++++++++++++++++++++++++++++ internal/cmd/main.go | 7 ++++ internal/version/version.go | 7 ++++ main.go | 2 + 5 files changed, 100 insertions(+) create mode 100644 cmd/version/generate.go create mode 100644 internal/version/version.go diff --git a/.gitignore b/.gitignore index 6dd5e8bb..75ccda17 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ app/.status_hash casdoor.pub .idea/deployment.xml .idea/webServers.xml +*.gen.go diff --git a/cmd/version/generate.go b/cmd/version/generate.go new file mode 100644 index 00000000..cf8144e0 --- /dev/null +++ b/cmd/version/generate.go @@ -0,0 +1,83 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "io/fs" + "log" + "os" + "path" + "runtime" + + "github.com/0xJacky/Nginx-UI/app" +) + +type VersionInfo struct { + Version string `json:"version"` + BuildId int `json:"build_id"` + TotalBuild int `json:"total_build"` +} + +func main() { + _, file, _, ok := runtime.Caller(0) + if !ok { + log.Print("Unable to get the current file") + return + } + basePath := path.Join(path.Dir(file), "../../") + + versionFile, err := app.DistFS.Open("dist/version.json") + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + log.Print("\"dist/version.json\" not found, load from src instead") + versionFile, err = os.Open(path.Join(basePath, "app/src/version.json")) + } + + if err != nil { + log.Fatal(err) + return + } + } + + defer func(versionFile fs.File) { + err := versionFile.Close() + if err != nil { + log.Fatal(err) + } + }(versionFile) + + // Read the version.json file + data, err := io.ReadAll(versionFile) + if err != nil { + log.Fatalf("Failed to read version.json: %v", err) + } + + // Parse the JSON data + var versionInfo VersionInfo + err = json.Unmarshal(data, &versionInfo) + if err != nil { + log.Fatalf("Failed to parse JSON: %v", err) + } + + // Generate the version.gen.go file content + genContent := fmt.Sprintf(`// Code generated by cmd/version/generate.go; DO NOT EDIT. + +package version + +func init() { + Version = "%s" + BuildId = %d + TotalBuild = %d +} +`, versionInfo.Version, versionInfo.BuildId, versionInfo.TotalBuild) + + genPath := path.Join(basePath, "internal/version/version.gen.go") + err = os.WriteFile(genPath, []byte(genContent), 0644) + if err != nil { + log.Fatalf("Failed to write version.gen.go: %v", err) + } + + fmt.Println("version.gen.go has been generated successfully.") +} diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 14f601dd..68476e13 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -2,9 +2,11 @@ package cmd import ( "context" + "fmt" "log" "os" + "github.com/0xJacky/Nginx-UI/internal/version" "github.com/urfave/cli/v3" ) @@ -32,6 +34,11 @@ func NewAppCmd() *cli.Command { }, }, DefaultCommand: "serve", + Version: version.Version, + } + + cli.VersionPrinter = func(cmd *cli.Command) { + fmt.Printf("%s (%d)\n", cmd.Root().Version, version.BuildId) } if err := cmd.Run(context.Background(), os.Args); err != nil { diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..29f5d30b --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,7 @@ +package version + +var ( + Version = "" + BuildId = 0 + TotalBuild = 0 +) diff --git a/main.go b/main.go index 5daa7806..db97a73e 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,8 @@ import ( cSettings "github.com/uozi-tech/cosy/settings" ) +//go:generate go run cmd/version/generate.go + func Program(confPath string) func(state overseer.State) { return func(state overseer.State) { defer logger.Sync() From c774359531f44b65b087173ce8466ef659691e28 Mon Sep 17 00:00:00 2001 From: Hintay Date: Mon, 3 Feb 2025 22:04:35 +0900 Subject: [PATCH 3/5] feat: generate version in build workflows --- .github/workflows/build.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dbcd4710..c3b16c5b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -142,6 +142,18 @@ jobs: with: go-version: ^1.23.0 + - name: Download app artifacts + uses: actions/download-artifact@v4 + with: + name: app-dist + path: app/dist + + - name: Generate files + env: + GOOS: linux + GOARCH: amd64 + run: go generate + - name: Setup compiler environment id: info run: | @@ -182,12 +194,6 @@ jobs: echo "CXX=${{ env.ARCH_NAME }}-clang++" >> $GITHUB_ENV echo "LD_FLAGS=-w" >> $GITHUB_ENV - - name: Download app artifacts - uses: actions/download-artifact@v4 - with: - name: app-dist - path: app/dist - - name: Build run: | mkdir -p dist From de420ab6ff78483fe0e85b353e14e939caf69d33 Mon Sep 17 00:00:00 2001 From: Hintay Date: Tue, 4 Feb 2025 00:09:31 +0900 Subject: [PATCH 4/5] fix: use direct file instead of embedded file for generate version --- cmd/version/generate.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/version/generate.go b/cmd/version/generate.go index cf8144e0..304f16f8 100644 --- a/cmd/version/generate.go +++ b/cmd/version/generate.go @@ -10,8 +10,6 @@ import ( "os" "path" "runtime" - - "github.com/0xJacky/Nginx-UI/app" ) type VersionInfo struct { @@ -28,7 +26,7 @@ func main() { } basePath := path.Join(path.Dir(file), "../../") - versionFile, err := app.DistFS.Open("dist/version.json") + versionFile, err := os.Open(path.Join(basePath, "app/dist/version.json")) if err != nil { if errors.Is(err, fs.ErrNotExist) { log.Print("\"dist/version.json\" not found, load from src instead") From f9802e6b2badcf5cbc22644140bc69014b87e59a Mon Sep 17 00:00:00 2001 From: Hintay Date: Tue, 4 Feb 2025 01:38:15 +0900 Subject: [PATCH 5/5] feat: change version API to generated version --- api/cluster/node.go | 6 ++++-- api/system/upgrade.go | 14 +++++--------- internal/upgrader/info.go | 21 ++------------------- internal/version/version.go | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/api/cluster/node.go b/api/cluster/node.go index 7544ed23..399b9807 100644 --- a/api/cluster/node.go +++ b/api/cluster/node.go @@ -1,14 +1,16 @@ package cluster import ( + "net/http" + "github.com/0xJacky/Nginx-UI/api" analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic" "github.com/0xJacky/Nginx-UI/internal/upgrader" + "github.com/0xJacky/Nginx-UI/internal/version" "github.com/dustin/go-humanize" "github.com/gin-gonic/gin" "github.com/shirou/gopsutil/v4/cpu" "github.com/shirou/gopsutil/v4/disk" - "net/http" ) func GetCurrentNode(c *gin.Context) { @@ -26,7 +28,7 @@ func GetCurrentNode(c *gin.Context) { } cpuInfo, _ := cpu.Info() memory, _ := analytic2.GetMemoryStat() - ver, _ := upgrader.GetCurrentVersion() + ver := version.GetVersionInfo() diskUsage, _ := disk.Usage(".") nodeInfo := analytic2.NodeInfo{ diff --git a/api/system/upgrade.go b/api/system/upgrade.go index 3dbd3478..d3c96ce0 100644 --- a/api/system/upgrade.go +++ b/api/system/upgrade.go @@ -1,14 +1,16 @@ package system import ( + "net/http" + "os" + "github.com/0xJacky/Nginx-UI/api" "github.com/0xJacky/Nginx-UI/internal/upgrader" + "github.com/0xJacky/Nginx-UI/internal/version" "github.com/0xJacky/Nginx-UI/settings" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/uozi-tech/cosy/logger" - "net/http" - "os" ) func GetRelease(c *gin.Context) { @@ -32,13 +34,7 @@ func GetRelease(c *gin.Context) { } func GetCurrentVersion(c *gin.Context) { - curVer, err := upgrader.GetCurrentVersion() - if err != nil { - api.ErrHandler(c, err) - return - } - - c.JSON(http.StatusOK, curVer) + c.JSON(http.StatusOK, version.GetVersionInfo()) } const ( diff --git a/internal/upgrader/info.go b/internal/upgrader/info.go index 6d536b1a..96252120 100644 --- a/internal/upgrader/info.go +++ b/internal/upgrader/info.go @@ -1,12 +1,11 @@ package upgrader import ( - "encoding/json" - "github.com/0xJacky/Nginx-UI/app" - "github.com/pkg/errors" "os" "path/filepath" "runtime" + + "github.com/pkg/errors" ) type RuntimeInfo struct { @@ -41,19 +40,3 @@ func GetRuntimeInfo() (r RuntimeInfo, err error) { return } - -func GetCurrentVersion() (c CurVersion, err error) { - verJson, err := app.DistFS.ReadFile("dist/version.json") - if err != nil { - err = errors.Wrap(err, "service.GetCurrentVersion ReadFile err") - return - } - - err = json.Unmarshal(verJson, &c) - if err != nil { - err = errors.Wrap(err, "service.GetCurrentVersion json.Unmarshal err") - return - } - - return -} diff --git a/internal/version/version.go b/internal/version/version.go index 29f5d30b..4eecdad1 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -5,3 +5,22 @@ var ( BuildId = 0 TotalBuild = 0 ) + +type Info struct { + Version string `json:"version"` + BuildId int `json:"build_id"` + TotalBuild int `json:"total_build"` +} + +var versionInfo *Info + +func GetVersionInfo() *Info { + if versionInfo == nil { + versionInfo = &Info{ + Version: Version, + BuildId: BuildId, + TotalBuild: TotalBuild, + } + } + return versionInfo +}