正在优化linux端的运行状态于逻辑

This commit is contained in:
2026-07-24 20:13:21 +08:00
parent 2105ed9d8e
commit 699a9e3b92
9 changed files with 106 additions and 54 deletions
+29 -6
View File
@@ -2,7 +2,9 @@ package main
import (
"bytes"
"embed"
"fmt"
"io/fs"
"net/http"
"strconv"
"text/template"
@@ -11,45 +13,66 @@ import (
"golang.org/x/crypto/bcrypt"
)
//go:embed static/*
var staticFS embed.FS
func SetupRouter() *gin.Engine {
r := gin.Default()
r.Static("/static", "./static")
// 从 embed 读取前端静态文件
// 注意:embed.FS 不支持直接作为 http.FileSystem 传参,需要用 http.FS 转换
staticSubFS, _ := fs.Sub(staticFS, "static")
r.StaticFS("/static", http.FS(staticSubFS))
// 根路由:直接返回 index.html
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 后端已启动 🎉")
})
r.GET("/", func(c *gin.Context) {
c.File("./static/index.html")
})
// API 路由
api := r.Group("/api")
{
// 公开接口
// 登录(无需鉴权)
api.GET("/check/users", checkUsersHandler)
api.POST("/register", registerHandler)
api.POST("/login", loginHandler)
// 需鉴权
// 需鉴权的路由
auth := api.Group("/")
auth.Use(AuthMiddleware())
{
// 全局配置
auth.GET("/config", getConfigHandler)
auth.PUT("/config", updateConfigHandler)
// 隧道管理
auth.GET("/proxies", getProxiesHandler)
auth.GET("/proxy/:id", getProxyHandler)
auth.POST("/proxy", createProxyHandler)
auth.PUT("/proxy/:id", updateProxyHandler)
auth.DELETE("/proxy/:id", deleteProxyHandler)
// frpc 控制
auth.POST("/frpc/reload", reloadFrpcHandler)
auth.POST("/frpc/start", startFrpcHandler)
auth.POST("/frpc/stop", stopFrpcHandler)
auth.GET("/frpc/status", getFrpcStatusHandler)
// 导入/导出 TOML
auth.POST("/import/toml", importTomlHandler)
auth.GET("/export/toml", ExportTomlHandler)
// 修改密码
auth.PUT("/user/password", changePasswordHandler)
}
}