Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow change minio protocol and download url #357

Merged
merged 3 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func NewConfig() *Config {

initDirectory(conf)
mode = conf.Sonic.Mode
logMode = conf.Sonic.LogMode
return conf
}

Expand All @@ -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()
}
}
12 changes: 10 additions & 2 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion log/gorm_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions log/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions model/property/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions model/property/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ var AllProperty = []Property{
MinioBucketName,
MinioAccessKey,
MinioAccessSecret,
MinioProtocol,
MinioSource,
MinioRegion,
MinioFrontBase,
AliOssEndpoint,
AliOssBucketName,
AliOssAccessKey,
Expand Down
2 changes: 2 additions & 0 deletions service/impl/client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 31 additions & 12 deletions service/storage/impl/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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