75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"frpc-console/internal/auth"
|
|
)
|
|
|
|
//go:embed static/*
|
|
var staticFS embed.FS
|
|
|
|
// SetupRouter 设置路由
|
|
func SetupRouter() *gin.Engine {
|
|
r := gin.Default()
|
|
|
|
// 从 embed 读取前端静态文件
|
|
staticSubFS, _ := fs.Sub(staticFS, "static")
|
|
r.StaticFS("/static", http.FS(staticSubFS))
|
|
|
|
// 根路由
|
|
r.GET("/", func(c *gin.Context) {
|
|
content, err := staticFS.ReadFile("static/index.html")
|
|
if err != nil {
|
|
c.String(500, "加载前端页面失败")
|
|
return
|
|
}
|
|
c.Data(http.StatusOK, "text/html; charset=utf-8", content)
|
|
})
|
|
|
|
// 健康检查
|
|
r.GET("/ping", func(c *gin.Context) {
|
|
c.String(200, "frpc-console 后端已启动 🎉")
|
|
})
|
|
|
|
api := r.Group("/api")
|
|
{
|
|
// ---- 公开路由(不需要认证) ----
|
|
api.GET("/check/users", CheckUsersHandler)
|
|
api.POST("/register", RegisterHandler)
|
|
api.POST("/login", LoginHandler)
|
|
api.GET("/ping", PingHandler)
|
|
|
|
// ---- 需要认证的路由 ----
|
|
authGroup := api.Group("/")
|
|
authGroup.Use(auth.AuthMiddleware())
|
|
{
|
|
authGroup.GET("/config", GetConfigHandler)
|
|
authGroup.PUT("/config", UpdateConfigHandler)
|
|
|
|
authGroup.GET("/proxies", GetProxiesHandler)
|
|
authGroup.GET("/proxy/:id", GetProxyHandler)
|
|
authGroup.POST("/proxy", CreateProxyHandler)
|
|
authGroup.PUT("/proxy/:id", UpdateProxyHandler)
|
|
authGroup.DELETE("/proxy/:id", DeleteProxyHandler)
|
|
|
|
authGroup.POST("/frpc/reload", ReloadFrpcHandler)
|
|
authGroup.POST("/frpc/start", StartFrpcHandler)
|
|
authGroup.POST("/frpc/stop", StopFrpcHandler)
|
|
authGroup.GET("/frpc/status", GetFrpcStatusHandler)
|
|
authGroup.GET("/frpc/log", GetFrpcLogHandler)
|
|
|
|
authGroup.POST("/import/toml", ImportTomlHandler)
|
|
authGroup.GET("/export/toml", ExportTomlHandler)
|
|
|
|
authGroup.PUT("/user/password", ChangePasswordHandler)
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|