Skip to content

Commit

Permalink
v0.8.11 支持前台静态文件服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 20, 2021
1 parent 5db2307 commit 2253a98
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 42 deletions.
29 changes: 25 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package app

import (
"log"
"io/ioutil"
"os"
"path/filepath"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/exception"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/yaoapp/xiang/data"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/xfs"
"github.com/yaoapp/xiang/xlog"
)

// Load 加载应用信息
Expand All @@ -21,24 +23,43 @@ func Load(cfg config.Config) {

// Init 应用初始化
func Init(cfg config.Config) {

// UI文件目录
if _, err := os.Stat(cfg.RootUI); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootUI, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootUI, err)
xlog.Printf("创建目录失败(%s) %s", cfg.RootUI, err)
os.Exit(1)
}

content, err := data.Asset("xiang/data/index.html")
if err != nil {
xlog.Printf("读取文件失败(%s) %s", cfg.RootUI, err)
os.Exit(1)
}

err = ioutil.WriteFile(filepath.Join(cfg.RootUI, "/index.html"), content, os.ModePerm)
if err != nil {
xlog.Printf("复制默认文件失败(%s) %s", cfg.RootUI, err)
os.Exit(1)
}
}

// 数据库目录
if _, err := os.Stat(cfg.RootDB); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootDB, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootDB, err)
xlog.Printf("创建目录失败(%s) %s", cfg.RootDB, err)
os.Exit(1)
}
}

// 文件数据目录
if _, err := os.Stat(cfg.RootData); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootData, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootData, err)
xlog.Printf("创建目录失败(%s) %s", cfg.RootData, err)
os.Exit(1)
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type XiangConfig struct {
RootData string `json:"root_data,omitempty" env:"XIANG_ROOT_DATA"` // 应用数据文件目录
RootAPI string `json:"root_api,omitempty" env:"XIANG_ROOT_API"` // 应用API文件目录
RootModel string `json:"root_model,omitempty" env:"XIANG_ROOT_MODEL"` // 应用模型文件目录
RootFLow string `json:"root_flow,omitempty" env:"XIANG_ROOT_FLOW"` // 应用工作流文件目录
RootPlugin string `json:"root_plugin,omitempty" env:"XIANG_ROOT_PLUGIN"` // 应用插件文件目录
RootTable string `json:"root_table,omitempty" env:"XIANG_ROOT_TABLE"` // 应用表格文件目录
RootChart string `json:"root_chart,omitempty" env:"XIANG_ROOT_CHART"` // 应用图表文件目录
RootKanban string `json:"root_kanban,omitempty" env:"XIANG_ROOT_KANBAN"` // 应用看板文件目录
RootFLow string `json:"root_flow,omitempty" env:"XIANG_ROOT_FLOW"` // 应用业务逻辑文件目录
RootPlugin string `json:"root_plugin,omitempty" env:"XIANG_ROOT_PLUGIN"` // 应用业务插件文件目录
RootTable string `json:"root_table,omitempty" env:"XIANG_ROOT_TABLE"` // 应用数据表格文件目录
RootChart string `json:"root_chart,omitempty" env:"XIANG_ROOT_CHART"` // 应用分析图表文件目录
RootPage string `json:"root_page,omitempty" env:"XIANG_ROOT_PAGE"` // 应用通用页面文件目录
RootScreen string `json:"root_screen,omitempty" env:"XIANG_ROOT_SCREEN"` // 应用大屏文件目录
}

Expand Down Expand Up @@ -151,8 +151,9 @@ func (cfg *Config) SetDefaults() {
if cfg.RootChart == "" {
cfg.RootChart = cfg.Root + "/charts"
}
if cfg.RootScreen == "" {
cfg.RootScreen = cfg.Root + "/screens"

if cfg.RootPage == "" {
cfg.RootPage = cfg.Root + "/pages"
}

if cfg.RootData == "" {
Expand Down Expand Up @@ -228,7 +229,7 @@ func SetAppPath(root string, envfile ...string) {
Conf.RootPlugin = filepath.Join(fullpath, "/plugins")
Conf.RootTable = filepath.Join(fullpath, "/tables")
Conf.RootChart = filepath.Join(fullpath, "/charts")
Conf.RootScreen = filepath.Join(root, "/screens")
Conf.RootPage = filepath.Join(fullpath, "/pages")
Conf.RootData = filepath.Join(fullpath, "/data")
Conf.RootUI = filepath.Join(fullpath, "/ui")
Conf.RootDB = filepath.Join(fullpath, "/db")
Expand Down
69 changes: 46 additions & 23 deletions data/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/data"
"github.com/yaoapp/xiang/share"
)

// FileServer 静态服务
var FileServer http.Handler = http.FileServer(data.AssetFS())
// AdminFileServer 数据管理平台
var AdminFileServer http.Handler = http.FileServer(data.AssetFS())

// AppFileServer 应用管理平台
var AppFileServer http.Handler = http.FileServer(http.Dir(config.Conf.RootUI))

// Middlewares 服务中间件
var Middlewares = []gin.HandlerFunc{
Expand All @@ -38,12 +42,18 @@ func BindDomain(c *gin.Context) {

// BinStatic 静态文件服务
func BinStatic(c *gin.Context) {
if len(c.Request.URL.Path) >= 5 && c.Request.URL.Path[0:5] == "/api/" {

if len(c.Request.URL.Path) >= 5 && c.Request.URL.Path[0:5] == "/api/" { // API接口
c.Next()
return

} else if len(c.Request.URL.Path) >= 7 && c.Request.URL.Path[0:7] == "/xiang/" { // 数据管理后台
AdminFileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
return
}

// 静态文件请求
FileServer.ServeHTTP(c.Writer, c.Request)
// 应用静态文件请求
AppFileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
Loading

0 comments on commit 2253a98

Please sign in to comment.