Skip to content

Commit 5089665

Browse files
authored
feat: allow change minio protocol and download url (go-sonic#357)
* feat: allow change minio protocol and download url Signed-off-by: ztelliot <[email protected]> * feat: choice whether to log to file Signed-off-by: ztelliot <[email protected]> --------- Signed-off-by: ztelliot <[email protected]>
1 parent b62349d commit 5089665

File tree

8 files changed

+73
-17
lines changed

8 files changed

+73
-17
lines changed

config/config.go

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewConfig() *Config {
8484

8585
initDirectory(conf)
8686
mode = conf.Sonic.Mode
87+
logMode = conf.Sonic.LogMode
8788
return conf
8889
}
8990

@@ -104,7 +105,19 @@ func initDirectory(conf *Config) {
104105
}
105106

106107
var mode string
108+
var logMode LogMode
107109

108110
func IsDev() bool {
109111
return mode == "development"
110112
}
113+
114+
func LogToConsole() bool {
115+
switch logMode {
116+
case Console:
117+
return true
118+
case File:
119+
return false
120+
default:
121+
return IsDev()
122+
}
123+
}

config/model.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ type Levels struct {
4141
Gorm string `mapstructure:"gorm"`
4242
}
4343

44+
type LogMode string
45+
46+
const (
47+
Console LogMode = "console"
48+
File LogMode = "file"
49+
)
50+
4451
type Sonic struct {
45-
Mode string `mapstructure:"mode"`
46-
WorkDir string `mapstructure:"work_dir"`
52+
Mode string `mapstructure:"mode"`
53+
LogMode LogMode `mapstructure:"log_mode"`
54+
WorkDir string `mapstructure:"work_dir"`
4755
UploadDir string
4856
LogDir string `mapstructure:"log_dir"`
4957
TemplateDir string `mapstructure:"template_dir"`

log/gorm_logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewGormLogger(conf *config.Config, zapLogger *zap.Logger) logger.Interface
2828
SlowThreshold: 200 * time.Millisecond,
2929
LogLevel: GetGormLogLevel(conf.Log.Levels.Gorm),
3030
IgnoreRecordNotFoundError: true,
31-
Colorful: config.IsDev(),
31+
Colorful: config.LogToConsole(),
3232
}
3333
gl := &gormLogger{
3434
Config: logConfig,

log/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func NewLogger(conf *config.Config) *zap.Logger {
1515
_, err := os.Stat(conf.Sonic.LogDir)
1616
if err != nil {
17-
if os.IsNotExist(err) && !config.IsDev() {
17+
if os.IsNotExist(err) && !config.LogToConsole() {
1818
err := os.MkdirAll(conf.Sonic.LogDir, os.ModePerm)
1919
if err != nil {
2020
panic("mkdir failed![%v]")
@@ -24,7 +24,7 @@ func NewLogger(conf *config.Config) *zap.Logger {
2424

2525
var core zapcore.Core
2626

27-
if config.IsDev() {
27+
if config.LogToConsole() {
2828
core = zapcore.NewCore(getDevEncoder(), os.Stdout, getLogLevel(conf.Log.Levels.App))
2929
} else {
3030
core = zapcore.NewCore(getProdEncoder(), getWriter(conf), zap.DebugLevel)

model/property/attachment.go

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ var MinioAccessSecret = Property{
5454
Kind: reflect.String,
5555
}
5656

57+
var MinioProtocol = Property{
58+
DefaultValue: "https://",
59+
KeyValue: "minio_protocol",
60+
Kind: reflect.String,
61+
}
62+
5763
var MinioSource = Property{
5864
DefaultValue: "",
5965
KeyValue: "minio_source",
@@ -66,6 +72,12 @@ var MinioRegion = Property{
6672
Kind: reflect.String,
6773
}
6874

75+
var MinioFrontBase = Property{
76+
DefaultValue: "",
77+
KeyValue: "minio_front_base",
78+
Kind: reflect.String,
79+
}
80+
6981
var AliOssEndpoint = Property{
7082
DefaultValue: "",
7183
KeyValue: "oss_ali_endpoint",

model/property/base.go

+2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ var AllProperty = []Property{
101101
MinioBucketName,
102102
MinioAccessKey,
103103
MinioAccessSecret,
104+
MinioProtocol,
104105
MinioSource,
105106
MinioRegion,
107+
MinioFrontBase,
106108
AliOssEndpoint,
107109
AliOssBucketName,
108110
AliOssAccessKey,

service/impl/client_option.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ func (c *clientOptionServiceImpl) getPrivateOption() map[string]struct{} {
6161
property.MinioBucketName,
6262
property.MinioAccessKey,
6363
property.MinioAccessSecret,
64+
property.MinioProtocol,
6465
property.MinioSource,
6566
property.MinioRegion,
67+
property.MinioFrontBase,
6668
property.AliOssEndpoint,
6769
property.AliOssBucketName,
6870
property.AliOssAccessKey,

service/storage/impl/minio.go

+31-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type minioClient struct {
3232
BucketName string
3333
Source string
3434
EndPoint string
35+
Protocol string
36+
FrontBase string
3537
}
3638

3739
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) (*
4143
}
4244

4345
fd, err := newURLFileDescriptor(
44-
withBaseURL(minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName),
46+
withBaseURL(minioClientInstance.Protocol+minioClientInstance.EndPoint+"/"+minioClientInstance.BucketName),
4547
withSubURLPath(minioClientInstance.Source),
4648
withShouldRenameURLOption(commonRenamePredicateFunc(ctx, consts.AttachmentTypeMinIO)),
4749
withOriginalNameURLOption(fileHeader.Filename),
@@ -103,14 +105,17 @@ func (m *MinIO) GetFilePath(ctx context.Context, relativePath string) (string, e
103105
if err != nil {
104106
return "", err
105107
}
106-
base := minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName
108+
base := minioClientInstance.Protocol + minioClientInstance.EndPoint + "/" + minioClientInstance.BucketName
109+
if minioClientInstance.FrontBase != "" {
110+
base = minioClientInstance.FrontBase
111+
}
107112
fullPath, _ := url.JoinPath(base, relativePath)
108113
fullPath, _ = url.PathUnescape(fullPath)
109114
return fullPath, nil
110115
}
111116

112117
func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
113-
getClientProperty := func(propertyValue *string, property property.Property, e error) error {
118+
getClientProperty := func(propertyValue *string, property property.Property, allowEmpty bool, e error) error {
114119
if e != nil {
115120
return e
116121
}
@@ -122,25 +127,37 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
122127
if !ok {
123128
return xerr.WithStatus(nil, xerr.StatusBadRequest).WithErrMsgf("wrong property type")
124129
}
125-
if strValue == "" {
130+
if !allowEmpty && strValue == "" {
126131
return xerr.WithStatus(nil, xerr.StatusInternalServerError).WithMsg("property not found: " + property.KeyValue)
127132
}
128133
*propertyValue = strValue
129134
return nil
130135
}
131-
var endPoint, bucketName, accessKey, accessSecret, source, region string
132-
err := getClientProperty(&endPoint, property.MinioEndpoint, nil)
133-
err = getClientProperty(&bucketName, property.MinioBucketName, err)
134-
err = getClientProperty(&accessKey, property.MinioAccessKey, err)
135-
err = getClientProperty(&accessSecret, property.MinioAccessSecret, err)
136-
err = getClientProperty(&source, property.MinioSource, err)
137-
err = getClientProperty(&region, property.MinioRegion, err)
136+
var endPoint, bucketName, accessKey, accessSecret, protocol, source, region, frontBase string
137+
err := getClientProperty(&endPoint, property.MinioEndpoint, false, nil)
138+
err = getClientProperty(&bucketName, property.MinioBucketName, false, err)
139+
err = getClientProperty(&accessKey, property.MinioAccessKey, false, err)
140+
err = getClientProperty(&accessSecret, property.MinioAccessSecret, false, err)
141+
err = getClientProperty(&protocol, property.MinioProtocol, false, err)
142+
err = getClientProperty(&source, property.MinioSource, true, err)
143+
err = getClientProperty(&region, property.MinioRegion, true, err)
144+
err = getClientProperty(&frontBase, property.MinioFrontBase, true, err)
138145
if err != nil {
139146
return nil, err
140147
}
148+
secure := func() bool {
149+
switch protocol {
150+
case "https://":
151+
return true
152+
case "http://":
153+
return false
154+
default:
155+
return true
156+
}
157+
}()
141158
client, err := minio.New(endPoint, &minio.Options{
142159
Creds: credentials.NewStaticV4(accessKey, accessSecret, ""),
143-
Secure: true,
160+
Secure: secure,
144161
Region: region,
145162
})
146163
if err != nil {
@@ -153,5 +170,7 @@ func (m *MinIO) getMinioClient(ctx context.Context) (*minioClient, error) {
153170
minioClientInstance.BucketName = bucketName
154171
minioClientInstance.Source = source
155172
minioClientInstance.EndPoint = endPoint
173+
minioClientInstance.Protocol = protocol
174+
minioClientInstance.FrontBase = frontBase
156175
return minioClientInstance, nil
157176
}

0 commit comments

Comments
 (0)