diff --git a/config/config.go b/config/config.go index 7148dc9..1e5f9ae 100644 --- a/config/config.go +++ b/config/config.go @@ -84,6 +84,7 @@ func NewConfig() *Config { initDirectory(conf) mode = conf.Sonic.Mode + logMode = conf.Sonic.LogMode return conf } @@ -104,7 +105,19 @@ func initDirectory(conf *Config) { } var mode string +var logMode LogMode func IsDev() bool { return mode == "development" } + +func LogToConsole() bool { + switch logMode { + case Console: + return true + case File: + return false + default: + return IsDev() + } +} diff --git a/config/model.go b/config/model.go index 44ec967..1b5d0eb 100644 --- a/config/model.go +++ b/config/model.go @@ -41,9 +41,17 @@ type Levels struct { Gorm string `mapstructure:"gorm"` } +type LogMode string + +const ( + Console LogMode = "console" + File LogMode = "file" +) + type Sonic struct { - Mode string `mapstructure:"mode"` - WorkDir string `mapstructure:"work_dir"` + Mode string `mapstructure:"mode"` + LogMode LogMode `mapstructure:"log_mode"` + WorkDir string `mapstructure:"work_dir"` UploadDir string LogDir string `mapstructure:"log_dir"` TemplateDir string `mapstructure:"template_dir"` diff --git a/log/gorm_logger.go b/log/gorm_logger.go index da3f08e..4b1dca0 100644 --- a/log/gorm_logger.go +++ b/log/gorm_logger.go @@ -28,7 +28,7 @@ func NewGormLogger(conf *config.Config, zapLogger *zap.Logger) logger.Interface SlowThreshold: 200 * time.Millisecond, LogLevel: GetGormLogLevel(conf.Log.Levels.Gorm), IgnoreRecordNotFoundError: true, - Colorful: config.IsDev(), + Colorful: config.LogToConsole(), } gl := &gormLogger{ Config: logConfig, diff --git a/log/init.go b/log/init.go index 8edb631..c384891 100644 --- a/log/init.go +++ b/log/init.go @@ -14,7 +14,7 @@ import ( func NewLogger(conf *config.Config) *zap.Logger { _, err := os.Stat(conf.Sonic.LogDir) if err != nil { - if os.IsNotExist(err) && !config.IsDev() { + if os.IsNotExist(err) && !config.LogToConsole() { err := os.MkdirAll(conf.Sonic.LogDir, os.ModePerm) if err != nil { panic("mkdir failed![%v]") @@ -24,7 +24,7 @@ func NewLogger(conf *config.Config) *zap.Logger { var core zapcore.Core - if config.IsDev() { + if config.LogToConsole() { core = zapcore.NewCore(getDevEncoder(), os.Stdout, getLogLevel(conf.Log.Levels.App)) } else { core = zapcore.NewCore(getProdEncoder(), getWriter(conf), zap.DebugLevel) diff --git a/model/property/attachment.go b/model/property/attachment.go index fe43912..85a4b8a 100644 --- a/model/property/attachment.go +++ b/model/property/attachment.go @@ -54,6 +54,12 @@ var MinioAccessSecret = Property{ Kind: reflect.String, } +var MinioProtocol = Property{ + DefaultValue: "https://", + KeyValue: "minio_protocol", + Kind: reflect.String, +} + var MinioSource = Property{ DefaultValue: "", KeyValue: "minio_source", @@ -66,6 +72,12 @@ var MinioRegion = Property{ Kind: reflect.String, } +var MinioFrontBase = Property{ + DefaultValue: "", + KeyValue: "minio_front_base", + Kind: reflect.String, +} + var AliOssEndpoint = Property{ DefaultValue: "", KeyValue: "oss_ali_endpoint", diff --git a/model/property/base.go b/model/property/base.go index 56b0b03..cc43cb4 100644 --- a/model/property/base.go +++ b/model/property/base.go @@ -101,8 +101,10 @@ var AllProperty = []Property{ MinioBucketName, MinioAccessKey, MinioAccessSecret, + MinioProtocol, MinioSource, MinioRegion, + MinioFrontBase, AliOssEndpoint, AliOssBucketName, AliOssAccessKey, diff --git a/service/impl/client_option.go b/service/impl/client_option.go index 14fccb4..2800aee 100644 --- a/service/impl/client_option.go +++ b/service/impl/client_option.go @@ -61,8 +61,10 @@ func (c *clientOptionServiceImpl) getPrivateOption() map[string]struct{} { property.MinioBucketName, property.MinioAccessKey, property.MinioAccessSecret, + property.MinioProtocol, property.MinioSource, property.MinioRegion, + property.MinioFrontBase, property.AliOssEndpoint, property.AliOssBucketName, property.AliOssAccessKey, diff --git a/service/storage/impl/minio.go b/service/storage/impl/minio.go index efaf2af..a4c9d05 100644 --- a/service/storage/impl/minio.go +++ b/service/storage/impl/minio.go @@ -32,6 +32,8 @@ type minioClient struct { BucketName string Source string EndPoint string + Protocol string + FrontBase string } func (m *MinIO) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (*dto.AttachmentDTO, error) { @@ -41,7 +43,7 @@ func (m *MinIO) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (* } fd, err := newURLFileDescriptor( - withBaseURL(minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName), + withBaseURL(minioClientInstance.Protocol+minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName), withSubURLPath(minioClientInstance.Source), withShouldRenameURLOption(commonRenamePredicateFunc(ctx, consts.AttachmentTypeMinIO)), withOriginalNameURLOption(fileHeader.Filename), @@ -103,14 +105,17 @@ func (m *MinIO) GetFilePath(ctx context.Context, relativePath string) (string, e if err != nil { return "", err } - base := minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName + base := minioClientInstance.Protocol + minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName + if minioClientInstance.FrontBase != "" { + base = minioClientInstance.FrontBase + } fullPath, _ := url.JoinPath(base, relativePath) fullPath, _ = url.PathUnescape(fullPath) return fullPath, nil } func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) { - getClientProperty := func(propertyValue *string, property property.Property, e error) error { + getClientProperty := func(propertyValue *string, property property.Property, allowEmpty bool, e error) error { if e != nil { return e } @@ -122,25 +127,37 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) { if !ok { return xerr.WithStatus(nil, xerr.StatusBadRequest).WithErrMsgf("wrong property type") } - if strValue == "" { + if !allowEmpty && strValue == "" { return xerr.WithStatus(nil, xerr.StatusInternalServerError).WithMsg("property not found: " + property.KeyValue) } *propertyValue = strValue return nil } - var endPoint, bucketName, accessKey, accessSecret, source, region string - err := getClientProperty(&endPoint, property.MinioEndpoint, nil) - err = getClientProperty(&bucketName, property.MinioBucketName, err) - err = getClientProperty(&accessKey, property.MinioAccessKey, err) - err = getClientProperty(&accessSecret, property.MinioAccessSecret, err) - err = getClientProperty(&source, property.MinioSource, err) - err = getClientProperty(®ion, property.MinioRegion, err) + var endPoint, bucketName, accessKey, accessSecret, protocol, source, region, frontBase string + err := getClientProperty(&endPoint, property.MinioEndpoint, false, nil) + err = getClientProperty(&bucketName, property.MinioBucketName, false, err) + err = getClientProperty(&accessKey, property.MinioAccessKey, false, err) + err = getClientProperty(&accessSecret, property.MinioAccessSecret, false, err) + err = getClientProperty(&protocol, property.MinioProtocol, false, err) + err = getClientProperty(&source, property.MinioSource, true, err) + err = getClientProperty(®ion, property.MinioRegion, true, err) + err = getClientProperty(&frontBase, property.MinioFrontBase, true, err) if err != nil { return nil, err } + secure := func() bool { + switch protocol { + case "https://": + return true + case "http://": + return false + default: + return true + } + }() client, err := minio.New(endPoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKey, accessSecret, ""), - Secure: true, + Secure: secure, Region: region, }) if err != nil { @@ -153,5 +170,7 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) { minioClientInstance.BucketName = bucketName minioClientInstance.Source = source minioClientInstance.EndPoint = endPoint + minioClientInstance.Protocol = protocol + minioClientInstance.FrontBase = frontBase return minioClientInstance, nil }