feat: restructure API routing and remove ginx package

- Added new routing functions for API and dashboard endpoints in controller_routes.go.
- Replaced previous ginx.HandleController calls with explicit route registrations.
- Removed the ginx package as it is no longer needed for handling controllers.
- Improved organization and readability of route definitions.
This commit is contained in:
mlogclub
2026-05-23 22:12:13 +08:00
parent 79614b2d07
commit d21b420765
3 changed files with 1037 additions and 175 deletions
File diff suppressed because it is too large Load Diff
+34 -38
View File
@@ -10,12 +10,8 @@ import (
"cs-agent/internal/ai/mcps"
_ "cs-agent/internal/ai/runtime"
"cs-agent/internal/controllers/api"
"cs-agent/internal/controllers/dashboard"
"cs-agent/internal/controllers/third"
"cs-agent/internal/middleware"
"cs-agent/internal/pkg/config"
"cs-agent/internal/pkg/ginx"
"cs-agent/internal/services"
"github.com/gin-gonic/gin"
@@ -94,11 +90,11 @@ func addRouter(app *gin.Engine) {
app.Any("/api/mcp", gin.WrapH(mcps.NewHTTPHandler()))
apiGroup := app.Group("/api")
ginx.HandleController(apiGroup, "/auth", new(api.AuthController))
ginx.HandleController(apiGroup, "/channel", new(api.ChannelController))
ginx.HandleController(apiGroup, "/customer", new(api.CustomerController))
ginx.HandleController(apiGroup, "/conversation", new(api.ConversationController), middleware.ExternalUserMiddleware)
ginx.HandleController(apiGroup, "/message", new(api.MessageController), middleware.ExternalUserMiddleware)
registerApiAuthRoutes(apiGroup.Group("/auth"))
registerApiChannelRoutes(apiGroup.Group("/channel"))
registerApiCustomerRoutes(apiGroup.Group("/customer"))
registerApiConversationRoutes(apiGroup.Group("/conversation", middleware.ExternalUserMiddleware))
registerApiMessageRoutes(apiGroup.Group("/message", middleware.ExternalUserMiddleware))
wsGroup := app.Group("/api/ws")
wsGroup.GET("/dashboard", middleware.AuthMiddleware, services.WsService.HandleDashboardWS)
@@ -106,37 +102,37 @@ func addRouter(app *gin.Engine) {
wsGroup.GET("/open", services.WsService.HandleOpenWS)
dashboardGroup := app.Group("/api/dashboard", middleware.AuthMiddleware)
ginx.HandleController(dashboardGroup, "/dashboard", new(dashboard.DashboardController))
ginx.HandleController(dashboardGroup, "/user", new(dashboard.UserController))
ginx.HandleController(dashboardGroup, "/company", new(dashboard.CompanyController))
ginx.HandleController(dashboardGroup, "/customer", new(dashboard.CustomerController))
ginx.HandleController(dashboardGroup, "/customer-contact", new(dashboard.CustomerContactController))
ginx.HandleController(dashboardGroup, "/role", new(dashboard.RoleController))
ginx.HandleController(dashboardGroup, "/permission", new(dashboard.PermissionController))
ginx.HandleController(dashboardGroup, "/session", new(dashboard.SessionController))
ginx.HandleController(dashboardGroup, "/tag", new(dashboard.TagController))
ginx.HandleController(dashboardGroup, "/conversation", new(dashboard.ConversationController))
ginx.HandleController(dashboardGroup, "/ticket", new(dashboard.TicketController))
ginx.HandleController(dashboardGroup, "/notification", new(dashboard.NotificationController))
ginx.HandleController(dashboardGroup, "/quick-reply", new(dashboard.QuickReplyController))
ginx.HandleController(dashboardGroup, "/channel", new(dashboard.ChannelController))
ginx.HandleController(dashboardGroup, "/agent", new(dashboard.AgentController))
ginx.HandleController(dashboardGroup, "/agent-team", new(dashboard.AgentTeamController))
ginx.HandleController(dashboardGroup, "/agent-team-schedule", new(dashboard.AgentTeamScheduleController))
ginx.HandleController(dashboardGroup, "/ai-agent", new(dashboard.AIAgentController))
ginx.HandleController(dashboardGroup, "/ai-config", new(dashboard.AIConfigController))
ginx.HandleController(dashboardGroup, "/asset", new(dashboard.AssetController))
ginx.HandleController(dashboardGroup, "/knowledge-base", new(dashboard.KnowledgeBaseController))
ginx.HandleController(dashboardGroup, "/knowledge-document", new(dashboard.KnowledgeDocumentController))
ginx.HandleController(dashboardGroup, "/knowledge-faq", new(dashboard.KnowledgeFAQController))
ginx.HandleController(dashboardGroup, "/knowledge-retrieve", new(dashboard.KnowledgeRetrieveController))
ginx.HandleController(dashboardGroup, "/knowledge-retrieve-log", new(dashboard.KnowledgeRetrieveLogController))
ginx.HandleController(dashboardGroup, "/agent-run-log", new(dashboard.AgentRunLogController))
ginx.HandleController(dashboardGroup, "/skill-definition", new(dashboard.SkillDefinitionController))
ginx.HandleController(dashboardGroup, "/mcp", new(dashboard.MCPController))
registerDashboardDashboardRoutes(dashboardGroup.Group("/dashboard"))
registerDashboardUserRoutes(dashboardGroup.Group("/user"))
registerDashboardCompanyRoutes(dashboardGroup.Group("/company"))
registerDashboardCustomerRoutes(dashboardGroup.Group("/customer"))
registerDashboardCustomerContactRoutes(dashboardGroup.Group("/customer-contact"))
registerDashboardRoleRoutes(dashboardGroup.Group("/role"))
registerDashboardPermissionRoutes(dashboardGroup.Group("/permission"))
registerDashboardSessionRoutes(dashboardGroup.Group("/session"))
registerDashboardTagRoutes(dashboardGroup.Group("/tag"))
registerDashboardConversationRoutes(dashboardGroup.Group("/conversation"))
registerDashboardTicketRoutes(dashboardGroup.Group("/ticket"))
registerDashboardNotificationRoutes(dashboardGroup.Group("/notification"))
registerDashboardQuickReplyRoutes(dashboardGroup.Group("/quick-reply"))
registerDashboardChannelRoutes(dashboardGroup.Group("/channel"))
registerDashboardAgentRoutes(dashboardGroup.Group("/agent"))
registerDashboardAgentTeamRoutes(dashboardGroup.Group("/agent-team"))
registerDashboardAgentTeamScheduleRoutes(dashboardGroup.Group("/agent-team-schedule"))
registerDashboardAIAgentRoutes(dashboardGroup.Group("/ai-agent"))
registerDashboardAIConfigRoutes(dashboardGroup.Group("/ai-config"))
registerDashboardAssetRoutes(dashboardGroup.Group("/asset"))
registerDashboardKnowledgeBaseRoutes(dashboardGroup.Group("/knowledge-base"))
registerDashboardKnowledgeDocumentRoutes(dashboardGroup.Group("/knowledge-document"))
registerDashboardKnowledgeFAQRoutes(dashboardGroup.Group("/knowledge-faq"))
registerDashboardKnowledgeRetrieveRoutes(dashboardGroup.Group("/knowledge-retrieve"))
registerDashboardKnowledgeRetrieveLogRoutes(dashboardGroup.Group("/knowledge-retrieve-log"))
registerDashboardAgentRunLogRoutes(dashboardGroup.Group("/agent-run-log"))
registerDashboardSkillDefinitionRoutes(dashboardGroup.Group("/skill-definition"))
registerDashboardMCPRoutes(dashboardGroup.Group("/mcp"))
thirdGroup := app.Group("/api/third")
ginx.HandleController(thirdGroup, "/wechat", new(third.WechatController))
registerThirdWechatRoutes(thirdGroup.Group("/wechat"))
}
func registerDashboardStatic(app *gin.Engine, root string) {
-137
View File
@@ -1,137 +0,0 @@
package ginx
import (
"net/http"
"reflect"
"strconv"
"strings"
"unicode"
"github.com/gin-gonic/gin"
"github.com/mlogclub/simple/web"
)
var jsonResultType = reflect.TypeOf((*web.JsonResult)(nil))
func HandleController(group *gin.RouterGroup, relativePath string, prototype any, handlers ...gin.HandlerFunc) {
router := group.Group(relativePath, handlers...)
t := reflect.TypeOf(prototype)
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
panic("ginx.HandleController requires a pointer to a controller struct")
}
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
httpMethods, path, ok := parseAction(method)
if !ok {
continue
}
handler := buildHandler(t, method)
for _, httpMethod := range httpMethods {
router.Handle(httpMethod, path, handler)
}
}
}
func parseAction(method reflect.Method) ([]string, string, bool) {
prefixes := []struct {
name string
methods []string
}{
{"Any", []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions}},
{"Get", []string{http.MethodGet}},
{"Post", []string{http.MethodPost}},
{"Put", []string{http.MethodPut}},
{"Delete", []string{http.MethodDelete}},
}
for _, prefix := range prefixes {
if !strings.HasPrefix(method.Name, prefix.name) {
continue
}
suffix := strings.TrimPrefix(method.Name, prefix.name)
return prefix.methods, actionPath(suffix, method.Type.NumIn()-1), true
}
return nil, "", false
}
func actionPath(suffix string, argCount int) string {
if suffix == "" {
return "/"
}
if suffix == "By" && argCount == 1 {
return "/:id"
}
if strings.HasSuffix(suffix, "By") && argCount == 1 {
base := strings.TrimSuffix(suffix, "By")
return "/" + actionSegmentPath(base) + "/:id"
}
return "/" + actionSegmentPath(suffix)
}
func actionSegmentPath(s string) string {
if s == "" {
return ""
}
parts := strings.Split(s, "_")
for i, part := range parts {
parts[i] = camelToPath(part)
}
return strings.Join(parts, "_")
}
func camelToPath(s string) string {
var b strings.Builder
for i, r := range s {
if unicode.IsUpper(r) {
if i > 0 {
b.WriteByte('/')
}
r = unicode.ToLower(r)
}
b.WriteRune(r)
}
return b.String()
}
func buildHandler(controllerType reflect.Type, method reflect.Method) gin.HandlerFunc {
return func(ctx *gin.Context) {
controller := reflect.New(controllerType.Elem())
if field := controller.Elem().FieldByName("Ctx"); field.IsValid() && field.CanSet() {
field.Set(reflect.ValueOf(ctx))
}
args := []reflect.Value{controller}
for i := 1; i < method.Type.NumIn(); i++ {
argType := method.Type.In(i)
raw := ctx.Param("id")
value, ok := convertPathArg(raw, argType)
if !ok {
ctx.JSON(http.StatusBadRequest, web.JsonErrorMsg("路径参数错误"))
return
}
args = append(args, value)
}
results := method.Func.Call(args)
if len(results) == 0 || results[0].IsNil() {
return
}
if result, ok := results[0].Interface().(*web.JsonResult); ok {
ctx.JSON(http.StatusOK, result)
}
}
}
func convertPathArg(raw string, t reflect.Type) (reflect.Value, bool) {
switch t.Kind() {
case reflect.Int64:
v, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return reflect.Value{}, false
}
return reflect.ValueOf(v), true
case reflect.String:
return reflect.ValueOf(raw), true
default:
return reflect.Zero(t), false
}
}