12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package main
- import (
- "cicv-data-closedloop/common/config/c_db"
- "cicv-data-closedloop/common/config/c_log"
- "cicv-data-closedloop/common/gin/handler"
- "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"`
- } `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.Ip,
- ApplicationYaml.Mysql.Port,
- ApplicationYaml.Mysql.Username,
- ApplicationYaml.Mysql.Password,
- ApplicationYaml.Mysql.Dbname,
- ApplicationYaml.Mysql.Charset,
- )
- }
- // @title yuexin-pay系统接口文档
- // @description 该后端主要用于对接支付接口
- // @host localhost:12345
- // @BasePath /yuexin-pay
- // @version v20240226
- func main() {
- c_log.GlobalLogger.Info("配置文件为:", ApplicationYaml)
- // 创建 gin 实例
- router := gin.Default()
- // 使用中间件
- router.Use(middleware.LogRequest()) // 请求日志打印
- router.Use(middleware.ValidateHeaders(ApplicationYaml.Web.WhiteList, ApplicationYaml.Web.Token)) // 全局请求头校验
- // 通过路由组设置全局前缀
- projectPrefix := router.Group(ApplicationYaml.Web.RoutePrefix)
- // 接口
- projectPrefix.POST("/page", handler.Hello) // 分页查询
- projectPrefix.POST("/pdf", handler.Hello) // pdf下载
- // 端口
- err := router.Run(":" + ApplicationYaml.Web.Port)
- if err != nil {
- c_log.GlobalLogger.Error("程序崩溃,监听端口 "+ApplicationYaml.Web.Port+" 失败:", err)
- os.Exit(-1)
- }
- }
|