diff --git a/service/middleware.go b/service/middleware.go index 052443fa..47254b63 100644 --- a/service/middleware.go +++ b/service/middleware.go @@ -2,15 +2,25 @@ package service import ( "fmt" + "net/http" "strings" "github.com/gin-gonic/gin" + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/data" "github.com/yaoapp/xiang/share" ) +// 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{ BindDomain, + BinStatic, } // BindDomain 绑定许可域名 @@ -29,3 +39,23 @@ func BindDomain(c *gin.Context) { }) c.Abort() } + +// BinStatic 静态文件服务 +func BinStatic(c *gin.Context) { + + length := len(c.Request.URL.Path) + + if length >= 5 && c.Request.URL.Path[0:5] == "/api/" { // API接口 + c.Next() + return + } else if length >= 7 && c.Request.URL.Path[0:7] == "/xiang/" { // 数据管理后台 + c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, "/xiang") + AdminFileServer.ServeHTTP(c.Writer, c.Request) + c.Abort() + return + } + + // 应用内静态文件目录(/ui) + AppFileServer.ServeHTTP(c.Writer, c.Request) + c.Abort() +} diff --git a/service/service.go b/service/service.go index 9f3335f1..95f07cce 100644 --- a/service/service.go +++ b/service/service.go @@ -1,29 +1,17 @@ package service import ( - "net/http" - "strings" - - "github.com/gin-gonic/gin" "github.com/yaoapp/gou" "github.com/yaoapp/xiang/config" - "github.com/yaoapp/xiang/data" ) var shutdown = make(chan bool) var shutdownComplete = make(chan bool) -// AdminFileServer 数据管理平台 -var AdminFileServer http.Handler = http.FileServer(data.AssetFS()) - -// AppFileServer 应用静态文件 -var AppFileServer http.Handler = http.FileServer(http.Dir(config.Conf.RootUI)) - // Start 启动服务 func Start() { gou.SetHTTPGuards(Guards) - gou.ServeHTTPCustomRouter( - router(), + gou.ServeHTTP( gou.Server{ Host: config.Conf.Service.Host, Port: config.Conf.Service.Port, @@ -42,22 +30,3 @@ func Stop(onComplete func()) { <-shutdownComplete onComplete() } - -// router 返回路由 -func router() *gin.Engine { - gin.SetMode(gin.ReleaseMode) - router := gin.Default() - - // 应用 UI 目录应用静态文件 - router.Any("/", func(c *gin.Context) { - AppFileServer.ServeHTTP(c.Writer, c.Request) - }) - - // 数据管理后台 - router.Any("/xiang/*path", func(c *gin.Context) { - c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, "/xiang") - AdminFileServer.ServeHTTP(c.Writer, c.Request) - }) - - return router -} diff --git a/share/const.go b/share/const.go index 9dbd745c..1c7ea42a 100644 --- a/share/const.go +++ b/share/const.go @@ -5,7 +5,7 @@ import ( ) // VERSION 版本号 -const VERSION = "0.8.13" +const VERSION = "0.8.14" // DOMAIN 许可域 const DOMAIN = "*.iqka.com"