From 3d07fffc82acb42f98226e7ff493b94d6ff2877f Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 28 Jan 2023 23:12:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=EF=BC=8C=E6=B7=BB=E5=8A=A0ss?= =?UTF-8?q?l=E8=AF=81=E4=B9=A6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 4 ++++ internal/cmd/main.go | 22 +++++++++++++++------- internal/pkg/config/config.go | 6 ++++++ internal/plugins/gravatar.go | 6 ++---- internal/plugins/pagination.go | 2 +- plugin/pagination/pagination.go | 4 ++-- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index a614e89..c0c07cf 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -23,6 +23,10 @@ Mail: Port: 465 Ssl: true +ssl: + cert: "" + key: "" + # 最近文章缓存时间 recentPostCacheTime: 5m # 分类缓存时间 diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 85a7254..b65c393 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -24,19 +24,19 @@ import ( ) var confPath string -var port string +var address string var middleWareReloadFn func() var intReg = regexp.MustCompile(`^\d`) func init() { flag.StringVar(&confPath, "c", "config.yaml", "config file") - flag.StringVar(&port, "p", "", "port") + flag.StringVar(&address, "p", "", "listen address(port)") flag.Parse() - if port == "" && os.Getenv("PORT") == "" { - port = "80" + if address == "" && os.Getenv("PORT") == "" { + address = "80" } - if intReg.MatchString(port) && !strings.Contains(port, ":") { - port = ":" + port + if intReg.MatchString(address) && !strings.Contains(address, ":") { + address = ":" + address } rand.Seed(time.Now().UnixNano()) err := initConf(confPath) @@ -129,8 +129,16 @@ func signalNotify() { func main() { go signalNotify() Gin, reloadFn := route.SetupRouter() + c := config.Conf.Load() middleWareReloadFn = reloadFn - err := Gin.Run(port) + if c.Ssl.Key != "" && c.Ssl.Cert != "" { + err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key) + if err != nil { + panic(err) + } + return + } + err := Gin.Run(address) if err != nil { panic(err) } diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 026020b..8302f16 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -11,6 +11,7 @@ import ( var Conf safety.Var[Config] type Config struct { + Ssl Ssl `yaml:"ssl"` Mysql Mysql `yaml:"mysql"` Mail Mail `yaml:"mail"` RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` @@ -41,6 +42,11 @@ type Config struct { PostOrder string `yaml:"postOrder"` } +type Ssl struct { + Cert string `yaml:"cert"` + Key string `yaml:"key"` +} + type Mail struct { User string `yaml:"user"` Alias string `yaml:"alias"` diff --git a/internal/plugins/gravatar.go b/internal/plugins/gravatar.go index 1ebb7c5..dbb5cb1 100644 --- a/internal/plugins/gravatar.go +++ b/internal/plugins/gravatar.go @@ -2,18 +2,16 @@ package plugins import ( "fmt" + "github.com/fthvgb1/wp-go/helper/number" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/wpconfig" - "math/rand" "net/url" "strings" - "time" ) func Gravatar(email string, isTls bool) (u string) { email = strings.Trim(email, " \t\n\r\000\x0B") - rand.Seed(time.Now().UnixNano()) - num := rand.Intn(3) + num := number.Rand(0, 2) h := "" if email != "" { h = str.Md5(strings.ToLower(email)) diff --git a/internal/plugins/pagination.go b/internal/plugins/pagination.go index bdc68d5..d4e93bf 100644 --- a/internal/plugins/pagination.go +++ b/internal/plugins/pagination.go @@ -29,7 +29,7 @@ var twentyFifteen = PageEle{ %d`, } -func (p PageEle) Current(page int) string { +func (p PageEle) Current(page, totalPage int) string { return fmt.Sprintf(p.CurrentEle, page) } diff --git a/plugin/pagination/pagination.go b/plugin/pagination/pagination.go index 32b05ca..1783c4e 100644 --- a/plugin/pagination/pagination.go +++ b/plugin/pagination/pagination.go @@ -6,7 +6,7 @@ import ( ) type Elements interface { - Current(page int) string + Current(page, totalPage int) string Prev(url string) string Next(url string) string Dots() string @@ -68,7 +68,7 @@ func (p ParsePagination) ToHtml() (html string) { for page := start; page <= end; page++ { h := "" if p.CurrentPage == page { - h = p.Current(page) + h = p.Current(page, p.TotalPage) } else { h = p.Middle(page, p.Url(p.Path, p.Query, page)) }