fix router bug -> v0.8.14

This commit is contained in:
Max 2021-10-21 09:25:01 +08:00
parent 7e240b913b
commit d053596a9b
3 changed files with 32 additions and 33 deletions

View file

@ -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()
}

View file

@ -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
}

View file

@ -5,7 +5,7 @@ import (
)
// VERSION 版本号
const VERSION = "0.8.13"
const VERSION = "0.8.14"
// DOMAIN 许可域
const DOMAIN = "*.iqka.com"