52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package tianyuanapi
|
|
|
|
import (
|
|
"time"
|
|
|
|
"hyapi-server/internal/config"
|
|
"hyapi-server/internal/shared/external_logger"
|
|
)
|
|
|
|
// NewTianyuanapiServiceWithConfig 使用配置创建天远 API 服务
|
|
func NewTianyuanapiServiceWithConfig(cfg *config.Config) (*TianyuanapiService, error) {
|
|
loggingConfig := external_logger.ExternalServiceLoggingConfig{
|
|
Enabled: cfg.Tianyuanapi.Logging.Enabled,
|
|
LogDir: cfg.Tianyuanapi.Logging.LogDir,
|
|
ServiceName: "tianyuanapi",
|
|
UseDaily: cfg.Tianyuanapi.Logging.UseDaily,
|
|
EnableLevelSeparation: cfg.Tianyuanapi.Logging.EnableLevelSeparation,
|
|
LevelConfigs: make(map[string]external_logger.ExternalServiceLevelFileConfig),
|
|
}
|
|
for k, v := range cfg.Tianyuanapi.Logging.LevelConfigs {
|
|
loggingConfig.LevelConfigs[k] = external_logger.ExternalServiceLevelFileConfig{
|
|
MaxSize: v.MaxSize,
|
|
MaxBackups: v.MaxBackups,
|
|
MaxAge: v.MaxAge,
|
|
Compress: v.Compress,
|
|
}
|
|
}
|
|
|
|
var logger *external_logger.ExternalServiceLogger
|
|
var err error
|
|
if loggingConfig.Enabled {
|
|
logger, err = external_logger.NewExternalServiceLogger(loggingConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
timeout := 30 * time.Second
|
|
if cfg.Tianyuanapi.Timeout > 0 {
|
|
timeout = cfg.Tianyuanapi.Timeout
|
|
}
|
|
|
|
serviceCfg := TianyuanapiConfig{
|
|
BaseURL: cfg.Tianyuanapi.BaseURL,
|
|
AccessID: cfg.Tianyuanapi.AccessID,
|
|
EncryptionKey: cfg.Tianyuanapi.EncryptionKey,
|
|
Timeout: timeout,
|
|
}
|
|
|
|
return NewTianyuanapiService(serviceCfg, logger), nil
|
|
}
|