main.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "cicv-data-closedloop/common/config/c_db"
  4. "cicv-data-closedloop/common/config/c_log"
  5. "cicv-data-closedloop/common/gin/handler"
  6. "cicv-data-closedloop/common/gin/middleware"
  7. _ "embed"
  8. "github.com/gin-gonic/gin"
  9. "gopkg.in/yaml.v3"
  10. "os"
  11. )
  12. type ApplicationYamlStruct struct {
  13. ApplicationName string `yaml:"application"`
  14. Web struct {
  15. Port string `yaml:"port"`
  16. RoutePrefix string `yaml:"route-prefix"`
  17. Token string `yaml:"token"`
  18. WhiteList []string `yaml:"white-list"`
  19. } `yaml:"web"`
  20. Log struct {
  21. Dir string `yaml:"dir"`
  22. Prefix string `yaml:"prefix"`
  23. } `yaml:"log"`
  24. Mysql struct {
  25. Ip string `yaml:"ip"`
  26. Port string `yaml:"port"`
  27. Username string `yaml:"username"`
  28. Password string `yaml:"password"`
  29. Dbname string `yaml:"dbname"`
  30. Charset string `yaml:"charset"`
  31. } `yaml:"mysql"`
  32. }
  33. var (
  34. //go:embed application.yaml
  35. applicationYamlBytes []byte
  36. ApplicationYaml ApplicationYamlStruct
  37. )
  38. func init() {
  39. // 解析YAML内容
  40. _ = yaml.Unmarshal(applicationYamlBytes, &ApplicationYaml)
  41. c_log.InitLog(
  42. ApplicationYaml.Log.Dir,
  43. ApplicationYaml.Log.Prefix,
  44. )
  45. c_db.InitSqlxMysql(
  46. ApplicationYaml.Mysql.Ip,
  47. ApplicationYaml.Mysql.Port,
  48. ApplicationYaml.Mysql.Username,
  49. ApplicationYaml.Mysql.Password,
  50. ApplicationYaml.Mysql.Dbname,
  51. ApplicationYaml.Mysql.Charset,
  52. )
  53. }
  54. // @title yuexin-pay系统接口文档
  55. // @description 该后端主要用于对接支付接口
  56. // @host localhost:12345
  57. // @BasePath /yuexin-pay
  58. // @version v20240226
  59. func main() {
  60. c_log.GlobalLogger.Info("配置文件为:", ApplicationYaml)
  61. // 创建 gin 实例
  62. router := gin.Default()
  63. // 使用中间件
  64. router.Use(middleware.LogRequest()) // 请求日志打印
  65. router.Use(middleware.ValidateHeaders(ApplicationYaml.Web.WhiteList, ApplicationYaml.Web.Token)) // 全局请求头校验
  66. // 通过路由组设置全局前缀
  67. projectPrefix := router.Group(ApplicationYaml.Web.RoutePrefix)
  68. // 接口
  69. projectPrefix.POST("/page", handler.Hello) // 分页查询
  70. projectPrefix.POST("/pdf", handler.Hello) // pdf下载
  71. // 端口
  72. err := router.Run(":" + ApplicationYaml.Web.Port)
  73. if err != nil {
  74. c_log.GlobalLogger.Error("程序崩溃,监听端口 "+ApplicationYaml.Web.Port+" 失败:", err)
  75. os.Exit(-1)
  76. }
  77. }