feat: allow change minio protocol and download url (#357)

* feat: allow change minio protocol and download url

Signed-off-by: ztelliot <ztell@foxmail.com>

* feat: choice whether to log to file

Signed-off-by: ztelliot <ztell@foxmail.com>

---------

Signed-off-by: ztelliot <ztell@foxmail.com>
pull/388/head
ztelliot 1 year ago committed by GitHub
parent b62349dff5
commit 5089665048
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -84,6 +84,7 @@ func NewConfig() *Config {
initDirectory(conf) initDirectory(conf)
mode = conf.Sonic.Mode mode = conf.Sonic.Mode
logMode = conf.Sonic.LogMode
return conf return conf
} }
@ -104,7 +105,19 @@ func initDirectory(conf *Config) {
} }
var mode string var mode string
var logMode LogMode
func IsDev() bool { func IsDev() bool {
return mode == "development" return mode == "development"
} }
func LogToConsole() bool {
switch logMode {
case Console:
return true
case File:
return false
default:
return IsDev()
}
}

@ -41,8 +41,16 @@ type Levels struct {
Gorm string `mapstructure:"gorm"` Gorm string `mapstructure:"gorm"`
} }
type LogMode string
const (
Console LogMode = "console"
File LogMode = "file"
)
type Sonic struct { type Sonic struct {
Mode string `mapstructure:"mode"` Mode string `mapstructure:"mode"`
LogMode LogMode `mapstructure:"log_mode"`
WorkDir string `mapstructure:"work_dir"` WorkDir string `mapstructure:"work_dir"`
UploadDir string UploadDir string
LogDir string `mapstructure:"log_dir"` LogDir string `mapstructure:"log_dir"`

@ -28,7 +28,7 @@ func NewGormLogger(conf *config.Config, zapLogger *zap.Logger) logger.Interface
SlowThreshold: 200 * time.Millisecond, SlowThreshold: 200 * time.Millisecond,
LogLevel: GetGormLogLevel(conf.Log.Levels.Gorm), LogLevel: GetGormLogLevel(conf.Log.Levels.Gorm),
IgnoreRecordNotFoundError: true, IgnoreRecordNotFoundError: true,
Colorful: config.IsDev(), Colorful: config.LogToConsole(),
} }
gl := &gormLogger{ gl := &gormLogger{
Config: logConfig, Config: logConfig,

@ -14,7 +14,7 @@ import (
func NewLogger(conf *config.Config) *zap.Logger { func NewLogger(conf *config.Config) *zap.Logger {
_, err := os.Stat(conf.Sonic.LogDir) _, err := os.Stat(conf.Sonic.LogDir)
if err != nil { if err != nil {
if os.IsNotExist(err) && !config.IsDev() { if os.IsNotExist(err) && !config.LogToConsole() {
err := os.MkdirAll(conf.Sonic.LogDir, os.ModePerm) err := os.MkdirAll(conf.Sonic.LogDir, os.ModePerm)
if err != nil { if err != nil {
panic("mkdir failed![%v]") panic("mkdir failed![%v]")
@ -24,7 +24,7 @@ func NewLogger(conf *config.Config) *zap.Logger {
var core zapcore.Core var core zapcore.Core
if config.IsDev() { if config.LogToConsole() {
core = zapcore.NewCore(getDevEncoder(), os.Stdout, getLogLevel(conf.Log.Levels.App)) core = zapcore.NewCore(getDevEncoder(), os.Stdout, getLogLevel(conf.Log.Levels.App))
} else { } else {
core = zapcore.NewCore(getProdEncoder(), getWriter(conf), zap.DebugLevel) core = zapcore.NewCore(getProdEncoder(), getWriter(conf), zap.DebugLevel)

@ -54,6 +54,12 @@ var MinioAccessSecret = Property{
Kind: reflect.String, Kind: reflect.String,
} }
var MinioProtocol = Property{
DefaultValue: "https://",
KeyValue: "minio_protocol",
Kind: reflect.String,
}
var MinioSource = Property{ var MinioSource = Property{
DefaultValue: "", DefaultValue: "",
KeyValue: "minio_source", KeyValue: "minio_source",
@ -66,6 +72,12 @@ var MinioRegion = Property{
Kind: reflect.String, Kind: reflect.String,
} }
var MinioFrontBase = Property{
DefaultValue: "",
KeyValue: "minio_front_base",
Kind: reflect.String,
}
var AliOssEndpoint = Property{ var AliOssEndpoint = Property{
DefaultValue: "", DefaultValue: "",
KeyValue: "oss_ali_endpoint", KeyValue: "oss_ali_endpoint",

@ -101,8 +101,10 @@ var AllProperty = []Property{
MinioBucketName, MinioBucketName,
MinioAccessKey, MinioAccessKey,
MinioAccessSecret, MinioAccessSecret,
MinioProtocol,
MinioSource, MinioSource,
MinioRegion, MinioRegion,
MinioFrontBase,
AliOssEndpoint, AliOssEndpoint,
AliOssBucketName, AliOssBucketName,
AliOssAccessKey, AliOssAccessKey,

@ -61,8 +61,10 @@ func (c *clientOptionServiceImpl) getPrivateOption() map[string]struct{} {
property.MinioBucketName, property.MinioBucketName,
property.MinioAccessKey, property.MinioAccessKey,
property.MinioAccessSecret, property.MinioAccessSecret,
property.MinioProtocol,
property.MinioSource, property.MinioSource,
property.MinioRegion, property.MinioRegion,
property.MinioFrontBase,
property.AliOssEndpoint, property.AliOssEndpoint,
property.AliOssBucketName, property.AliOssBucketName,
property.AliOssAccessKey, property.AliOssAccessKey,

@ -32,6 +32,8 @@ type minioClient struct {
BucketName string BucketName string
Source string Source string
EndPoint string EndPoint string
Protocol string
FrontBase string
} }
func (m *MinIO) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (*dto.AttachmentDTO, error) { 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( fd, err := newURLFileDescriptor(
withBaseURL(minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName), withBaseURL(minioClientInstance.Protocol+minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName),
withSubURLPath(minioClientInstance.Source), withSubURLPath(minioClientInstance.Source),
withShouldRenameURLOption(commonRenamePredicateFunc(ctx, consts.AttachmentTypeMinIO)), withShouldRenameURLOption(commonRenamePredicateFunc(ctx, consts.AttachmentTypeMinIO)),
withOriginalNameURLOption(fileHeader.Filename), withOriginalNameURLOption(fileHeader.Filename),
@ -103,14 +105,17 @@ func (m *MinIO) GetFilePath(ctx context.Context, relativePath string) (string, e
if err != nil { if err != nil {
return "", err 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.JoinPath(base, relativePath)
fullPath, _ = url.PathUnescape(fullPath) fullPath, _ = url.PathUnescape(fullPath)
return fullPath, nil return fullPath, nil
} }
func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) { 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 { if e != nil {
return e return e
} }
@ -122,25 +127,37 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
if !ok { if !ok {
return xerr.WithStatus(nil, xerr.StatusBadRequest).WithErrMsgf("wrong property type") 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) return xerr.WithStatus(nil, xerr.StatusInternalServerError).WithMsg("property not found: " + property.KeyValue)
} }
*propertyValue = strValue *propertyValue = strValue
return nil return nil
} }
var endPoint, bucketName, accessKey, accessSecret, source, region string var endPoint, bucketName, accessKey, accessSecret, protocol, source, region, frontBase string
err := getClientProperty(&endPoint, property.MinioEndpoint, nil) err := getClientProperty(&endPoint, property.MinioEndpoint, false, nil)
err = getClientProperty(&bucketName, property.MinioBucketName, err) err = getClientProperty(&bucketName, property.MinioBucketName, false, err)
err = getClientProperty(&accessKey, property.MinioAccessKey, err) err = getClientProperty(&accessKey, property.MinioAccessKey, false, err)
err = getClientProperty(&accessSecret, property.MinioAccessSecret, err) err = getClientProperty(&accessSecret, property.MinioAccessSecret, false, err)
err = getClientProperty(&source, property.MinioSource, err) err = getClientProperty(&protocol, property.MinioProtocol, false, err)
err = getClientProperty(&region, property.MinioRegion, err) err = getClientProperty(&source, property.MinioSource, true, err)
err = getClientProperty(&region, property.MinioRegion, true, err)
err = getClientProperty(&frontBase, property.MinioFrontBase, true, err)
if err != nil { if err != nil {
return nil, err 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{ client, err := minio.New(endPoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, accessSecret, ""), Creds: credentials.NewStaticV4(accessKey, accessSecret, ""),
Secure: true, Secure: secure,
Region: region, Region: region,
}) })
if err != nil { if err != nil {
@ -153,5 +170,7 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
minioClientInstance.BucketName = bucketName minioClientInstance.BucketName = bucketName
minioClientInstance.Source = source minioClientInstance.Source = source
minioClientInstance.EndPoint = endPoint minioClientInstance.EndPoint = endPoint
minioClientInstance.Protocol = protocol
minioClientInstance.FrontBase = frontBase
return minioClientInstance, nil return minioClientInstance, nil
} }

Loading…
Cancel
Save