diff --git a/bootstrap/app.go b/bootstrap/app.go index 616c8026c2760d738897f6ac1f007142a7ea7f3d..ccb4fdcc65538bb0a7e0251ecb6d5f76a2e2e3f0 100644 --- a/bootstrap/app.go +++ b/bootstrap/app.go @@ -37,11 +37,13 @@ func CheckUpdate() { res, err := client.Request("GET", "https://api.github.com/repos/cloudreve/cloudreve/releases", nil).GetResponse() if err != nil { util.Log().Warning("更新检查失败, %s", err) + return } var list []GitHubRelease if err := json.Unmarshal([]byte(res), &list); err != nil { util.Log().Warning("更新检查失败, %s", err) + return } if len(list) > 0 { diff --git a/main.go b/main.go index a05bd5a4a47094e61e8eebcd7b663110dd24e785..d65ff07d54946a058fda0c99661f5197b2a5511e 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,18 @@ func init() { func main() { api := routers.InitRouter() + + // 如果启用了SSL + if conf.SSLConfig.CertPath != "" { + go func() { + util.Log().Info("开始监听 %s", conf.SSLConfig.Listen) + if err := api.RunTLS(conf.SSLConfig.Listen, + conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil { + util.Log().Error("无法监听[%s],%s", conf.SSLConfig.Listen, err) + } + }() + } + util.Log().Info("开始监听 %s", conf.SystemConfig.Listen) if err := api.Run(conf.SystemConfig.Listen); err != nil { util.Log().Error("无法监听[%s],%s", conf.SystemConfig.Listen, err) diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index d28d04341a0553af480a01e9ccbab5dd53bdc929..a19110b1f8fa3e34a17286e43614f898e78dbe3d 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -27,6 +27,12 @@ type system struct { HashIDSalt string } +type ssl struct { + CertPath string `validate:"omitempty,required"` + KeyPath string `validate:"omitempty,required"` + Listen string `validate:"required"` +} + // slave 作为slave存储端配置 type slave struct { Secret string `validate:"omitempty,gte=64"` @@ -113,6 +119,7 @@ func Init(path string) { sections := map[string]interface{}{ "Database": DatabaseConfig, "System": SystemConfig, + "SSL": SSLConfig, "Captcha": CaptchaConfig, "Redis": RedisConfig, "Thumbnail": ThumbConfig, diff --git a/pkg/conf/defaults.go b/pkg/conf/defaults.go index f4ff903f917f14a8298802900019f40f9a8819e4..406a2030d378044f9fe2cb0cd91f60bfadc92c63 100644 --- a/pkg/conf/defaults.go +++ b/pkg/conf/defaults.go @@ -59,3 +59,9 @@ var SlaveConfig = &slave{ CallbackTimeout: 20, SignatureTTL: 60, } + +var SSLConfig = &ssl{ + Listen: ":443", + CertPath: "", + KeyPath: "", +} diff --git a/pkg/util/logger.go b/pkg/util/logger.go index 81e365289affba5f30ab83a1624ddd73574d7170..107ec718bf0500c92c7f73120a51ba37ed87a4e9 100644 --- a/pkg/util/logger.go +++ b/pkg/util/logger.go @@ -3,6 +3,7 @@ package util import ( "fmt" "github.com/fatih/color" + "sync" "time" ) @@ -23,6 +24,7 @@ var Level = LevelDebug // Logger 日志 type Logger struct { level int + mu sync.Mutex } // 日志颜色 @@ -49,6 +51,10 @@ func (ll *Logger) Println(prefix string, msg string) { // color.NoColor = false c := color.New() + + ll.mu.Lock() + defer ll.mu.Unlock() + _, _ = c.Printf( "%s%s %s %s\n", colors[prefix]("["+prefix+"]"),