247 lines
7.4 KiB
Go
247 lines
7.4 KiB
Go
package router
|
|
|
|
import (
|
|
"ArmedPolice/app/api"
|
|
"ArmedPolice/config"
|
|
"ArmedPolice/router/rate"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Router struct {
|
|
*Option
|
|
handler *gin.Engine
|
|
}
|
|
|
|
type (
|
|
Option struct {
|
|
Mode string
|
|
IsCors bool
|
|
*RateLimitConfig
|
|
}
|
|
// RateLimitConfig 限流配置
|
|
RateLimitConfig struct {
|
|
IsRate bool `json:"is_rate"`
|
|
Limit int `json:"limit"`
|
|
Capacity int `json:"capacity"`
|
|
}
|
|
)
|
|
|
|
func (this *Router) registerWeb() {
|
|
this.handler.LoadHTMLGlob("dist/*.html") // 添加入口index.html
|
|
this.handler.LoadHTMLFiles("dist/*") // 添加资源路径
|
|
this.handler.Static("/js", "dist/js")
|
|
this.handler.Static("/css", "dist/css")
|
|
this.handler.Static("/assets", "dist/assets")
|
|
this.handler.StaticFile("/", "dist/index.html") //前端接口
|
|
}
|
|
|
|
func (this *Router) registerAPI() {
|
|
apiPrefix := "/api"
|
|
g := this.handler.Group(apiPrefix)
|
|
g.Use()
|
|
// 登录验证
|
|
g.Use(NeedLogin(AddSkipperURL([]string{
|
|
apiPrefix + "/v1/account/login",
|
|
apiPrefix + "/v1/account/logout",
|
|
apiPrefix + "/v1/captcha",
|
|
apiPrefix + "/v1/config",
|
|
}...)))
|
|
v1 := g.Group("/v1")
|
|
// Websocket socket接口管理
|
|
{
|
|
_api := new(api.Websocket)
|
|
this.handler.GET("/ws", _api.Ws)
|
|
this.handler.GET("/pong", _api.Pong)
|
|
this.handler.GET("/publish", _api.Publish)
|
|
}
|
|
// Captcha 验证码接口管理
|
|
v1.GET("/captcha", new(api.Captcha).Captcha)
|
|
// Upload 上传接口管理
|
|
v1.POST("/upload", new(api.Upload).Upload)
|
|
// Dashboard 验证码接口管理
|
|
dashboardV1 := v1.Group("/dashboard")
|
|
{
|
|
_api := new(api.Dashboard)
|
|
dashboardV1.GET("/", _api.Index)
|
|
dashboardV1.POST("/repair", _api.Repair)
|
|
}
|
|
// Account 接口管理
|
|
accountV1 := v1.Group("/account")
|
|
{
|
|
_api := new(api.Account)
|
|
accountV1.POST("/login", _api.Login)
|
|
accountV1.POST("/logout", _api.Logout)
|
|
}
|
|
// Config 配置管理
|
|
configV1 := v1.Group("/config")
|
|
{
|
|
_api := new(api.Config)
|
|
configV1.GET("", _api.List)
|
|
configV1.POST("/edit", _api.Edit)
|
|
configV1.GET("/area", _api.Area)
|
|
configV1.POST("/breakdown", _api.Breakdown)
|
|
configV1.GET("/breakdown/select", _api.BreakdownSelect)
|
|
configV1.POST("/breakdown/add", _api.BreakdownAdd)
|
|
configV1.POST("/breakdown/edit", _api.BreakdownEdit)
|
|
configV1.POST("/breakdown/delete", _api.BreakdownDelete)
|
|
}
|
|
// Tenant 租户单位管理
|
|
tenantV1 := v1.Group("/tenant")
|
|
{
|
|
_api := new(api.Tenant)
|
|
tenantV1.POST("/list", _api.List)
|
|
tenantV1.POST("/add", _api.Add)
|
|
tenantV1.POST("/edit", _api.Edit)
|
|
tenantV1.POST("/delete", _api.Delete)
|
|
}
|
|
// User 用户管理
|
|
userV1 := v1.Group("/user")
|
|
{
|
|
_api := new(api.User)
|
|
userV1.GET("/info", _api.Info)
|
|
userV1.GET("/select", _api.Select)
|
|
userV1.GET("/menu", _api.Menu)
|
|
userV1.POST("/list", _api.List)
|
|
userV1.POST("/detail", _api.Detail)
|
|
userV1.POST("/add", _api.Add)
|
|
userV1.POST("/edit", _api.Edit)
|
|
userV1.POST("/delete", _api.Delete)
|
|
userV1.POST("/password/quick", _api.PasswordQuick)
|
|
userV1.POST("/password/tradition", _api.PasswordTradition)
|
|
userV1.POST("/activate", _api.Activate)
|
|
userV1.POST("/frozen", _api.Frozen)
|
|
userV1.GET("/role", _api.Role)
|
|
userV1.POST("/role/bind", _api.RoleBind)
|
|
}
|
|
// Menu 菜单管理
|
|
menuV1 := v1.Group("/menu")
|
|
{
|
|
_api := new(api.Menu)
|
|
menuV1.GET("/list", _api.List)
|
|
menuV1.POST("/add", _api.Add)
|
|
menuV1.POST("/edit", _api.Edit)
|
|
menuV1.POST("/status", _api.Status)
|
|
menuV1.POST("/delete", _api.Delete)
|
|
}
|
|
// Role 角色管理
|
|
roleV1 := v1.Group("/role")
|
|
{
|
|
_api := new(api.Role)
|
|
roleV1.GET("/list", _api.List)
|
|
roleV1.POST("/add", _api.Add)
|
|
roleV1.POST("/menu", _api.Edit)
|
|
roleV1.POST("/delete", _api.Delete)
|
|
roleV1.POST("/menus", _api.Menu)
|
|
roleV1.POST("/menu/bind", _api.MenuBind)
|
|
}
|
|
// Supplier 供应商管理
|
|
supplierV1 := v1.Group("/supplier")
|
|
{
|
|
_api := new(api.Supplier)
|
|
supplierV1.POST("/select", _api.Select)
|
|
supplierV1.POST("/material", _api.Material)
|
|
supplierV1.POST("/material/add", _api.MaterialAdd)
|
|
supplierV1.POST("/material/edit", _api.MaterialEdit)
|
|
supplierV1.POST("/material/delete", _api.MaterialDelete)
|
|
supplierV1.POST("/repair", _api.Repair)
|
|
supplierV1.POST("/repair/add", _api.RepairAdd)
|
|
supplierV1.POST("/repair/edit", _api.RepairEdit)
|
|
supplierV1.POST("/repair/delete", _api.RepairDelete)
|
|
supplierV1.POST("/manufacturer", _api.Manufacturer)
|
|
supplierV1.POST("/manufacturer/add", _api.ManufacturerAdd)
|
|
supplierV1.POST("/manufacturer/edit", _api.ManufacturerEdit)
|
|
supplierV1.POST("/manufacturer/delete", _api.ManufacturerDelete)
|
|
}
|
|
// Manage 数据管理
|
|
manageV1 := v1.Group("/manage")
|
|
{
|
|
_api := new(api.Manage)
|
|
manageV1.POST("/equipment", _api.Equipment)
|
|
manageV1.GET("/equipment/select", _api.EquipmentSelect)
|
|
manageV1.POST("/equipment/detail", _api.EquipmentDetail)
|
|
manageV1.POST("/equipment/add", _api.EquipmentAdd)
|
|
manageV1.POST("/equipment/edit", _api.EquipmentEdit)
|
|
manageV1.POST("/equipment/delete", _api.EquipmentDelete)
|
|
manageV1.POST("/equipment/material", _api.EquipmentMaterial)
|
|
manageV1.POST("/equipment/material/bind", _api.EquipmentMaterialBind)
|
|
manageV1.POST("/equipment/material/delete", _api.EquipmentMaterialDelete)
|
|
manageV1.POST("/material", _api.Material)
|
|
manageV1.GET("/material/select", _api.MaterialSelect)
|
|
manageV1.POST("/material/add", _api.MaterialAdd)
|
|
manageV1.POST("/material/edit", _api.MaterialEdit)
|
|
manageV1.POST("/material/delete", _api.MaterialDelete)
|
|
manageV1.POST("/material/supplier/select", _api.MaterialSupplierSelect)
|
|
manageV1.POST("/material/supplier/bind", _api.MaterialSupplierBind)
|
|
manageV1.POST("/material/supplier/delete", _api.MaterialSupplierDelete)
|
|
manageV1.POST("/notice", _api.Notice)
|
|
manageV1.POST("/notice/detail", _api.NoticeDetail)
|
|
manageV1.POST("/notice/add", _api.NoticeAdd)
|
|
manageV1.POST("/notice/edit", _api.NoticeEdit)
|
|
manageV1.POST("/notice/delete", _api.NoticeDelete)
|
|
}
|
|
// Work 工单管理
|
|
workV1 := v1.Group("/work")
|
|
{
|
|
_api := new(api.Work)
|
|
workV1.POST("/list", _api.Instance)
|
|
workV1.POST("/person", _api.Person)
|
|
workV1.POST("/workbench", _api.Workbench)
|
|
workV1.POST("/detail", _api.Detail)
|
|
workV1.POST("/launch", _api.Launch)
|
|
workV1.POST("/examine", _api.Examine)
|
|
workV1.POST("/delete", _api.Delete)
|
|
workV1.GET("/schedule", _api.Schedule)
|
|
workV1.POST("/schedule/edit", _api.ScheduleEdit)
|
|
workV1.POST("/schedule/delete", _api.ScheduleDelete)
|
|
workV1.POST("/repair", _api.Repair)
|
|
workV1.POST("/repair/detail", _api.RepairDetail)
|
|
workV1.POST("/repair/begin", _api.RepairBegin)
|
|
workV1.POST("/repair/finish", _api.RepairFinish)
|
|
workV1.POST("/repair/evaluate", _api.RepairEvaluate)
|
|
}
|
|
}
|
|
|
|
func (this *Router) Init() *gin.Engine {
|
|
gin.SetMode(this.Mode)
|
|
|
|
app := gin.New()
|
|
|
|
if this.IsCors {
|
|
app.Use(Cors())
|
|
}
|
|
app.NoRoute(NoRouteHandler())
|
|
app.NoMethod(NoMethodHandler())
|
|
app.Use(LoggerHandle("log/gin.log", 3))
|
|
app.Use(TimeoutHandle(time.Second * 30))
|
|
|
|
if config.IsDebug() {
|
|
gin.DefaultWriter = io.MultiWriter(os.Stdout)
|
|
app.StaticFS("/api-docs", http.Dir("./doc"))
|
|
} else {
|
|
app.Use(RecoveryHandler())
|
|
}
|
|
if this.RateLimitConfig != nil {
|
|
if this.RateLimitConfig.IsRate {
|
|
rate.NewIPRateLimiter()(this.RateLimitConfig.Limit, this.RateLimitConfig.Capacity)
|
|
app.Use(rate.RequestIPRateLimiter()(this.RateLimitConfig.Limit, this.RateLimitConfig.Capacity))
|
|
}
|
|
}
|
|
app.StaticFS("/upload", http.Dir("./upload"))
|
|
app.MaxMultipartMemory = 4 << 20
|
|
this.handler = app
|
|
// 注册路由
|
|
this.registerWeb()
|
|
this.registerAPI()
|
|
|
|
return app
|
|
}
|
|
|
|
func NewRouter(option *Option) *Router {
|
|
return &Router{Option: option}
|
|
}
|