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)
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()
}
}

@ -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"`

@ -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,

@ -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)

@ -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",

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

@ -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,

@ -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(&region, 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(&region, 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
}

Loading…
Cancel
Save