1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package main
- import (
- "cicv-data-closedloop/amd64/web_server/handler"
- "cicv-data-closedloop/common/config/c_db"
- "cicv-data-closedloop/common/config/c_log"
- "cicv-data-closedloop/common/gin/middleware"
- _ "embed"
- "github.com/gin-gonic/gin"
- "gopkg.in/yaml.v3"
- "os"
- )
- type ApplicationYamlStruct struct {
- ApplicationName string `yaml:"application"`
- Web struct {
- Port string `yaml:"port"`
- RoutePrefix string `yaml:"route-prefix"`
- Token string `yaml:"token"`
- WhiteList []string `yaml:"white-list"`
- } `yaml:"web"`
- Log struct {
- Dir string `yaml:"dir"`
- Prefix string `yaml:"prefix"`
- } `yaml:"log"`
- Mysql struct {
- Ip string `yaml:"ip"`
- Port string `yaml:"port"`
- Username string `yaml:"username"`
- Password string `yaml:"password"`
- Dbname string `yaml:"dbname"`
- Charset string `yaml:"charset"`
- SqlfileDir string `yaml:"sqlfile-dir"`
- } `yaml:"mysql"`
- }
- var (
- //go:embed application.yaml
- applicationYamlBytes []byte
- ApplicationYaml ApplicationYamlStruct
- )
- func init() {
- // 解析YAML内容
- _ = yaml.Unmarshal(applicationYamlBytes, &ApplicationYaml)
- c_log.InitLog(
- ApplicationYaml.Log.Dir,
- ApplicationYaml.Log.Prefix,
- )
- c_db.InitSqlxMysql(
- ApplicationYaml.Mysql.Username,
- ApplicationYaml.Mysql.Password,
- ApplicationYaml.Mysql.Ip,
- ApplicationYaml.Mysql.Port,
- ApplicationYaml.Mysql.Dbname,
- ApplicationYaml.Mysql.Charset,
- )
- c_db.InitSqlFilesMap(ApplicationYaml.Mysql.SqlfileDir)
- }
- func main() {
- c_log.GlobalLogger.Info("配置文件为:", ApplicationYaml)
- // 创建 gin 实例
- router := gin.Default()
- // 使用中间件
- //router.Use(cors.Default())
- router.Use(middleware.Cors())
- router.Use(middleware.LogRequest()) // 请求日志打印
- router.Use(middleware.ValidateHeaders(ApplicationYaml.Web.WhiteList, ApplicationYaml.Web.Token)) // 全局请求头校验
- // 通过路由组设置全局前缀
- projectPrefix := router.Group(ApplicationYaml.Web.RoutePrefix)
- reportPrefix := projectPrefix.Group("/report")
- reportPrefix.POST("/page", handler.Page) // 分页查询
- //reportPrefix.POST("/pdf", handler.Hello) // pdf下载
- monitorPrefix := projectPrefix.Group("/monitor")
- monitorPrefix.POST("/insert", handler.SaveDeviceMonitor)
- // 端口
- err := router.Run(":" + ApplicationYaml.Web.Port)
- if err != nil {
- c_log.GlobalLogger.Error("程序崩溃,监听端口 "+ApplicationYaml.Web.Port+" 失败:", err)
- os.Exit(-1)
- }
- }
|