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.
paopao-ce/init.go

128 lines
2.7 KiB

3 years ago
package main
import (
"flag"
3 years ago
"log"
"strings"
3 years ago
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/rocboss/paopao-ce/global"
"github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/logger"
"github.com/rocboss/paopao-ce/pkg/setting"
"github.com/rocboss/paopao-ce/pkg/zinc"
3 years ago
)
var (
noDefaultFeatures bool
features suites
)
type suites []string
func (s *suites) String() string {
return strings.Join(*s, ",")
}
func (s *suites) Set(value string) error {
for _, item := range strings.Split(value, ",") {
*s = append(*s, strings.TrimSpace(item))
}
return nil
}
3 years ago
func init() {
flagParse()
3 years ago
err := setupSetting()
if err != nil {
log.Fatalf("init.setupSetting err: %v", err)
}
3 years ago
err = setupLogger()
if err != nil {
log.Fatalf("init.setupLogger err: %v", err)
}
3 years ago
err = setupDBEngine()
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
client := zinc.NewClient(global.ZincSetting)
service.Initialize(global.DBEngine, client)
3 years ago
}
func setupSetting() error {
setting, err := setting.NewSetting()
if err != nil {
return err
}
global.Features = setting.FeaturesFrom("Features")
if len(features) > 0 {
if err = global.Features.Use(features, noDefaultFeatures); err != nil {
return err
}
}
objects := map[string]interface{}{
"App": &global.AppSetting,
"Server": &global.ServerSetting,
"Alipay": &global.AlipaySetting,
"SmsJuhe": &global.SmsJuheSetting,
"LoggerFile": &global.LoggerFileSetting,
"LoggerZinc": &global.LoggerZincSetting,
"MySQL": &global.MySQLSetting,
"Zinc": &global.ZincSetting,
"Redis": &global.RedisSetting,
"JWT": &global.JWTSetting,
"AliOSS": &global.AliOSSSetting,
3 years ago
}
if err = setting.Unmarshal(objects); err != nil {
3 years ago
return err
}
global.JWTSetting.Expire *= time.Second
global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second
global.Mutex = &sync.Mutex{}
return nil
}
func flagParse() {
flag.BoolVar(&noDefaultFeatures, "no-default-features", false, "whether use default features")
flag.Var(&features, "features", "use special features")
flag.Parse()
}
3 years ago
func setupLogger() error {
logger, err := logger.New()
3 years ago
if err != nil {
return err
}
global.Logger = logger
return nil
}
// setupDBEngine 暂时只支持MySQL
3 years ago
func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.MySQLSetting)
3 years ago
if err != nil {
return err
}
global.Redis = redis.NewClient(&redis.Options{
Addr: global.RedisSetting.Host,
Password: global.RedisSetting.Password,
DB: global.RedisSetting.DB,
})
return nil
}