添加评论

This commit is contained in:
xing 2022-10-08 21:13:26 +08:00
parent 9bbbbc8f7b
commit ea117d04ee
6 changed files with 191 additions and 1 deletions

53
actions/comment.go Normal file
View File

@ -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))
}

View File

@ -55,3 +55,5 @@ userInfoCacheTime: 24h
commentsCacheTime: 24h
# Gzip
gzip: false
# 提交评论url
postCommentUrl: http://wp.test/wp-comments-post.php

View File

@ -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")
}

View File

@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width">
<meta name='robots' content='max-image-preview:large, noindex, follow'/>
<title>{{.title}}</title>
<style>
html {
background: #f1f1f1;
}
body {
background: #fff;
border: 1px solid #ccd0d4;
color: #444;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 2em auto;
padding: 1em 2em;
max-width: 700px;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
}
h1 {
border-bottom: 1px solid #dadada;
clear: both;
color: #666;
font-size: 24px;
margin: 30px 0 0 0;
padding: 0 0 7px;
}
#error-page {
margin-top: 50px;
}
#error-page p,
#error-page .wp-die-message {
font-size: 14px;
line-height: 1.5;
margin: 25px 0 20px;
}
#error-page code {
font-family: Consolas, Monaco, monospace;
}
ul li {
margin-bottom: 10px;
font-size: 14px;
}
a {
color: #0073aa;
}
a:hover,
a:active {
color: #006799;
}
a:focus {
color: #124964;
-webkit-box-shadow: 0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, 0.8);
box-shadow: 0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, 0.8);
outline: none;
}
.button {
background: #f3f5f6;
border: 1px solid #016087;
color: #016087;
display: inline-block;
text-decoration: none;
font-size: 13px;
line-height: 2;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
-webkit-border-radius: 3px;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
vertical-align: top;
}
.button.button-large {
line-height: 2.30769231;
min-height: 32px;
padding: 0 12px;
}
.button:hover,
.button:focus {
background: #f1f1f1;
}
.button:focus {
background: #f3f5f6;
border-color: #007cba;
-webkit-box-shadow: 0 0 0 1px #007cba;
box-shadow: 0 0 0 1px #007cba;
color: #016087;
outline: 2px solid transparent;
outline-offset: 0;
}
.button:active {
background: #f3f5f6;
border-color: #7e8993;
-webkit-box-shadow: none;
box-shadow: none;
}
</style>
</head>
<body id="error-page">
<div class="wp-die-message">
<p>{{.messge}}</p>
</div>
<p>
<a href='javascript:history.back()'>&laquo; 返回</a>
</p>
</body>
</html>

View File

@ -57,7 +57,7 @@
<a rel="nofollow" id="cancel-comment-reply-link" href="/p/{{.post.Id}}#respond" style="display:none;">取消回复</a>
</small>
</h3>
<form action="/wp-comments-post.php" method="post" id="commentform" class="comment-form"
<form action="/comment" method="post" id="commentform" class="comment-form"
novalidate="">
<p class="comment-notes">
<span id="email-notes">您的电子邮箱地址不会被公开。</span>

View File

@ -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 {