diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 857f50a7..7ad0a429 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -2,6 +2,7 @@ before: hooks: # You may remove this if you don't use go modules. - go mod tidy + - go install github.com/rakyll/statik snapshot: name_template: '{{ .Tag }}' diff --git a/Makefile b/Makefile index ea9d599c..23e9592f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: start build +.PHONY: prebuild start build ROOT:=$(shell pwd -P) GIT_COMMIT:=$(shell git --work-tree ${ROOT} rev-parse 'HEAD^{commit}') @@ -6,18 +6,23 @@ _GIT_VERSION:=$(shell git --work-tree ${ROOT} describe --tags --abbrev=14 "${GIT TAG=$(shell echo "${_GIT_VERSION}" | awk -F"-" '{print $$1}') RELEASE_VERSION:="$(TAG)-$(GIT_COMMIT)" +prebuild: + echo "begin download and embed the front-end file..." + sh fe.sh + echo "front-end file download and embedding completed." + all: build -build: +build: prebuild go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version=$(RELEASE_VERSION)" -o n9e ./cmd/center/main.go -build-alert: +build-alert: prebuild go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version=$(RELEASE_VERSION)" -o n9e-alert ./cmd/alert/main.go -build-pushgw: +build-pushgw: prebuild go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version=$(RELEASE_VERSION)" -o n9e-pushgw ./cmd/pushgw/main.go -build-cli: +build-cli: prebuild go build -ldflags "-w -s -X github.com/ccfos/nightingale/v6/pkg/version.Version=$(RELEASE_VERSION)" -o n9e-cli ./cmd/cli/main.go run: diff --git a/center/router/router.go b/center/router/router.go index 6d454f67..582feee0 100644 --- a/center/router/router.go +++ b/center/router/router.go @@ -3,8 +3,6 @@ package router import ( "fmt" "net/http" - "path" - "runtime" "strings" "time" @@ -12,13 +10,15 @@ import ( "github.com/ccfos/nightingale/v6/center/cstats" "github.com/ccfos/nightingale/v6/center/metas" "github.com/ccfos/nightingale/v6/center/sso" + _ "github.com/ccfos/nightingale/v6/front/statik" "github.com/ccfos/nightingale/v6/memsto" "github.com/ccfos/nightingale/v6/pkg/aop" "github.com/ccfos/nightingale/v6/pkg/ctx" "github.com/ccfos/nightingale/v6/pkg/httpx" "github.com/ccfos/nightingale/v6/prom" "github.com/ccfos/nightingale/v6/storage" - "github.com/toolkits/pkg/runner" + "github.com/rakyll/statik/fs" + "github.com/toolkits/pkg/logger" "github.com/gin-gonic/gin" ) @@ -89,38 +89,32 @@ func languageDetector(i18NHeaderKey string) gin.HandlerFunc { } } -func (rt *Router) configNoRoute(r *gin.Engine) { +func (rt *Router) configNoRoute(r *gin.Engine, fs *http.FileSystem) { r.NoRoute(func(c *gin.Context) { arr := strings.Split(c.Request.URL.Path, ".") suffix := arr[len(arr)-1] + switch suffix { case "png", "jpeg", "jpg", "svg", "ico", "gif", "css", "js", "html", "htm", "gz", "zip", "map": - cwdarr := []string{"/"} - if runtime.GOOS == "windows" { - cwdarr[0] = "" - } - cwdarr = append(cwdarr, strings.Split(runner.Cwd, "/")...) - cwdarr = append(cwdarr, "pub") - cwdarr = append(cwdarr, strings.Split(c.Request.URL.Path, "/")...) - c.File(path.Join(cwdarr...)) + c.FileFromFS(c.Request.URL.Path, *fs) default: - cwdarr := []string{"/"} - if runtime.GOOS == "windows" { - cwdarr[0] = "" - } - cwdarr = append(cwdarr, strings.Split(runner.Cwd, "/")...) - cwdarr = append(cwdarr, "pub") - cwdarr = append(cwdarr, "index.html") - c.File(path.Join(cwdarr...)) + c.FileFromFS("/", *fs) } }) } func (rt *Router) Config(r *gin.Engine) { + r.Use(stat()) r.Use(languageDetector(rt.Center.I18NHeaderKey)) r.Use(aop.Recovery()) + statikFS, err := fs.New() + if err != nil { + logger.Errorf("cannot create statik fs: %v", err) + } + r.StaticFS("/pub", statikFS) + pagesPrefix := "/api/n9e" pages := r.Group(pagesPrefix) { @@ -403,7 +397,8 @@ func (rt *Router) Config(r *gin.Engine) { } } - rt.configNoRoute(r) + rt.configNoRoute(r, &statikFS) + } func Render(c *gin.Context, data, msg interface{}) { diff --git a/docker/Dockerfile b/docker/Dockerfile index 332a315e..aa29282d 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,8 +4,7 @@ FROM python:3-slim WORKDIR /app ADD n9e /app ADD http://download.flashcat.cloud/wait /wait -RUN mkdir -p /app/pub && chmod +x /wait -ADD pub /app/pub/ +RUN chmod +x /wait RUN chmod +x n9e EXPOSE 17000 diff --git a/docker/Dockerfile.goreleaser b/docker/Dockerfile.goreleaser index 71de12c5..acce5900 100644 --- a/docker/Dockerfile.goreleaser +++ b/docker/Dockerfile.goreleaser @@ -7,7 +7,6 @@ ADD etc /app/ ADD integrations /app/integrations/ ADD --chmod=755 https://github.com/ufoscout/docker-compose-wait/releases/download/2.11.0/wait_x86_64 /wait RUN chmod +x /wait -ADD pub /app/pub/ EXPOSE 17000 diff --git a/docker/Dockerfile.goreleaser.arm64 b/docker/Dockerfile.goreleaser.arm64 index af892dd9..06d1e8ce 100644 --- a/docker/Dockerfile.goreleaser.arm64 +++ b/docker/Dockerfile.goreleaser.arm64 @@ -6,7 +6,6 @@ WORKDIR /app ADD n9e /app/ ADD etc /app/ ADD integrations /app/integrations/ -ADD pub /app/pub/ COPY --chmod=755 --from=toolbox /toolbox/wait_aarch64 /wait EXPOSE 17000 diff --git a/docker/build.sh b/docker/build.sh index ddc9f3e7..d261086c 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -10,7 +10,6 @@ echo "tag: ${tag}" rm -rf n9e pub cp ../n9e . -cp -r ../pub . docker build -t nightingale:${tag} . diff --git a/fe.sh b/fe.sh index 22eb3dce..c73f2eda 100755 --- a/fe.sh +++ b/fe.sh @@ -8,3 +8,10 @@ curl -o n9e-fe-${VERSION}.tar.gz -L https://github.com/n9e/fe/releases/download/ tar zxvf n9e-fe-${VERSION}.tar.gz cp ./docker/initsql/a-n9e.sql n9e.sql + +# Embed files into a Go executable +statik -src=./pub -dest=./front + +# rm the fe file +rm n9e-fe-${VERSION}.tar.gz +rm -r ./pub \ No newline at end of file diff --git a/front/statik/statik.go b/front/statik/statik.go new file mode 100644 index 00000000..c01359f3 --- /dev/null +++ b/front/statik/statik.go @@ -0,0 +1,14 @@ +// Code generated by statik. DO NOT EDIT. + +package statik + +import ( + "github.com/rakyll/statik/fs" +) + + +func init() { + data := "PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + fs.Register(data) + } + \ No newline at end of file diff --git a/go.mod b/go.mod index cb97c268..3986dfc4 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/prometheus/client_golang v1.14.0 github.com/prometheus/common v0.39.0 github.com/prometheus/prometheus v2.5.0+incompatible + github.com/rakyll/statik v0.1.7 github.com/redis/go-redis/v9 v9.0.2 github.com/tidwall/gjson v1.14.0 github.com/toolkits/pkg v1.3.3 diff --git a/go.sum b/go.sum index 5c68dc32..c29f141d 100644 --- a/go.sum +++ b/go.sum @@ -232,6 +232,8 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5 github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/prometheus/prometheus v2.5.0+incompatible h1:7QPitgO2kOFG8ecuRn9O/4L9+10He72rVRJvMXrE9Hg= github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s= +github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ= +github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc= github.com/redis/go-redis/v9 v9.0.2 h1:BA426Zqe/7r56kCcvxYLWe1mkaz71LKF77GwgFzSxfE= github.com/redis/go-redis/v9 v9.0.2/go.mod h1:/xDTe9EF1LM61hek62Poq2nzQSGj0xSrEtEHbBQevps= github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62/go.mod h1:65XQgovT59RWatovFwnwocoUxiI/eENTnOY5GK3STuY=