配置可为url,添加wordpress目录为静态资源目录并限制.php后缀

This commit is contained in:
xing 2023-05-31 21:54:23 +08:00
parent e408efacc8
commit ae6e496dd8
6 changed files with 70 additions and 16 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/fthvgb1/wp-go/app/static"
"github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/wpconfig"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
@ -54,6 +55,14 @@ func SetupRouter() *gin.Engine {
}
f := static.Fs{FS: static.FsDir, Path: "wp-includes"}
if c.WpDir != "" {
r.Static("/wp-content/uploads", str.Join(c.WpDir, "/wp-content/uploads"))
r.Static("/wp-content/themes", str.Join(c.WpDir, "/wp-content/themes"))
r.Static("/wp-content/plugins", str.Join(c.WpDir, "/wp-content/plugins"))
r.Static("/wp-includes/css", str.Join(c.WpDir, "/wp-includes/css"))
r.Static("/wp-includes/fonts", str.Join(c.WpDir, "/wp-includes/fonts"))
r.Static("/wp-includes/js", str.Join(c.WpDir, "/wp-includes/js"))
} else {
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsDir))
r.StaticFS("/wp-includes", http.FS(f))
r.StaticFS("/wp-content/plugins", http.FS(static.Fs{
@ -67,6 +76,8 @@ func SetupRouter() *gin.Engine {
if c.UploadDir != "" {
r.Static("/wp-content/uploads", c.UploadDir)
}
}
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("go-wp", store))
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home))

View File

@ -4,6 +4,8 @@ import (
"fmt"
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/gin-gonic/gin"
"net/http"
"path/filepath"
"strings"
)
@ -16,6 +18,11 @@ var path = map[string]struct{}{
func SetStaticFileCache(c *gin.Context) {
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
if _, ok := path[f[0]]; ok {
if ".php" == filepath.Ext(c.Request.URL.Path) {
c.Abort()
c.Status(http.StatusForbidden)
return
}
t := config.GetConfig().CacheTime.CacheControl
if t > 0 {
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%d", int(t.Seconds())))

View File

@ -1,10 +1,16 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
@ -39,6 +45,7 @@ type Config struct {
ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"`
Plugins []string `yaml:"plugins" json:"plugins,omitempty"`
LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"`
WpDir string `yaml:"wpDir" json:"wpDir"`
}
type CacheTime struct {
@ -84,12 +91,39 @@ func InitConfig(conf string) error {
if conf == "" {
conf = "config.yaml"
}
file, err := os.ReadFile(conf)
var file []byte
var err error
if strings.Contains(conf, "http") {
get, err := http.Get(conf)
if err != nil {
return err
}
file, err = io.ReadAll(get.Body)
} else {
file, err = os.ReadFile(conf)
}
if err != nil {
return err
}
var c Config
switch strings.ToLower(filepath.Ext(conf)) {
case ".yaml":
err = yaml.Unmarshal(file, &c)
case ".json":
err = json.Unmarshal(file, &c)
default:
err = yaml.Unmarshal(file, &c)
if err != nil {
err = json.Unmarshal(file, &c)
if err == nil {
break
}
} else {
break
}
return errors.New("invalid suffix config file")
}
if err != nil {
return err
}

View File

@ -88,6 +88,8 @@ trustServerNames: ["xy.test","blog.xy.test"]
theme: "twentyfifteen"
# 文档排序默认升序还是降序
postOrder: "desc"
# WordPress path
wpDir: "/var/www/wordpress"
# 上传的目录
uploadDir: ""
# pprof route path 为空表示不开启pprof,否则为pprof的路由

View File

@ -42,7 +42,7 @@ type post struct {
PostMeta *[]models.PostMeta
TermTaxonomy *[]TermTaxonomy
Terms *[]models.Terms
CommentMetas *[]CommentMeta
CommentMetas []CommentMeta
}
type TermRelationships struct {

View File

@ -175,7 +175,7 @@ var postHaveManyCommentMetas = func() RelationFn {
v := slice.Map(*i, func(t metas) CommentMeta {
return t.CommentMeta
})
m.CommentMetas = &v
m.CommentMetas = v
}, Relationship{
RelationType: HasOne,
Table: "wp_commentmeta",