Browse Source

feat: embed front-end files into n9e executable (#1556)

* feat: embed front-end files into n9e executable
tags/v6.0.0-ga12
xtan GitHub 3 years ago
parent
commit
3fd4ea4853
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 52 additions and 31 deletions
  1. +1
    -0
      .goreleaser.yaml
  2. +10
    -5
      Makefile
  3. +16
    -21
      center/router/router.go
  4. +1
    -2
      docker/Dockerfile
  5. +0
    -1
      docker/Dockerfile.goreleaser
  6. +0
    -1
      docker/Dockerfile.goreleaser.arm64
  7. +0
    -1
      docker/build.sh
  8. +7
    -0
      fe.sh
  9. +14
    -0
      front/statik/statik.go
  10. +1
    -0
      go.mod
  11. +2
    -0
      go.sum

+ 1
- 0
.goreleaser.yaml View File

@@ -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 }}'


+ 10
- 5
Makefile View File

@@ -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:


+ 16
- 21
center/router/router.go View File

@@ -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{}) {


+ 1
- 2
docker/Dockerfile View File

@@ -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


+ 0
- 1
docker/Dockerfile.goreleaser View File

@@ -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



+ 0
- 1
docker/Dockerfile.goreleaser.arm64 View File

@@ -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


+ 0
- 1
docker/build.sh View File

@@ -10,7 +10,6 @@ echo "tag: ${tag}"

rm -rf n9e pub
cp ../n9e .
cp -r ../pub .

docker build -t nightingale:${tag} .



+ 7
- 0
fe.sh View File

@@ -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

+ 14
- 0
front/statik/statik.go View File

@@ -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)
}

+ 1
- 0
go.mod View File

@@ -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


+ 2
- 0
go.sum View File

@@ -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=


Loading…
Cancel
Save