Skip to content

Commit

Permalink
支持前后端代码合并 (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jan 15, 2025
1 parent 6dd3834 commit 5633186
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 72 deletions.
4 changes: 3 additions & 1 deletion api/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func InstallRouters(o *options.Options) {

install(o, fs...)

o.HttpEngine.Use(static.ServeEmbed("static", EmbedFS))
// StaticFiles 启用前端集成
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 @@ -51,6 +51,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
4 changes: 4 additions & 0 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
defaultConfigFile = "/etc/pixiu/config.yaml"
defaultLogFormat = logutil.LogFormatJson
defaultWorkDir = "/etc/pixiu"
defaultStaticDir = "/static"

defaultSlowSQLDuration = 1 * time.Second

Expand Down Expand Up @@ -114,6 +115,9 @@ func (o *Options) Complete() error {
if o.ComponentConfig.Worker.WorkDir == "" {
o.ComponentConfig.Worker.WorkDir = defaultWorkDir
}
if len(o.ComponentConfig.Default.StaticFiles) == 0 {
o.ComponentConfig.Default.StaticFiles = defaultStaticDir
}
if o.ComponentConfig.Audit.Schedule == "" {
o.ComponentConfig.Audit.Schedule = jobmanager.DefaultSchedule
}
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

#tls:
# cert_file: test.pem
Expand Down
6 changes: 3 additions & 3 deletions install.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ worker:
- rocky9.3
- openEuler22.03

# 前端配置(ip 根据实际情况调整,如果是虚拟机,则配置成虚拟机的公网IP,安全组放通80和8090端口)
# 前端配置(ip 根据实际情况调整,如果是虚拟机,则配置成虚拟机的公网IP,安全组放通80(http)或者443(https)端口)
vim /etc/pixiu/config.json
{
"url": "http://192.168.16.156",
"watchUrl": "http://192.168.16.156:8090"
"watchUrl": "http://192.168.16.156"
}
```
# 启动 pixiu
```bash
docker run -d --net host --restart=always --privileged=true -v /etc/pixiu:/etc/pixiu -v /var/run/docker.sock:/var/run/docker.sock --name pixiu-aio ccr.ccs.tencentyun.com/pixiucloud/pixiu-aio
docker run -d --net host --restart=always --privileged=true -v /etc/pixiu:/etc/pixiu -v /var/run/docker.sock:/var/run/docker.sock --name pixiu ccr.ccs.tencentyun.com/pixiucloud/pixiu
登录效果
浏览器登陆: http://192.168.16.156
```
Expand Down
67 changes: 0 additions & 67 deletions pkg/static/embedfile.go

This file was deleted.

61 changes: 61 additions & 0 deletions pkg/static/localfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2025 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 (
"github.com/gin-gonic/gin"
"net/http"
"os"
"path"
"strings"
)

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
}
6 changes: 5 additions & 1 deletion pkg/static/servefilesystem.go → pkg/static/static.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 The Pixiu Authors.
Copyright 2025 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.
Expand Down Expand Up @@ -28,6 +28,10 @@ type ServeFileSystem interface {
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)
Expand Down

0 comments on commit 5633186

Please sign in to comment.