From b4cff3c13b532c605948d1af163a808fb223ede9 Mon Sep 17 00:00:00 2001 From: xiamingyu Date: Wed, 11 Dec 2024 13:48:48 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=90=8E=E7=AB=AF=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile-aio | 21 ++++++++++++++++ Makefile | 3 +++ api/server/router/router.go | 3 ++- pkg/static/localfile.go | 49 +++++++++++++++++++++++++++++++++++++ pkg/static/static.go | 40 ++++++++++++++++++++++++++++++ static/_balnk | 0 6 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Dockerfile-aio create mode 100644 pkg/static/localfile.go create mode 100644 pkg/static/static.go create mode 100644 static/_balnk diff --git a/Dockerfile-aio b/Dockerfile-aio new file mode 100644 index 00000000..6e8e9f00 --- /dev/null +++ b/Dockerfile-aio @@ -0,0 +1,21 @@ +FROM golang:1.17 as pixiu-builder +WORKDIR /app +ARG VERSION +ENV GOPROXY=https://goproxy.cn +COPY . . +RUN CGO_ENABLED=0 go build -ldflags "-s -w -X 'main.version=${VERSION}'" -o pixiu ./cmd + +FROM timbru31/node-alpine-git:18 as builder +LABEL MAINTAINER="PIXIU" +WORKDIR /build +RUN git clone https://github.com/pixiu-io/dashboard.git +RUN cd dashboard && yarn add npm && npm i && npm run build + + +FROM nginx:alpine +LABEL MAINTAINER="PIXIU" + +COPY --from=pixiu-builder /app/pixiu /app +COPY --from=builder /build/dashboard/dist /static + +CMD ["/app"] \ No newline at end of file diff --git a/Makefile b/Makefile index 3cf8b4f7..c722663f 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ build: image: docker build -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) . +image-aio: + DOCKER_BUILDKIT=1 docker buildx build --platform linux/amd64 -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) -f Dockerfile-aio . + push: image docker push $(dockerhubUser)/$(releaseName):$(tag) diff --git a/api/server/router/router.go b/api/server/router/router.go index 20a5c20d..4b01d6f1 100644 --- a/api/server/router/router.go +++ b/api/server/router/router.go @@ -36,6 +36,7 @@ import ( "github.com/caoyingjunz/pixiu/api/server/router/tenant" "github.com/caoyingjunz/pixiu/api/server/router/user" "github.com/caoyingjunz/pixiu/cmd/app/options" + "github.com/caoyingjunz/pixiu/pkg/static" ) type RegisterFunc func(o *options.Options) @@ -53,7 +54,7 @@ func InstallRouters(o *options.Options) { } install(o, fs...) - + o.HttpEngine.Use(static.Serve("/", static.LocalFile("./static", true))) // 启动健康检查 o.HttpEngine.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") }) // 启动 APIs 服务 diff --git a/pkg/static/localfile.go b/pkg/static/localfile.go new file mode 100644 index 00000000..2fd2ee88 --- /dev/null +++ b/pkg/static/localfile.go @@ -0,0 +1,49 @@ +package static + +import ( + "net/http" + "os" + "path" + "strings" + + "github.com/gin-gonic/gin" +) + +type localFileSystem struct { + http.FileSystem + root string + indexes bool +} + +func LocalFile(root string, indexes bool) *localFileSystem { + return &localFileSystem{ + FileSystem: gin.Dir(root, indexes), + root: root, + indexes: indexes, + } +} + +func (l *localFileSystem) Exists(prefix string, file string) bool { + if p := strings.TrimPrefix(file, prefix); len(p) < len(file) { + name := path.Join(l.root, path.Clean(p)) + + if strings.Contains(name, "\\") || strings.Contains(name, "..") { + return false + } + + stats, err := os.Stat(name) + if err != nil { + return false + } + if stats.IsDir() { + if !l.indexes { + _, err := os.Stat(path.Join(name, "index.html")) + if err != nil { + return false + } + } + } + return true + } + return false +} diff --git a/pkg/static/static.go b/pkg/static/static.go new file mode 100644 index 00000000..fd387e0f --- /dev/null +++ b/pkg/static/static.go @@ -0,0 +1,40 @@ +package static + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +type ServeFileSystem interface { + http.FileSystem + Exists(prefix string, path string) bool +} + +func ServeRoot(urlPrefix, root string) gin.HandlerFunc { + return Serve(urlPrefix, LocalFile(root, false)) +} + +// Serve returns a middleware handler that serves static files in the given directory. +func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { + return ServeCached(urlPrefix, fs, 0) +} + +// ServeCached returns a middleware handler that similar as Serve +// but with the Cache-Control Header set as passed in the cacheAge parameter +func ServeCached(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc { + fileserver := http.FileServer(fs) + if urlPrefix != "" { + fileserver = http.StripPrefix(urlPrefix, fileserver) + } + return func(c *gin.Context) { + if fs.Exists(urlPrefix, c.Request.URL.Path) { + if cacheAge != 0 { + c.Writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d", cacheAge)) + } + fileserver.ServeHTTP(c.Writer, c.Request) + c.Abort() + } + } +} diff --git a/static/_balnk b/static/_balnk new file mode 100644 index 00000000..e69de29b From 70b7be471fcbb3e0fc74e78ad6cc36118481321d Mon Sep 17 00:00:00 2001 From: xiamingyu Date: Thu, 12 Dec 2024 09:28:06 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E4=BB=8E=E9=85=8D=E7=BD=AE=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=9D=99=E6=80=81=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/router/router.go | 4 +++- cmd/app/config/config.go | 2 ++ config.yaml | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/api/server/router/router.go b/api/server/router/router.go index 4b01d6f1..043fa43a 100644 --- a/api/server/router/router.go +++ b/api/server/router/router.go @@ -54,7 +54,9 @@ func InstallRouters(o *options.Options) { } install(o, fs...) - o.HttpEngine.Use(static.Serve("/", static.LocalFile("./static", true))) + o.HttpEngine.Use(static.Serve("/", static.LocalFile(o.ComponentConfig.Default.StaticFiles, + true))) + // 启动健康检查 o.HttpEngine.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") }) // 启动 APIs 服务 diff --git a/cmd/app/config/config.go b/cmd/app/config/config.go index e9c6d00a..4d7842a7 100644 --- a/cmd/app/config/config.go +++ b/cmd/app/config/config.go @@ -48,6 +48,8 @@ type DefaultOptions struct { AutoMigrate bool `yaml:"auto_migrate"` logutil.LogOptions `yaml:",inline"` + + StaticFiles string `yaml:"static_files"` } func (o DefaultOptions) Valid() error { diff --git a/config.yaml b/config.yaml index ab9aacc5..8b6ebfc6 100644 --- a/config.yaml +++ b/config.yaml @@ -10,6 +10,8 @@ default: # 日志的格式,可选 text 和 json log_format: json log_level: info + # 静态文件路径 + static_files: ./static # 数据库地址信息 mysql: From 914480b6cda0f04d434dcb196daf0e548cd9c26b Mon Sep 17 00:00:00 2001 From: xiamingyu Date: Thu, 12 Dec 2024 11:07:55 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/_balnk | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 static/_balnk diff --git a/static/_balnk b/static/_balnk deleted file mode 100644 index e69de29b..00000000 From d2c82ab47c403a64d2dfaa3dda61bdf3537648a8 Mon Sep 17 00:00:00 2001 From: xiamingyu Date: Thu, 12 Dec 2024 11:19:51 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9D=99=E6=80=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E5=8A=A0=E8=BD=BD=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/router/router.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/server/router/router.go b/api/server/router/router.go index 043fa43a..3aacb3ef 100644 --- a/api/server/router/router.go +++ b/api/server/router/router.go @@ -54,6 +54,8 @@ func InstallRouters(o *options.Options) { } install(o, fs...) + // StaticFiles 目录为空不影响 api 调用 + // 运行过程中 StaticFiles 添加上了,前端页面自动生效 o.HttpEngine.Use(static.Serve("/", static.LocalFile(o.ComponentConfig.Default.StaticFiles, true))) From 13b8c02611bca263eec4845fa2b1f4fa99e181c8 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:04:02 +0800 Subject: [PATCH 05/10] add --- Dockerfile-aio | 9 +++------ api/server/router/router.go | 9 +++++---- pkg/static/localfile.go | 16 ++++++++++++++++ pkg/static/static.go | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Dockerfile-aio b/Dockerfile-aio index 6e8e9f00..337a142a 100644 --- a/Dockerfile-aio +++ b/Dockerfile-aio @@ -5,17 +5,14 @@ ENV GOPROXY=https://goproxy.cn COPY . . RUN CGO_ENABLED=0 go build -ldflags "-s -w -X 'main.version=${VERSION}'" -o pixiu ./cmd -FROM timbru31/node-alpine-git:18 as builder -LABEL MAINTAINER="PIXIU" +FROM node:16.18.0-alpine as builder WORKDIR /build RUN git clone https://github.com/pixiu-io/dashboard.git -RUN cd dashboard && yarn add npm && npm i && npm run build - +RUN cd dashboard && npm install && npm run build FROM nginx:alpine LABEL MAINTAINER="PIXIU" COPY --from=pixiu-builder /app/pixiu /app COPY --from=builder /build/dashboard/dist /static - -CMD ["/app"] \ No newline at end of file +CMD ["/app"] diff --git a/api/server/router/router.go b/api/server/router/router.go index 3aacb3ef..e16cde63 100644 --- a/api/server/router/router.go +++ b/api/server/router/router.go @@ -54,10 +54,11 @@ func InstallRouters(o *options.Options) { } install(o, fs...) - // StaticFiles 目录为空不影响 api 调用 - // 运行过程中 StaticFiles 添加上了,前端页面自动生效 - o.HttpEngine.Use(static.Serve("/", static.LocalFile(o.ComponentConfig.Default.StaticFiles, - true))) + + // StaticFiles 目录不为空时,启用前端集成 + if len(o.ComponentConfig.Default.StaticFiles) != 0 { + o.HttpEngine.Use(static.Serve("/", static.LocalFile(o.ComponentConfig.Default.StaticFiles, true))) + } // 启动健康检查 o.HttpEngine.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") }) diff --git a/pkg/static/localfile.go b/pkg/static/localfile.go index 2fd2ee88..d470edd8 100644 --- a/pkg/static/localfile.go +++ b/pkg/static/localfile.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package static import ( diff --git a/pkg/static/static.go b/pkg/static/static.go index fd387e0f..eb0fa8be 100644 --- a/pkg/static/static.go +++ b/pkg/static/static.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package static import ( From 6dabaa6dd1f0e1d0235b92c23e941518d819b6d1 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:08:44 +0800 Subject: [PATCH 06/10] add --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c722663f..64f0b90f 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,14 @@ image: docker build -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) . image-aio: - DOCKER_BUILDKIT=1 docker buildx build --platform linux/amd64 -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) -f Dockerfile-aio . + docker build -t $(dockerhubUser)/pixiu-aio:v3.0.1 --build-arg VERSION=$(version) -f Dockerfile-aio . push: image docker push $(dockerhubUser)/$(releaseName):$(tag) +push-aio: image-aio + docker push $(dockerhubUser)/pixiu-aio:v3.0.1 + webshell-image: docker build --build-arg K8S_VERSION=$(k8sVersion) \ --build-arg HELM_VERSION=$(helmVersion) \ From cc05fd655fd33c21b9dff3d9ba5406cc7bd6eae2 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:14:54 +0800 Subject: [PATCH 07/10] add --- Dockerfile-aio | 4 ++-- Makefile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile-aio b/Dockerfile-aio index 337a142a..bea74f2e 100644 --- a/Dockerfile-aio +++ b/Dockerfile-aio @@ -10,8 +10,8 @@ WORKDIR /build RUN git clone https://github.com/pixiu-io/dashboard.git RUN cd dashboard && npm install && npm run build -FROM nginx:alpine -LABEL MAINTAINER="PIXIU" +FROM jacky06/static:nonroot +LABEL MAINTAINER="caoyingjunz" COPY --from=pixiu-builder /app/pixiu /app COPY --from=builder /build/dashboard/dist /static diff --git a/Makefile b/Makefile index 64f0b90f..d7d65699 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ push: image docker push $(dockerhubUser)/$(releaseName):$(tag) push-aio: image-aio - docker push $(dockerhubUser)/pixiu-aio:v3.0.1 + docker push $(dockerhubUser)/pixiu-aio:v1.1.0 webshell-image: docker build --build-arg K8S_VERSION=$(k8sVersion) \ From cd51995c187d5ca70f971ebc2950f48a94f2407b Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:18:37 +0800 Subject: [PATCH 08/10] add --- .github/workflows/docker-image.yml | 12 ++++++++++++ Makefile | 6 ------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 198e336e..4e8603de 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -46,3 +46,15 @@ jobs: harbor.cloud.pixiuio.com/${{ secrets.HARBOR_PROJECT }}/pixiu:latest harbor.cloud.pixiuio.com/${{ secrets.HARBOR_PROJECT }}/pixiu:v0.1 harbor.cloud.pixiuio.com/${{ secrets.HARBOR_PROJECT }}/pixiu:${{ env.COMMIT_HASH }} + + - name: Build and push the pixiu aio image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile-aio + platforms: linux/amd64,linux/arm64 + build-args: | + VERSION=${{ env.COMMIT_HASH }}-${{ env.TIMESTAMP }} + push: true + tags: | + harbor.cloud.pixiuio.com/${{ secrets.HARBOR_PROJECT }}/pixiu-aio:v1.1.0 diff --git a/Makefile b/Makefile index d7d65699..3cf8b4f7 100644 --- a/Makefile +++ b/Makefile @@ -21,15 +21,9 @@ build: image: docker build -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) . -image-aio: - docker build -t $(dockerhubUser)/pixiu-aio:v3.0.1 --build-arg VERSION=$(version) -f Dockerfile-aio . - push: image docker push $(dockerhubUser)/$(releaseName):$(tag) -push-aio: image-aio - docker push $(dockerhubUser)/pixiu-aio:v1.1.0 - webshell-image: docker build --build-arg K8S_VERSION=$(k8sVersion) \ --build-arg HELM_VERSION=$(helmVersion) \ From bb611c04654ec7ef3c5915ae8aec9f6ef71b3d59 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:22:25 +0800 Subject: [PATCH 09/10] add --- .github/workflows/docker-image.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 4e8603de..0b53ee2b 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -7,6 +7,10 @@ on: paths-ignore: - 'docs/**' + pull_request: + branches: + - master + jobs: build: runs-on: ubuntu-latest From dfc356b9ddaa277d8eeb83f5593ce7e236568e34 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Fri, 13 Dec 2024 00:26:19 +0800 Subject: [PATCH 10/10] add --- .github/workflows/docker-image.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 0b53ee2b..4e8603de 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -7,10 +7,6 @@ on: paths-ignore: - 'docs/**' - pull_request: - branches: - - master - jobs: build: runs-on: ubuntu-latest