64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package main
|
||
|
||
import (
|
||
"dypid/config"
|
||
"dypid/controller"
|
||
"dypid/db"
|
||
"embed"
|
||
"fmt"
|
||
"io/fs"
|
||
"net/http"
|
||
|
||
"github.com/gin-contrib/cors"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
//go:embed web/dist/*
|
||
var webDir embed.FS
|
||
|
||
func main() {
|
||
config.InitConfig()
|
||
db.InitRedis()
|
||
db.InitLocalDB()
|
||
//初始化一个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) //写入数据
|
||
}
|
||
|
||
// 监听并在 0.0.0.0:8080 上启动服务
|
||
fmt.Printf("服务器正在运行:http://%s\n", config.APPConfig.Host)
|
||
err := r.Run(config.APPConfig.Host)
|
||
if err != nil {
|
||
fmt.Println("服务启动失败:", err)
|
||
return
|
||
}
|
||
}
|