diff --git a/actions/comment.go b/actions/comment.go new file mode 100644 index 0000000..d2a6830 --- /dev/null +++ b/actions/comment.go @@ -0,0 +1,53 @@ +package actions + +import ( + "errors" + "github.com/gin-gonic/gin" + "github/fthvgb1/wp-go/vars" + "io/ioutil" + "net/http" + "net/http/cookiejar" + "strings" + "time" +) + +func PostComment(c *gin.Context) { + jar, _ := cookiejar.New(nil) + cli := &http.Client{ + Jar: jar, + Timeout: time.Second * 3, + } + body, err := ioutil.ReadAll(c.Request.Body) + defer func() { + if err != nil { + c.String(http.StatusConflict, err.Error()) + } + }() + if err != nil { + return + } + req, err := http.NewRequest("POST", vars.Conf.PostCommentUrl, strings.NewReader(string(body))) + if err != nil { + return + } + defer req.Body.Close() + for k, v := range c.Request.Header { + req.Header.Set(k, v[0]) + } + res, err := cli.Do(req) + if err != nil { + return + } + if res.StatusCode == http.StatusOK && res.Request.Response.StatusCode == http.StatusFound { + for _, cookie := range res.Request.Response.Cookies() { + c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly) + } + c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location")) + return + } + s, err := ioutil.ReadAll(res.Body) + if err != nil { + return + } + err = errors.New(string(s)) +} diff --git a/config.example.yaml b/config.example.yaml index 036ed1a..ec9d9f5 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -55,3 +55,5 @@ userInfoCacheTime: 24h commentsCacheTime: 24h # Gzip gzip: false +# 提交评论url +postCommentUrl: http://wp.test/wp-comments-post.php diff --git a/route/route.go b/route/route.go index 0e572e7..06780f0 100644 --- a/route/route.go +++ b/route/route.go @@ -58,6 +58,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", actions.PostComment) if helper.IsContainInArr(gin.Mode(), []string{gin.DebugMode, gin.TestMode}) { pprof.Register(r, "dev/pprof") } diff --git a/templates/layout/message.gohtml b/templates/layout/message.gohtml new file mode 100644 index 0000000..c97243b --- /dev/null +++ b/templates/layout/message.gohtml @@ -0,0 +1,133 @@ + + + + + + + {{.title}} + + + +
+

{{.messge}}

+
+

+ « 返回 +

+ + diff --git a/templates/posts/detail.gohtml b/templates/posts/detail.gohtml index 2f7f0a2..c55829d 100644 --- a/templates/posts/detail.gohtml +++ b/templates/posts/detail.gohtml @@ -57,7 +57,7 @@ -

您的电子邮箱地址不会被公开。 diff --git a/vars/config.go b/vars/config.go index e8e2207..7f0e70e 100644 --- a/vars/config.go +++ b/vars/config.go @@ -32,6 +32,7 @@ type Config struct { UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` Gzip bool `yaml:"gzip"` + PostCommentUrl string `yaml:"postCommentUrl"` } type Mysql struct {