feat: support configurable timezone

This commit is contained in:
Paul Pan 2023-12-22 15:23:24 +08:00
parent eb6f5d0aca
commit dc50562f22
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
5 changed files with 7 additions and 3 deletions

View File

@ -21,6 +21,7 @@ Database:
MaxOpenConns: ${DATABASE_MAX_OPEN_CONNS}
MaxIdleConns: ${DATABASE_MAX_IDLE_CONNS}
ConnMaxLifetime: ${DATABASE_CONN_MAX_LIFETIME}
TimeZone: ${DATABASE_TIMEZONE}
Storage:
Endpoint: ${STORAGE_ENDPOINT}

View File

@ -54,6 +54,7 @@ check_env "DATABASE_PREFIX" "oj_" true
check_env "DATABASE_MAX_OPEN_CONNS" 100 false
check_env "DATABASE_MAX_IDLE_CONNS" 60 false
check_env "DATABASE_CONN_MAX_LIFETIME" 60 false
check_env "DATABASE_TIMEZONE" "Asia/Shanghai" true
check_env "STORAGE_ENDPOINT" "minio:9000" true
check_env "STORAGE_USE_SSL" "false" false

View File

@ -25,6 +25,7 @@ type ConfigDatabase struct {
MaxOpenConns int `yaml:"MaxOpenConns"`
MaxIdleConns int `yaml:"MaxIdleConns"`
ConnMaxLifetime int `yaml:"ConnMaxLifetime"`
TimeZone string `yaml:"TimeZone"`
}
type ConfigStorage struct {

View File

@ -66,14 +66,15 @@ func (s *service) setup(conf *model.Config) {
logger := zapgorm2.New(s.log)
logger.IgnoreRecordNotFoundError = true
tz := utils.If(conf.Database.TimeZone == "", "Asia/Shanghai", conf.Database.TimeZone)
dsn := fmt.Sprintf(
// TODO: timezone as config
"user=%s password=%s dbname=%s host=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
"user=%s password=%s dbname=%s host=%s port=%d sslmode=disable TimeZone=%s",
conf.Database.User,
conf.Database.Password,
conf.Database.Database,
conf.Database.Host,
conf.Database.Port,
tz,
)
s.db, s.err = gorm.Open(

View File

@ -54,7 +54,7 @@ func (s *service) HealthCheck() error {
}
func (s *service) initRouters(conf *model.Config, injector *do.Injector) *gin.Engine {
gin.SetMode(utils.If(conf.Development, gin.DebugMode, gin.ReleaseMode))
gin.SetMode(utils.If[string](conf.Development, gin.DebugMode, gin.ReleaseMode))
r := gin.New()
r.MaxMultipartMemory = 8 << 20