sys_init.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package request
  2. import (
  3. "fmt"
  4. "log-server/config"
  5. )
  6. type InitDB struct {
  7. DBType string `json:"dbType"` // 数据库类型
  8. Host string `json:"host"` // 服务器地址
  9. Port string `json:"port"` // 数据库连接端口
  10. UserName string `json:"userName" binding:"required"` // 数据库用户名
  11. Password string `json:"password"` // 数据库密码
  12. DBName string `json:"dbName" binding:"required"` // 数据库名
  13. }
  14. // MysqlEmptyDsn msyql 空数据库 建库链接
  15. // Author SliverHorn
  16. func (i *InitDB) MysqlEmptyDsn() string {
  17. if i.Host == "" {
  18. i.Host = "127.0.0.1"
  19. }
  20. if i.Port == "" {
  21. i.Port = "3306"
  22. }
  23. return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
  24. }
  25. // PgsqlEmptyDsn pgsql 空数据库 建库链接
  26. // Author SliverHorn
  27. func (i *InitDB) PgsqlEmptyDsn() string {
  28. if i.Host == "" {
  29. i.Host = "127.0.0.1"
  30. }
  31. if i.Port == "" {
  32. i.Port = "5432"
  33. }
  34. return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " dbname=" + "postgres" + " " + "sslmode=disable TimeZone=Asia/Shanghai"
  35. }
  36. // ToMysqlConfig 转换 config.Mysql
  37. // Author [SliverHorn](https://github.com/SliverHorn)
  38. func (i *InitDB) ToMysqlConfig() config.Mysql {
  39. return config.Mysql{
  40. GeneralDB: config.GeneralDB{
  41. Path: i.Host,
  42. Port: i.Port,
  43. Dbname: i.DBName,
  44. Username: i.UserName,
  45. Password: i.Password,
  46. MaxIdleConns: 10,
  47. MaxOpenConns: 100,
  48. LogMode: "error",
  49. Config: "charset=utf8mb4&parseTime=True&loc=Local",
  50. },
  51. }
  52. }
  53. // ToPgsqlConfig 转换 config.Pgsql
  54. // Author [SliverHorn](https://github.com/SliverHorn)
  55. func (i *InitDB) ToPgsqlConfig() config.Pgsql {
  56. return config.Pgsql{
  57. GeneralDB: config.GeneralDB{
  58. Path: i.Host,
  59. Port: i.Port,
  60. Dbname: i.DBName,
  61. Username: i.UserName,
  62. Password: i.Password,
  63. MaxIdleConns: 10,
  64. MaxOpenConns: 100,
  65. LogMode: "error",
  66. Config: "sslmode=disable TimeZone=Asia/Shanghai",
  67. },
  68. }
  69. }