Files

102 lines
2.7 KiB
Go

// Package initial is the package that starts the service to initialize the service, including
// the initialization configuration, service configuration, connecting to the database, and
// resource release needed when shutting down the service.
package initial
import (
"flag"
"strconv"
"github.com/go-dev-frame/sponge/pkg/logger"
"github.com/go-dev-frame/sponge/pkg/stat"
"github.com/go-dev-frame/sponge/pkg/tracer"
"github.com/go-dev-frame/sponge/configs"
"github.com/go-dev-frame/sponge/internal/config"
//"github.com/go-dev-frame/sponge/internal/database"
)
var (
version string
configFile string
)
// InitApp initial app configuration
func InitApp() {
initConfig()
cfg := config.Get()
// initializing log
_, err := logger.Init(
logger.WithLevel(cfg.Logger.Level),
logger.WithFormat(cfg.Logger.Format),
logger.WithSave(
cfg.Logger.IsSave,
//logger.WithFileName(cfg.Logger.LogFileConfig.Filename),
//logger.WithFileMaxSize(cfg.Logger.LogFileConfig.MaxSize),
//logger.WithFileMaxBackups(cfg.Logger.LogFileConfig.MaxBackups),
//logger.WithFileMaxAge(cfg.Logger.LogFileConfig.MaxAge),
//logger.WithFileIsCompression(cfg.Logger.LogFileConfig.IsCompression),
),
)
if err != nil {
panic(err)
}
logger.Debug(config.Show())
logger.Info("[logger] was initialized")
// initializing tracing
if cfg.App.EnableTrace {
tracer.InitWithConfig(
cfg.App.Name,
cfg.App.Env,
cfg.App.Version,
cfg.Jaeger.AgentHost,
strconv.Itoa(cfg.Jaeger.AgentPort),
cfg.App.TracingSamplingRate,
)
logger.Info("[tracer] was initialized")
}
// initializing the print system and process resources
if cfg.App.EnableStat {
stat.Init(
stat.WithLog(logger.Get()),
stat.WithAlarm(), // invalid if it is windows, the default threshold for cpu and memory is 0.8, you can modify them
stat.WithPrintField(logger.String("service_name", cfg.App.Name), logger.String("host", cfg.App.Host)),
)
logger.Info("[resource statistics] was initialized")
}
// initializing database
//database.InitDB()
//logger.Infof("[%s] was initialized", cfg.Database.Driver)
//database.InitCache(cfg.App.CacheType)
//if cfg.App.CacheType != "" {
// logger.Infof("[%s] was initialized", cfg.App.CacheType)
//}
}
func initConfig() {
flag.StringVar(&version, "version", "", "service Version Number")
flag.StringVar(&configFile, "c", "", "configuration file")
flag.Parse()
getConfigFromLocal()
if version != "" {
config.Get().App.Version = version
}
}
// get configuration from local configuration file
func getConfigFromLocal() {
if configFile == "" {
configFile = configs.Location("serverNameExample.yml")
}
err := config.Init(configFile)
if err != nil {
panic("init config error: " + err.Error())
}
}