配置可为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/static"
"github.com/fthvgb1/wp-go/app/theme" "github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/app/wpconfig"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof" "github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
@ -54,19 +55,29 @@ func SetupRouter() *gin.Engine {
} }
f := static.Fs{FS: static.FsDir, Path: "wp-includes"} f := static.Fs{FS: static.FsDir, Path: "wp-includes"}
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsDir)) if c.WpDir != "" {
r.StaticFS("/wp-includes", http.FS(f)) r.Static("/wp-content/uploads", str.Join(c.WpDir, "/wp-content/uploads"))
r.StaticFS("/wp-content/plugins", http.FS(static.Fs{ r.Static("/wp-content/themes", str.Join(c.WpDir, "/wp-content/themes"))
FS: static.FsDir, r.Static("/wp-content/plugins", str.Join(c.WpDir, "/wp-content/plugins"))
Path: "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.StaticFS("/wp-content/themes", http.FS(static.Fs{ r.Static("/wp-includes/js", str.Join(c.WpDir, "/wp-includes/js"))
FS: static.FsDir, } else {
Path: "wp-content/themes", r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsDir))
})) r.StaticFS("/wp-includes", http.FS(f))
if c.UploadDir != "" { r.StaticFS("/wp-content/plugins", http.FS(static.Fs{
r.Static("/wp-content/uploads", c.UploadDir) FS: static.FsDir,
Path: "wp-content/plugins",
}))
r.StaticFS("/wp-content/themes", http.FS(static.Fs{
FS: static.FsDir,
Path: "wp-content/themes",
}))
if c.UploadDir != "" {
r.Static("/wp-content/uploads", c.UploadDir)
}
} }
store := cookie.NewStore([]byte("secret")) store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("go-wp", store)) r.Use(sessions.Sessions("go-wp", store))
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home)) r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home))

View File

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

View File

@ -1,10 +1,16 @@
package config package config
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io"
"net/http"
"os" "os"
"path/filepath"
"strings"
"time" "time"
) )
@ -39,6 +45,7 @@ type Config struct {
ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"` ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"`
Plugins []string `yaml:"plugins" json:"plugins,omitempty"` Plugins []string `yaml:"plugins" json:"plugins,omitempty"`
LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"` LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"`
WpDir string `yaml:"wpDir" json:"wpDir"`
} }
type CacheTime struct { type CacheTime struct {
@ -84,12 +91,39 @@ func InitConfig(conf string) error {
if conf == "" { if conf == "" {
conf = "config.yaml" 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 { if err != nil {
return err return err
} }
var c Config var c Config
err = yaml.Unmarshal(file, &c) 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 { if err != nil {
return err return err
} }

View File

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

View File

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

View File

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