diff --git a/app/cmd/route/route.go b/app/cmd/route/route.go index b9759d3..183271c 100644 --- a/app/cmd/route/route.go +++ b/app/cmd/route/route.go @@ -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,19 +55,29 @@ func SetupRouter() *gin.Engine { } f := static.Fs{FS: static.FsDir, Path: "wp-includes"} - 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{ - 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) + 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{ + 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")) r.Use(sessions.Sessions("go-wp", store)) r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home)) diff --git a/app/middleware/staticFileCache.go b/app/middleware/staticFileCache.go index 3e2fb33..e281e7f 100644 --- a/app/middleware/staticFileCache.go +++ b/app/middleware/staticFileCache.go @@ -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()))) diff --git a/app/pkg/config/config.go b/app/pkg/config/config.go index aec012c..7979d9a 100644 --- a/app/pkg/config/config.go +++ b/app/pkg/config/config.go @@ -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 - 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 { return err } diff --git a/config.example.yaml b/config.example.yaml index 60e9e7c..39c6bc3 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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的路由 diff --git a/model/query_test.go b/model/query_test.go index a691c34..18cc71d 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -42,7 +42,7 @@ type post struct { PostMeta *[]models.PostMeta TermTaxonomy *[]TermTaxonomy Terms *[]models.Terms - CommentMetas *[]CommentMeta + CommentMetas []CommentMeta } type TermRelationships struct { diff --git a/model/relation_test.go b/model/relation_test.go index dd51587..907d71e 100644 --- a/model/relation_test.go +++ b/model/relation_test.go @@ -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",