代码结构优化,添加readme
This commit is contained in:
parent
5880c02eb2
commit
faaafa2349
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
## wp-go
|
||||
a simple front of wordpress build with golang.
|
||||
|
||||
一个go写的wordress的前端,功能比较简单,只有列表页和详情页,rss2,主题只有一个twentyfifeen主题,插件的话只有一个简单的列表页的摘要生成和enlighter代码高亮。本身只用于展示文章,添加评论走的转发请求到php的wordpress。
|
@ -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
|
||||
|
@ -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() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package vars
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
20
db/db.go
20
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
|
||||
}
|
||||
|
4
go.mod
4
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
|
||||
)
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
16
mail/mail.go
16
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)
|
||||
|
@ -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
|
||||
|
6
main.go
6
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:
|
||||
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user