pet-house/core/server/web/web_iris/router.go

103 lines
2.8 KiB
Go

package web_iris
import (
"github.com/kataras/iris/v12"
"net/http"
"strings"
"time"
"github.com/kataras/iris/v12/middleware/pprof"
"github.com/kataras/iris/v12/middleware/rate"
"github.com/kataras/iris/v12/middleware/recover"
"pet-house.com/core/helper/arr"
"pet-house.com/core/server/web"
"pet-house.com/core/server/web/web_iris/middleware"
)
// InitRouter
func (ws *WebServer) InitRouter() error {
app := ws.app.Party("/").AllowMethods(iris.MethodOptions)
{
app.Get("/v0/version", func(ctx iris.Context) {
ctx.WriteString("IRIS-ADMIN is running!!!")
})
app.UseRouter(middleware.CrsAuth())
app.UseRouter(recover.New())
if !web.CONFIG.Limit.Disable {
limitV1 := rate.Limit(web.CONFIG.Limit.Limit, web.CONFIG.Limit.Burst, rate.PurgeEvery(time.Minute, 5*time.Minute))
app.Use(limitV1)
}
if web.CONFIG.System.Level == "debug" {
debug := func(index iris.Party) {
index.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1>请点击<a href='/debug/pprof'>这里</a>打开调试页面")
})
index.Any("/pprof", pprof.New())
index.Any("/pprof/{action:path}", pprof.New())
}
app.PartyFunc("/debug", debug)
}
for _, party := range ws.parties {
app.PartyFunc(party.Prefix, party.PartyFunc)
}
}
// http test must build
if err := ws.app.Build(); err != nil {
return err
}
return nil
}
// GetSources
// - PermRoutes
// - NoPermRoutes
func (ws *WebServer) GetSources() ([]map[string]string, []map[string]string) {
methodExcepts := strings.Split(web.CONFIG.Except.Method, ";")
uris := strings.Split(web.CONFIG.Except.Uri, ";")
methodMenus := strings.Split(web.CONFIG.Menu.Method, ";")
uriMenus := strings.Split(web.CONFIG.Menu.Uri, ";")
routeLen := len(ws.app.GetRoutes())
permRoutes := make([]map[string]string, 0, routeLen)
noPermRoutes := make([]map[string]string, 0, routeLen)
for _, r := range ws.app.GetRoutes() {
route := map[string]string{
"path": r.Path,
"name": r.Name,
"act": r.Method,
}
if len(methodMenus) > 0 && len(uriMenus) > 0 && len(methodMenus) == len(uriMenus) {
for i := 0; i < len(methodMenus); i++ {
if strings.EqualFold(r.Method, strings.ToLower(methodMenus[i])) && strings.EqualFold(r.Path, strings.ToLower(uriMenus[i])) {
route["is_menu"] = "1"
}
}
}
httpStatusType := arr.NewCheckArrayType(4)
httpStatusType.AddMutil(http.MethodGet, http.MethodPost)
if !httpStatusType.Check(r.Method) {
noPermRoutes = append(noPermRoutes, route)
continue
}
if len(methodExcepts) > 0 && len(uris) > 0 && len(methodExcepts) == len(uris) {
for i := 0; i < len(methodExcepts); i++ {
if strings.EqualFold(r.Method, strings.ToLower(methodExcepts[i])) && strings.EqualFold(r.Path, strings.ToLower(uris[i])) {
noPermRoutes = append(noPermRoutes, route)
continue
}
}
}
permRoutes = append(permRoutes, route)
}
return permRoutes, noPermRoutes
}