Browse Source

添加全局变量保存使用的数据库连接数据

pull/6/head
zhangxin 3 years ago
parent
commit
a64002217d
3 changed files with 8 additions and 1 deletions
  1. +2
    -0
      global/global.go
  2. +4
    -1
      infra/db/mongo.go
  3. +2
    -0
      infra/db/mssql.go

+ 2
- 0
global/global.go View File

@ -7,6 +7,8 @@ import (
) )
var TimezoneLocation *time.Location var TimezoneLocation *time.Location
var DatabaseDsn string
var MongoDsn string
type TaskRecord struct { type TaskRecord struct {
TaskMap map[int]time.Time TaskMap map[int]time.Time


+ 4
- 1
infra/db/mongo.go View File

@ -1,6 +1,7 @@
package db package db
import ( import (
"LAPP_ETL/global"
"LAPP_ETL/infra/config" "LAPP_ETL/infra/config"
"context" "context"
"fmt" "fmt"
@ -14,7 +15,9 @@ var AppMongo *mongo.Client
func InitMongo() error { func InitMongo() error {
//1.建立链接 //1.建立链接
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", config.AppConfig.Mongo.Host, config.AppConfig.Mongo.Port)))
dsn := fmt.Sprintf("mongodb://%s:%d", config.AppConfig.Mongo.Host, config.AppConfig.Mongo.Port)
global.MongoDsn = dsn
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dsn))
if err != nil { if err != nil {
return err return err
} }


+ 2
- 0
infra/db/mssql.go View File

@ -1,6 +1,7 @@
package db package db
import ( import (
"LAPP_ETL/global"
"LAPP_ETL/infra/config" "LAPP_ETL/infra/config"
model "LAPP_ETL/models/etl" model "LAPP_ETL/models/etl"
"fmt" "fmt"
@ -17,6 +18,7 @@ func InitDB() error {
config.AppConfig.DB.User, config.AppConfig.DB.User,
config.AppConfig.DB.Password, config.AppConfig.DB.Password,
config.AppConfig.DB.Port) config.AppConfig.DB.Port)
global.DatabaseDsn = dsn
engine, err := xorm.NewEngine("mssql", dsn) engine, err := xorm.NewEngine("mssql", dsn)
if err != nil { if err != nil {
return err return err


Loading…
Cancel
Save