From faaafa2349abf1327bfa4b2d96851b91b995484d Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 4 Nov 2022 10:38:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=B7=BB=E5=8A=A0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ actions/comment.go | 6 +++--- actions/common/common.go | 26 +++++++++++++------------- {vars => config}/config.go | 2 +- db/db.go | 20 ++++++++++---------- go.mod | 4 ++-- helper/func.go | 2 +- helper/func_test.go | 6 +++--- mail/mail.go | 16 ++++++++-------- mail/mail_test.go | 4 ++-- main.go | 6 +++--- middleware/sendmail.go | 4 ++-- middleware/validateservername.go | 4 ++-- plugins/digest.go | 6 +++--- route/route.go | 14 +++++++------- 15 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 README.md rename {vars => config}/config.go (99%) diff --git a/README.md b/README.md new file mode 100644 index 0000000..cc70ef5 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +## wp-go +a simple front of wordpress build with golang. + +一个go写的wordress的前端,功能比较简单,只有列表页和详情页,rss2,主题只有一个twentyfifeen主题,插件的话只有一个简单的列表页的摘要生成和enlighter代码高亮。本身只用于展示文章,添加评论走的转发请求到php的wordpress。 \ No newline at end of file diff --git a/actions/comment.go b/actions/comment.go index 64dd731..99fd958 100644 --- a/actions/comment.go +++ b/actions/comment.go @@ -6,10 +6,10 @@ import ( "fmt" "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/actions/common" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/logs" "github/fthvgb1/wp-go/mail" "github/fthvgb1/wp-go/models" - "github/fthvgb1/wp-go/vars" "io" "net/http" "net/http/cookiejar" @@ -39,7 +39,7 @@ func PostComment(c *gin.Context) { m := c.PostForm("email") comment := c.PostForm("comment") c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) - req, err := http.NewRequest("POST", vars.Conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) + req, err := http.NewRequest("POST", config.Conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) if err != nil { return } @@ -69,7 +69,7 @@ func PostComment(c *gin.Context) { return } su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", models.Options["siteurl"], author, m, post.PostTitle) - err = mail.SendMail([]string{vars.Conf.Mail.User}, su, comment) + err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment) logs.ErrPrintln(err, "发送邮件") }() return diff --git a/actions/common/common.go b/actions/common/common.go index c7c8150..1fbdccb 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -4,9 +4,9 @@ import ( "context" "fmt" "github/fthvgb1/wp-go/cache" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/logs" "github/fthvgb1/wp-go/models" - "github/fthvgb1/wp-go/vars" "sync" "time" ) @@ -32,29 +32,29 @@ func InitActionsCommonCache() { setCacheFunc: archives, } - searchPostIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime) + searchPostIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, config.Conf.SearchPostCacheTime) - postListIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime) + postListIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, config.Conf.PostListCacheTime) - monthPostsCache = cache.NewMapCacheByFn[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime) + monthPostsCache = cache.NewMapCacheByFn[string, []uint64](monthPost, config.Conf.MonthPostCacheTime) - postContextCache = cache.NewMapCacheByFn[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) + postContextCache = cache.NewMapCacheByFn[uint64, PostContext](getPostContext, config.Conf.ContextPostCacheTime) - postsCache = cache.NewMapCacheByBatchFn[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime) + postsCache = cache.NewMapCacheByBatchFn[uint64, models.WpPosts](getPostsByIds, config.Conf.PostDataCacheTime) - categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) + categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, config.Conf.CategoryCacheTime) - recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) + recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, config.Conf.RecentPostCacheTime) - recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) + recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, config.Conf.RecentCommentsCacheTime) - postCommentCaches = cache.NewMapCacheByFn[uint64, []uint64](postComments, vars.Conf.PostCommentsCacheTime) + postCommentCaches = cache.NewMapCacheByFn[uint64, []uint64](postComments, config.Conf.PostCommentsCacheTime) - maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime) + maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, config.Conf.MaxPostIdCacheTime) - usersCache = cache.NewMapCacheByBatchFn[uint64, models.WpUsers](getUsers, vars.Conf.UserInfoCacheTime) + usersCache = cache.NewMapCacheByBatchFn[uint64, models.WpUsers](getUsers, config.Conf.UserInfoCacheTime) - commentsCache = cache.NewMapCacheByBatchFn[uint64, models.WpComments](getCommentByIds, vars.Conf.CommentsCacheTime) + commentsCache = cache.NewMapCacheByBatchFn[uint64, models.WpComments](getCommentByIds, config.Conf.CommentsCacheTime) } func ClearCache() { diff --git a/vars/config.go b/config/config.go similarity index 99% rename from vars/config.go rename to config/config.go index 123d894..1afadef 100644 --- a/vars/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package vars +package config import ( "fmt" diff --git a/db/db.go b/db/db.go index 9ffe13a..dcb3736 100644 --- a/db/db.go +++ b/db/db.go @@ -3,29 +3,29 @@ package db import ( _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" - "github/fthvgb1/wp-go/vars" + "github/fthvgb1/wp-go/config" ) var Db *sqlx.DB func InitDb() error { - dsn := vars.Conf.Mysql.Dsn.GetDsn() + dsn := config.Conf.Mysql.Dsn.GetDsn() var err error Db, err = sqlx.Open("mysql", dsn) if err != nil { return err } - if vars.Conf.Mysql.Pool.ConnMaxIdleTime != 0 { - Db.SetConnMaxIdleTime(vars.Conf.Mysql.Pool.ConnMaxLifetime) + if config.Conf.Mysql.Pool.ConnMaxIdleTime != 0 { + Db.SetConnMaxIdleTime(config.Conf.Mysql.Pool.ConnMaxLifetime) } - if vars.Conf.Mysql.Pool.MaxIdleConn != 0 { - Db.SetMaxIdleConns(vars.Conf.Mysql.Pool.MaxIdleConn) + if config.Conf.Mysql.Pool.MaxIdleConn != 0 { + Db.SetMaxIdleConns(config.Conf.Mysql.Pool.MaxIdleConn) } - if vars.Conf.Mysql.Pool.MaxOpenConn != 0 { - Db.SetMaxOpenConns(vars.Conf.Mysql.Pool.MaxOpenConn) + if config.Conf.Mysql.Pool.MaxOpenConn != 0 { + Db.SetMaxOpenConns(config.Conf.Mysql.Pool.MaxOpenConn) } - if vars.Conf.Mysql.Pool.ConnMaxLifetime != 0 { - Db.SetConnMaxLifetime(vars.Conf.Mysql.Pool.ConnMaxLifetime) + if config.Conf.Mysql.Pool.ConnMaxLifetime != 0 { + Db.SetConnMaxLifetime(config.Conf.Mysql.Pool.ConnMaxLifetime) } return err } diff --git a/go.mod b/go.mod index 563dcb3..3c7abb9 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ require ( github.com/gin-gonic/gin v1.8.1 github.com/go-sql-driver/mysql v1.6.0 github.com/jmoiron/sqlx v1.3.5 + golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 ) @@ -29,11 +31,9 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/ugorji/go/codec v1.2.7 // indirect - golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect - gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect ) diff --git a/helper/func.go b/helper/func.go index 114c328..158df07 100644 --- a/helper/func.go +++ b/helper/func.go @@ -280,7 +280,7 @@ func (r anyArr[T]) Less(i, j int) bool { return r.fn(r.data[i], r.data[j]) } -func SampleSort[T any](arr []T, fn func(i, j T) bool) { +func SimpleSort[T any](arr []T, fn func(i, j T) bool) { slice := anyArr[T]{ data: arr, fn: fn, diff --git a/helper/func_test.go b/helper/func_test.go index 2d014b2..fd0e676 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -608,7 +608,7 @@ func TestRandNum(t *testing.T) { } } -func TestSampleSort(t *testing.T) { +func TestSimpleSort(t *testing.T) { type xy struct { x int y int @@ -654,8 +654,8 @@ func TestSampleSort(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if SampleSort(tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) { - t.Errorf("SampleSort() = %v, want %v", tt.args.arr, tt.wantR) + if SimpleSort(tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) { + t.Errorf("SimpleSort() = %v, want %v", tt.args.arr, tt.wantR) } }) } diff --git a/mail/mail.go b/mail/mail.go index 80dd781..f86a3ea 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -3,7 +3,7 @@ package mail import ( "crypto/tls" "fmt" - "github/fthvgb1/wp-go/vars" + "github/fthvgb1/wp-go/config" "gopkg.in/gomail.v2" "mime" "strings" @@ -24,8 +24,8 @@ func SendMail(mailTo []string, subject string, body string, a ...AttacheFile) er gomail.SetEncoding(gomail.Base64), ) m.SetHeader("From", - m.FormatAddress(vars.Conf.Mail.User, - vars.Conf.Mail.Alias, + m.FormatAddress(config.Conf.Mail.User, + config.Conf.Mail.Alias, )) m.SetHeader("To", mailTo...) m.SetHeader("Subject", subject) @@ -43,12 +43,12 @@ func SendMail(mailTo []string, subject string, body string, a ...AttacheFile) er } d := gomail.NewDialer( - vars.Conf.Mail.Host, - vars.Conf.Mail.Port, - vars.Conf.Mail.User, - vars.Conf.Mail.Pass, + config.Conf.Mail.Host, + config.Conf.Mail.Port, + config.Conf.Mail.User, + config.Conf.Mail.Pass, ) - if vars.Conf.Mail.Ssl { + if config.Conf.Mail.Ssl { d.TLSConfig = &tls.Config{InsecureSkipVerify: true} } err := d.DialAndSend(m) diff --git a/mail/mail_test.go b/mail/mail_test.go index 9d3fa01..fac1e42 100644 --- a/mail/mail_test.go +++ b/mail/mail_test.go @@ -1,12 +1,12 @@ package mail import ( - "github/fthvgb1/wp-go/vars" + "github/fthvgb1/wp-go/config" "testing" ) func TestSendMail(t *testing.T) { - vars.InitConfig() + config.InitConfig() type args struct { mailTo []string subject string diff --git a/main.go b/main.go index 1ea84fe..62ad5e7 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,18 @@ package main import ( "github/fthvgb1/wp-go/actions" "github/fthvgb1/wp-go/actions/common" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/plugins" "github/fthvgb1/wp-go/route" - "github/fthvgb1/wp-go/vars" "math/rand" "time" ) func init() { rand.Seed(time.Now().UnixNano()) - err := vars.InitConfig() + err := config.InitConfig() if err != nil { panic(err) } @@ -40,7 +40,7 @@ func init() { } func cronClearCache() { - t := time.NewTicker(vars.Conf.CrontabClearCacheTime) + t := time.NewTicker(config.Conf.CrontabClearCacheTime) for { select { case <-t.C: diff --git a/middleware/sendmail.go b/middleware/sendmail.go index ac74b6c..d742f5f 100644 --- a/middleware/sendmail.go +++ b/middleware/sendmail.go @@ -4,10 +4,10 @@ import ( "bytes" "fmt" "github.com/gin-gonic/gin" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/logs" "github/fthvgb1/wp-go/mail" "github/fthvgb1/wp-go/models" - "github/fthvgb1/wp-go/vars" "io" "io/ioutil" "net/http" @@ -42,7 +42,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) { ) er := mail.SendMail( - []string{vars.Conf.Mail.User}, + []string{config.Conf.Mail.User}, fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(models.Options["siteurl"]), c.FullPath(), time.Now().Format(time.RFC1123Z)), content) if er != nil { diff --git a/middleware/validateservername.go b/middleware/validateservername.go index b21401d..ad986d2 100644 --- a/middleware/validateservername.go +++ b/middleware/validateservername.go @@ -2,14 +2,14 @@ package middleware import ( "github.com/gin-gonic/gin" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/helper" - "github/fthvgb1/wp-go/vars" "net/http" "strings" ) func ValidateServerNames() func(ctx *gin.Context) { - serverName := helper.SimpleSliceToMap(vars.Conf.TrustServerNames, func(v string) string { + serverName := helper.SimpleSliceToMap(config.Conf.TrustServerNames, func(v string) string { return v }) return func(c *gin.Context) { diff --git a/plugins/digest.go b/plugins/digest.go index d67b485..1d521f2 100644 --- a/plugins/digest.go +++ b/plugins/digest.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/cache" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/models" - "github/fthvgb1/wp-go/vars" "regexp" "strings" "time" @@ -19,7 +19,7 @@ var digestCache *cache.MapCache[uint64, string] var quto = regexp.MustCompile(`" *|& *|< *|> ?|  *`) func InitDigestCache() { - digestCache = cache.NewMapCacheByFn[uint64](digestRaw, vars.Conf.DigestCacheTime) + digestCache = cache.NewMapCacheByFn[uint64](digestRaw, config.Conf.DigestCacheTime) } func ClearDigestCache() { @@ -29,7 +29,7 @@ func ClearDigestCache() { func digestRaw(arg ...any) (string, error) { str := arg[0].(string) id := arg[1].(uint64) - limit := vars.Conf.DigestWordCount + limit := config.Conf.DigestWordCount if limit < 0 { return str, nil } else if limit == 0 { diff --git a/route/route.go b/route/route.go index 8c37664..526df08 100644 --- a/route/route.go +++ b/route/route.go @@ -7,11 +7,11 @@ import ( "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/actions" + "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/middleware" "github/fthvgb1/wp-go/static" "github/fthvgb1/wp-go/templates" - "github/fthvgb1/wp-go/vars" "html/template" "net/http" "time" @@ -21,8 +21,8 @@ func SetupRouter() *gin.Engine { // Disable Console Color // gin.DisableConsoleColor() r := gin.New() - if len(vars.Conf.TrustIps) > 0 { - err := r.SetTrustedProxies(vars.Conf.TrustIps) + if len(config.Conf.TrustIps) > 0 { + err := r.SetTrustedProxies(config.Conf.TrustIps) if err != nil { panic(err) } @@ -40,11 +40,11 @@ func SetupRouter() *gin.Engine { middleware.ValidateServerNames(), gin.Logger(), middleware.RecoverAndSendMail(gin.DefaultErrorWriter), - middleware.FlowLimit(vars.Conf.MaxRequestSleepNum, vars.Conf.MaxRequestNum, vars.Conf.SleepTime), + middleware.FlowLimit(config.Conf.MaxRequestSleepNum, config.Conf.MaxRequestNum, config.Conf.SleepTime), middleware.SetStaticFileCache, ) //gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用 - if vars.Conf.Gzip { + if config.Conf.Gzip { r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{ "/wp-includes/", "/wp-content/", }))) @@ -58,7 +58,7 @@ func SetupRouter() *gin.Engine { })) store := cookie.NewStore([]byte("secret")) r.Use(sessions.Sessions("go-wp", store)) - r.GET("/", middleware.SearchLimit(vars.Conf.SingleIpSearchNum), actions.Index) + r.GET("/", middleware.SearchLimit(config.Conf.SingleIpSearchNum), actions.Index) r.GET("/page/:page", actions.Index) r.GET("/p/category/:category", actions.Index) r.GET("/p/category/:category/page/:page", actions.Index) @@ -71,7 +71,7 @@ func SetupRouter() *gin.Engine { r.GET("/p/:id/feed", actions.PostFeed) r.GET("/feed", actions.Feed) r.GET("/comments/feed", actions.CommentsFeed) - r.POST("/comment", middleware.FlowLimit(vars.Conf.MaxRequestSleepNum, 5, vars.Conf.SleepTime), actions.PostComment) + r.POST("/comment", middleware.FlowLimit(config.Conf.MaxRequestSleepNum, 5, config.Conf.SleepTime), actions.PostComment) if helper.IsContainInArr(gin.Mode(), []string{gin.DebugMode, gin.TestMode}) { pprof.Register(r, "dev/pprof") }