refactor: 重构项目结构并优化路由组织
All checks were successful
部署开发环境 / deploy-dev (push) Successful in 8m30s

This commit is contained in:
2025-10-28 12:20:16 +08:00
parent ea9ecb770d
commit 819a2eb8ec
7 changed files with 50 additions and 33 deletions

41
api/api.go Normal file
View File

@@ -0,0 +1,41 @@
package api
import (
"dypid/internal/controller"
"embed"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
)
func RegRoutes(r *gin.Engine) {
g := r.Group("/api") //初始化路由组 /api/xxxx
{
g.GET("/token", controller.ListTokenHandler) //获取token列表
g.POST("/token", controller.CreateTokenHandler) //创建token
g.PUT("/token", controller.UpdateTokenHandler) //更新token
g.DELETE("/token", controller.DeleteTokenHandler) //删除token
g.GET("/token/info", controller.GetTokenInfoHandler) //获取token信息
g.DELETE("/token/info", controller.DeleteTokenInfoHandler) //删除token数据库
}
{
g.GET("/data", controller.ReadDataHandler) //获取数据
g.POST("/data", controller.WriteDataHandler) //写入数据
}
}
func RegWebService(r *gin.Engine, webDir embed.FS) {
assets, _ := fs.Sub(webDir, "web/dist/assets")
r.StaticFS("/assets", http.FS(assets))
icon, _ := fs.ReadFile(webDir, "web/dist/favicon.ico")
r.GET("/favicon.ico", func(c *gin.Context) {
c.Data(200, "image/x-icon", icon)
})
indexHtml, _ := fs.ReadFile(webDir, "web/dist/index.html")
r.NoRoute(func(c *gin.Context) {
c.Data(200, "text/html; charset=utf-8", indexHtml)
})
}

View File

@@ -1,8 +1,8 @@
package controller
import (
"dypid/db"
"dypid/global"
"dypid/internal/db"
"fmt"
"net/http"
"strings"

View File

@@ -1,8 +1,8 @@
package controller
import (
"dypid/db"
"dypid/global"
"dypid/internal/db"
"net/http"
"strconv"

38
main.go
View File

@@ -1,13 +1,11 @@
package main
import (
"dypid/api"
"dypid/config"
"dypid/controller"
"dypid/db"
"dypid/internal/db"
"embed"
"fmt"
"io/fs"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
@@ -23,35 +21,13 @@ func main() {
//初始化一个http服务对象
gin.SetMode(config.APPConfig.RunMode)
r := gin.Default()
//跨域设置
r.Use(cors.Default())
//Vue网站服务
assets, _ := fs.Sub(webDir, "web/dist/assets")
r.StaticFS("/assets", http.FS(assets))
icon, _ := fs.ReadFile(webDir, "web/dist/favicon.ico")
r.GET("/favicon.ico", func(c *gin.Context) {
c.Data(200, "image/x-icon", icon)
})
indexHtml, _ := fs.ReadFile(webDir, "web/dist/index.html")
r.NoRoute(func(c *gin.Context) {
c.Data(200, "text/html; charset=utf-8", indexHtml)
})
//API接口
g := r.Group("/api") //初始化路由组 /api/xxxx
{
g.GET("/token", controller.ListTokenHandler) //获取token列表
g.POST("/token", controller.CreateTokenHandler) //创建token
g.PUT("/token", controller.UpdateTokenHandler) //更新token
g.DELETE("/token", controller.DeleteTokenHandler) //删除token
g.GET("/token/info", controller.GetTokenInfoHandler) //获取token信息
g.DELETE("/token/info", controller.DeleteTokenInfoHandler) //删除token数据库
}
{
g.GET("/data", controller.ReadDataHandler) //获取数据
g.POST("/data", controller.WriteDataHandler) //写入数据
}
//注册网页服务Vue
api.RegWebService(r, webDir)
//注册API接口
api.RegRoutes(r)
// 监听并在 0.0.0.0:8080 上启动服务
fmt.Printf("服务器正在运行http://%s\n", config.APPConfig.Host)