Files
opsx-licence/backend/router/router.go
T
Logan.Li 4ae318335c feat: 完成 opsx-licence 许可证管理平台全栈实现
基于 opsx-web-lite 框架(Go+Gin+Vue3+Element Plus)构建许可证管理服务器,
业务域参考 Licence 服务器 Web 版。

【后端 - 4层架构】
- 认证:单 Admin 表 + JWT(HS256)
- 核心授权域:Product / Customer / License / Activation
  · License 生成/验证/激活/机器绑定/心跳/续期/吊销
  · 客户端契约接口:/api/v1/license/{verify,activate,deactivate,heartbeat}
- 定价订单:Plan / Order / 支付网关配置
- 防重复购买:CustomerEntitlement + 幂等支付 webhook + 手动发放
- 分析审计:Dashboard 统计 / Analytics / AuditLog / Setting 管理

【前端】
- Dashboard(ECharts 图表)、产品、客户、License、计划、订单、权益、日志、设置
- 9 个完整 CRUD 业务页面 + 登录/布局

【部署】
- SQLite + 单二进制(//go:embed 嵌入前端)
- 开发:./run.sh start(前端5181/后端9001)
- 生产:./run.sh build && ./run.sh binary start
- 默认账号:admin/admin

Author: Logan.Li
2026-08-07 15:02:00 +08:00

226 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"backend/config"
"backend/internal/controller/analytics"
"backend/internal/controller/auth"
"backend/internal/controller/customer"
"backend/internal/controller/entitlement"
"backend/internal/controller/license"
"backend/internal/controller/order"
"backend/internal/controller/plan"
"backend/internal/controller/product"
"backend/internal/controller/setting"
"backend/middleware"
"strings"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// register 同时注册带和不带 trailing slash 的路由 // Logan.Li
func register(r gin.IRoutes, method, relativePath string, handlers ...gin.HandlerFunc) {
isParamRoute := strings.Contains(relativePath, ":")
switch method {
case "GET":
r.GET(relativePath, handlers...)
if !isParamRoute {
r.GET(relativePath+"/", handlers...)
}
case "POST":
r.POST(relativePath, handlers...)
if !isParamRoute {
r.POST(relativePath+"/", handlers...)
}
case "PUT":
r.PUT(relativePath, handlers...)
if !isParamRoute {
r.PUT(relativePath+"/", handlers...)
}
case "DELETE":
r.DELETE(relativePath, handlers...)
if !isParamRoute {
r.DELETE(relativePath+"/", handlers...)
}
case "PATCH":
r.PATCH(relativePath, handlers...)
if !isParamRoute {
r.PATCH(relativePath+"/", handlers...)
}
}
}
// SetupRouter 初始化路由 // Logan.Li
func SetupRouter(db *gorm.DB, cfg *config.Config) *gin.Engine {
gin.SetMode(cfg.Mode)
r := gin.Default()
r.RedirectTrailingSlash = false
r.RedirectFixedPath = false
// Gzip 压缩中间件 // Logan.Li
r.Use(gzip.Gzip(gzip.DefaultCompression))
// CORS 中间件 // Logan.Li
r.Use(middleware.CORSMiddleware(cfg))
// 健康检查 // Logan.Li
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"code": 0, "message": "ok", "data": nil})
})
// 初始化控制器 // Logan.Li
authController := auth.NewAuthController(db, cfg)
productController := product.NewProductController(db)
customerController := customer.NewCustomerController(db)
licenseController := license.NewLicenseController(db)
planController := plan.NewPlanController(db)
orderController := order.NewOrderController(db)
entitlementController := entitlement.NewEntitlementController(db)
analyticsController := analytics.NewAnalyticsController(db)
settingController := setting.NewSettingController(db)
api := r.Group("/api")
{
// ============ 认证路由(无需 JWT) ============
authGroup := api.Group("/auth")
{
register(authGroup, "POST", "/login", authController.Login)
}
// ============ 客户端授权契约(无需认证,供外部工具调用) ============
clientAPI := api.Group("/v1")
{
// License 验证 / 激活 / 停用 / 心跳
register(clientAPI, "POST", "/license/verify", licenseController.Verify)
register(clientAPI, "POST", "/license/activate", licenseController.ActivateDevice)
register(clientAPI, "POST", "/license/deactivate", licenseController.Deactivate)
register(clientAPI, "POST", "/license/heartbeat", licenseController.Heartbeat)
// 产品定价查询(展示用)
clientAPI.GET("/products/:product_id/plans", planController.GetByProduct)
// 订单与支付(客户端购买入口 + 支付回调)
register(clientAPI, "POST", "/orders", orderController.CreateOrder)
register(clientAPI, "POST", "/webhooks/payment", orderController.PaymentWebhook)
register(clientAPI, "POST", "/purchase/check", orderController.CheckDuplicatePurchase)
register(clientAPI, "POST", "/purchase/status", orderController.CheckPurchaseStatus)
// 客户权益查询(通过邮箱)
register(clientAPI, "GET", "/entitlements/my", entitlementController.GetByEmail)
}
// ============ 需要认证的管理路由 ============
protected := api.Group("", middleware.AuthMiddleware(cfg))
{
// 认证相关
register(protected, "GET", "/auth/me", authController.Me)
register(protected, "PUT", "/auth/password", authController.ChangePassword)
// 产品管理
products := protected.Group("/products")
{
register(products, "GET", "", productController.List)
register(products, "GET", "/all", productController.GetAll)
register(products, "POST", "", productController.Create)
register(products, "GET", "/:id", productController.Get)
register(products, "PUT", "/:id", productController.Update)
register(products, "DELETE", "/:id", productController.Delete)
register(products, "PATCH", "/:id/status", productController.UpdateStatus)
}
// 客户管理
customers := protected.Group("/customers")
{
register(customers, "GET", "", customerController.List)
register(customers, "GET", "/all", customerController.GetAll)
register(customers, "POST", "", customerController.Create)
register(customers, "GET", "/:id", customerController.Get)
register(customers, "PUT", "/:id", customerController.Update)
register(customers, "DELETE", "/:id", customerController.Delete)
register(customers, "PATCH", "/:id/status", customerController.UpdateStatus)
}
// License 管理
licenses := protected.Group("/licenses")
{
register(licenses, "GET", "", licenseController.List)
register(licenses, "POST", "", licenseController.Create)
register(licenses, "GET", "/export", licenseController.Export)
register(licenses, "GET", "/:id", licenseController.Get)
register(licenses, "PUT", "/:id", licenseController.Update)
register(licenses, "DELETE", "/:id", licenseController.Delete)
register(licenses, "POST", "/:id/renew", licenseController.Renew)
register(licenses, "POST", "/:id/revoke", licenseController.Revoke)
register(licenses, "POST", "/:id/suspend", licenseController.Suspend)
register(licenses, "POST", "/:id/activate", licenseController.Activate)
register(licenses, "GET", "/:id/activations", licenseController.GetActivations)
register(licenses, "POST", "/:id/activations/:activationId/offline", licenseController.ForceOffline)
}
// 定价计划管理
plans := protected.Group("/plans")
{
register(plans, "GET", "", planController.List)
register(plans, "POST", "", planController.Create)
register(plans, "PUT", "/:id", planController.Update)
register(plans, "DELETE", "/:id", planController.Delete)
register(plans, "PATCH", "/:id/status", planController.ToggleStatus)
}
// 订单管理
orders := protected.Group("/orders")
{
register(orders, "GET", "", orderController.List)
register(orders, "GET", "/:id", orderController.GetDetail)
register(orders, "POST", "/manual-grant", orderController.ManualGrant)
}
// 客户权益管理
entitlements := protected.Group("/entitlements")
{
register(entitlements, "GET", "", entitlementController.List)
register(entitlements, "GET", "/:id", entitlementController.Get)
register(entitlements, "POST", "/:id/revoke", entitlementController.Revoke)
register(entitlements, "POST", "/:id/renew", entitlementController.Renew)
}
// 仪表板与数据分析
dashboard := protected.Group("/dashboard")
{
register(dashboard, "GET", "/stats", analyticsController.GetDashboardStats)
register(dashboard, "GET", "/expiring", analyticsController.GetExpiringLicenses)
register(dashboard, "GET", "/activities", analyticsController.GetRecentActivities)
register(dashboard, "GET", "/online-stats", analyticsController.GetOnlineStats)
register(dashboard, "GET", "/revenue", analyticsController.GetRevenueStats)
}
analyticsGroup := protected.Group("/analytics")
{
register(analyticsGroup, "GET", "/license-growth", analyticsController.GetLicenseGrowth)
register(analyticsGroup, "GET", "/product-distribution", analyticsController.GetProductDistribution)
}
// 日志审计与系统设置
logs := protected.Group("/logs")
{
register(logs, "GET", "", settingController.ListAuditLogs)
register(logs, "DELETE", "/clear", settingController.ClearAuditLogs)
}
system := protected.Group("/settings")
{
register(system, "GET", "", settingController.Get)
register(system, "PUT", "", settingController.Update)
register(system, "GET", "/license-templates", settingController.ListLicenseTemplates)
register(system, "POST", "/license-templates", settingController.SaveLicenseTemplate)
register(system, "DELETE", "/license-templates/:id", settingController.DeleteLicenseTemplate)
}
}
}
return r
}