EasyGoAdmin_Gin_EleVue-1 发表于 2023-07-12 | 更新于 2024-09-02 | 分类于 Gin 本文字数: 22k | 阅读时长 ≈ 20 分钟 思考并回答以下问题: 源代码 目录 1234567891011121314151617181920├── app // 应用目录│ ├── controller // 控制器│ ├── dto // DTO层│ ├── middleware // 中间件层│ ├── model // 模型层│ └── service // 服务层│ └── vo // VO视图层├── boot├── config // 系统配置├── document // 文档目录├── i18n // 国际化├── library // 类库├── packed├── public // 资源目录├── router // 路由├── templates // 自定义模板├── utils // 工具类库├── Dockerfile├── go.mod└── main.go main.go123456789101112131415161718192021222324252627/** * 系统入口文件 * @File : main */package mainimport ( _ "easygoadmin/boot" "easygoadmin/library/cfg" _ "easygoadmin/router" _ "easygoadmin/utils" "fmt" "github.com/gin-gonic/gin")func main() { // 开始调试模式 gin.SetMode("debug") // 实例化配置 config := cfg.Instance() if config == nil { fmt.Printf("参数错误") return }} library library/cfg/cfg.go 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859/** * 配置工具 * @File : cfg */package cfgimport ( "github.com/BurntSushi/toml" "os" "sync")var ( instance *config once sync.Once)// 获取配置文档实例func Instance() *config { once.Do(func() { var conf config path, _ := os.Getwd() filePath := path + "/config/config.toml" if _, err := toml.DecodeFile(filePath, &conf); err != nil { return } instance = &conf }) return instance}type config struct { Database database Logger logger EasyGoAdmin easygoadmin}type database struct { Master string Slave string Debug bool Log string}type logger struct { Path string Level uint32 Stdout bool}// 自定义配置type easygoadmin struct { Version string Debug bool Image string Uploads string} library/db.go 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192package libraryimport ( "easygoadmin/library/cfg" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "strings" "sync" "xorm.io/core")type dbEngine struct { master *xorm.Engine //主数据库 slave *xorm.Engine //从数据库}var ( instance *dbEngine once sync.Once)//初始化数据操作 driver为数据库类型func Instance(driver ...string) *dbEngine { once.Do(func() { driverName := "mysql" if len(driver) > 0 { driverName = driver[0] } var db dbEngine config := cfg.Instance() //没有配置从数据库 if len(config.Database.Slave) == 0 { engine, err := xorm.NewEngine(driverName, config.Database.Master) if err != nil { fmt.Printf("数据库连接错误:%v", err.Error()) return } err = engine.Ping() if err != nil { fmt.Printf("数据库连接错误:%v", err.Error()) return } if cfg.Instance().Database.Debug { engine.ShowSQL(cfg.Instance().Database.Debug) engine.Logger().SetLevel(core.LOG_DEBUG) } db.master = engine instance = &db } else { master, err := xorm.NewEngine(driverName, config.Database.Master) if err != nil { return } if cfg.Instance().Database.Debug { master.ShowSQL(cfg.Instance().Database.Debug) master.Logger().SetLevel(core.LOG_DEBUG) } slave, err := xorm.NewEngine(driverName, config.Database.Slave) if err != nil { return } slaves := []*xorm.Engine{slave} group, err := xorm.NewEngineGroup(master, slaves) if cfg.Instance().Database.Debug { group.ShowSQL(cfg.Instance().Database.Debug) group.Logger().SetLevel(core.LOG_DEBUG) } db.master = group.Master() db.slave = group.Slave() instance = &db } }) return instance}//获取操作实例 如果传入slave 并且成功配置了slave 返回slave orm引擎 否则返回master orm引擎func (db *dbEngine) Engine(dbType ...string) *xorm.Engine { if dbType != nil && len(dbType) > 0 { if strings.EqualFold(dbType[0], "slave") { if db.slave != nil { return db.slave } } } return db.master}