You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.0 KiB
53 lines
1.0 KiB
2 months ago
|
package util
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"ims/util/cache"
|
||
|
"ims/util/db"
|
||
|
"ims/util/jwt"
|
||
|
"ims/util/rdb"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/shopspring/decimal"
|
||
|
"zestack.dev/env"
|
||
|
)
|
||
|
|
||
|
func Init(_ context.Context) error {
|
||
|
// 设置 decimal 的序列化结果为不带引号的字符串,
|
||
|
// 也就是 json 数字为非字符串。
|
||
|
decimal.MarshalJSONWithoutQuotes = true
|
||
|
|
||
|
var err error
|
||
|
call := func(fn func() error) {
|
||
|
if err == nil {
|
||
|
err = fn()
|
||
|
}
|
||
|
}
|
||
|
call(initTimeZone)
|
||
|
call(db.Init)
|
||
|
call(rdb.Init)
|
||
|
call(cache.Init)
|
||
|
call(jwt.Init)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 初始化时区
|
||
|
func initTimeZone() error {
|
||
|
timeZone := env.String("TIME_ZONE", "Asia/Shanghai")
|
||
|
if tzdata := env.String("TZDATA_PATH"); tzdata != "" {
|
||
|
if data, err := os.ReadFile(tzdata); err != nil {
|
||
|
return err
|
||
|
} else if local, err := time.LoadLocationFromTZData(timeZone, data); err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
time.Local = local
|
||
|
}
|
||
|
} else if local, err := time.LoadLocation(timeZone); err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
time.Local = local
|
||
|
}
|
||
|
return nil
|
||
|
}
|