forked from free-lancers/electricity_bill_calc_service
		
	build(init):创建基本项目结构。
This commit is contained in:
		
							
								
								
									
										39
									
								
								global/db.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								global/db.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| package global | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"electricity_bill_calc/config" | ||||
|  | ||||
| 	"gorm.io/driver/postgres" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	DBConn *gorm.DB | ||||
| ) | ||||
|  | ||||
| func SetupDatabaseConnection() error { | ||||
| 	var err error | ||||
| 	DBConn, err = gorm.Open(postgres.New(postgres.Config{ | ||||
| 		DSN: fmt.Sprintf( | ||||
| 			"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", | ||||
| 			config.DatabaseSettings.Host, | ||||
| 			config.DatabaseSettings.User, | ||||
| 			config.DatabaseSettings.Pass, | ||||
| 			config.DatabaseSettings.DB, | ||||
| 			config.DatabaseSettings.Port), | ||||
| 	}), &gorm.Config{}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	db, err := DBConn.DB() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	db.SetMaxIdleConns(config.DatabaseSettings.MaxIdleConns) | ||||
| 	db.SetMaxOpenConns(config.DatabaseSettings.MaxOpenConns) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										26
									
								
								global/redis.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								global/redis.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| package global | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/config" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/go-redis/redis" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	RedisConn *redis.Client | ||||
| ) | ||||
|  | ||||
| func SetupRedisConnection() error { | ||||
| 	RedisConn = redis.NewClient(&redis.Options{ | ||||
| 		Addr:     fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port), | ||||
| 		Password: config.RedisSettings.Password, | ||||
| 		DB:       config.RedisSettings.DB, | ||||
| 	}) | ||||
|  | ||||
| 	_, err := RedisConn.Ping().Result() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user