This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string `mapstructure:"host"`
|
||||
RunMode string `mapstructure:"run-mode"`
|
||||
Redis Redis `mapstructure:"redis"`
|
||||
}
|
||||
type Redis struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
}
|
||||
|
||||
var APPConfig Config
|
||||
|
||||
func InitConfig() {
|
||||
//设置默认值
|
||||
defaultConf := Config{
|
||||
Host: "0.0.0.0:8080",
|
||||
RunMode: "release",
|
||||
Redis: Redis{
|
||||
Host: "localhost:6379",
|
||||
Password: "",
|
||||
DB: 0,
|
||||
},
|
||||
}
|
||||
viper.SetDefault("host", defaultConf.Host)
|
||||
viper.SetDefault("run-mode", defaultConf.RunMode)
|
||||
viper.SetDefault("redis", defaultConf.Redis)
|
||||
//设置配置文件名和路径 ./config.toml
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
viper.SafeWriteConfig() //安全写入默认配置
|
||||
//读取配置文件
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
fmt.Errorf("无法读取配置文件: %w", err)
|
||||
}
|
||||
//解析配置文件
|
||||
if err := viper.Unmarshal(&APPConfig); err != nil {
|
||||
fmt.Errorf("无法解析配置: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"dypid/global"
|
||||
"dypid/internal/db"
|
||||
"dypid/internal/global"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"dypid/global"
|
||||
"dypid/internal/db"
|
||||
"dypid/internal/global"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"dypid/config"
|
||||
"dypid/global"
|
||||
"dypid/internal/config"
|
||||
"dypid/internal/global"
|
||||
"fmt"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var RDB *redis.Client
|
||||
var RCtx = context.Background()
|
||||
|
||||
var Version = "dev"
|
||||
@@ -0,0 +1,51 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"dypid/internal/controller"
|
||||
"dypid/internal/global"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegAPIService(r *gin.Engine) {
|
||||
g := r.Group("/api") //初始化路由组 /api/xxxx
|
||||
{
|
||||
g.GET("/test", func(context *gin.Context) {
|
||||
context.String(http.StatusOK, "ok")
|
||||
})
|
||||
g.GET("/version", func(context *gin.Context) {
|
||||
context.String(http.StatusOK, fmt.Sprintf("程序版本: %s\nGin: %s", global.Version, gin.Version))
|
||||
})
|
||||
}
|
||||
{
|
||||
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, webFiles embed.FS) {
|
||||
assets, _ := fs.Sub(webFiles, "web/dist/assets")
|
||||
r.StaticFS("/assets", http.FS(assets))
|
||||
|
||||
icon, _ := fs.ReadFile(webFiles, "web/dist/favicon.ico")
|
||||
r.GET("/favicon.ico", func(c *gin.Context) {
|
||||
c.Data(200, "image/x-icon", icon)
|
||||
})
|
||||
|
||||
indexHtml, _ := fs.ReadFile(webFiles, "web/dist/index.html")
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
c.Data(200, "text/html; charset=utf-8", indexHtml)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user