Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集成 Dashboard 前端代码到 pixiu #552

Merged
merged 10 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions Dockerfile-aio
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 node:16.18.0-alpine as builder
WORKDIR /build
RUN git clone https://github.com/pixiu-io/dashboard.git
RUN cd dashboard && npm install && npm run build

FROM jacky06/static:nonroot
LABEL MAINTAINER="caoyingjunz"

COPY --from=pixiu-builder /app/pixiu /app
COPY --from=builder /build/dashboard/dist /static
CMD ["/app"]
6 changes: 6 additions & 0 deletions api/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -54,6 +55,11 @@ func InstallRouters(o *options.Options) {

install(o, fs...)

// 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") })
// 启动 APIs 服务
Expand Down
2 changes: 2 additions & 0 deletions cmd/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ default:
# 日志的格式,可选 text 和 json
log_format: json
log_level: info
# 静态文件路径
static_files: ./static

# 数据库地址信息
mysql:
Expand Down
65 changes: 65 additions & 0 deletions pkg/static/localfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 (
"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
}
56 changes: 56 additions & 0 deletions pkg/static/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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 (
"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()
}
}
}
Loading