Skip to content

Commit d724d6f

Browse files
committed
Update the server config.
Signed-off-by: corvofeng <corvofeng@gmail.com>
1 parent 2f09112 commit d724d6f

8 files changed

Lines changed: 315 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var/
2+
export/
23
db.json
34
index/
45
venv/

cmd/server/main.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
65
"flag"
76
"log"
87
"net/http"
@@ -70,27 +69,27 @@ func main() {
7069

7170
// http
7271
srv := &http.Server{Addr: ":" + strconv.Itoa(mcf.HTTPPort), Handler: root}
73-
// srv = &http.Server{Addr: ":" + *httpPort, Handler: root}
7472
go func() {
75-
log.Fatal(srv.ListenAndServe())
73+
// log.Fatal(srv.ListenAndServe())
74+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
75+
log.Fatalf("listen: %s\n", err)
76+
}
7677
}()
7778

7879
logger.Debug("Web server Listen port", strconv.Itoa(mcf.HTTPPort))
7980

8081
<-stopChan // wait for SIGINT
81-
logger.Notice("Shutting down server...")
82+
logger.Info("Shutting down server...")
8283

8384
// refer to https://medium.com/honestbee-tw-engineer/gracefully-shutdown-in-go-http-server-5f5e6b83da5a
8485
// shut down gracefully, but wait no longer than 10 seconds before halting
85-
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
86-
defer func() {
87-
app.Close()
88-
}()
89-
86+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // 5-second timeout
87+
defer cancel()
9088
if err := srv.Shutdown(ctx); err != nil {
9189
logger.Errorf("Server shutdown error: %+v", err)
9290
}
93-
logger.Notice("Server gracefully stopped")
91+
app.Close()
92+
logger.Info("Server gracefully stopped")
9493
}
9594

9695
func redirectHandler(w http.ResponseWriter, r *http.Request) {

cmd/tools/main.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"os"
6+
"strings"
77

88
"github.com/corvofeng/go-flarum/model"
99
"github.com/corvofeng/go-flarum/system"
@@ -13,6 +13,8 @@ import (
1313
func main() {
1414
configFile := flag.String("config", "./config/config.yaml", "full path of config.yaml file")
1515
logLevel := flag.String("lvl", "INFO", "DEBUG LEVEL")
16+
export := flag.String("export", "", "export data to file, e.g. export=article,comment,user")
17+
importer := flag.String("import", "", "import data to file, e.g. impoert=article,comment,user")
1618

1719
flag.Parse()
1820
util.InitLogger(*logLevel)
@@ -32,14 +34,76 @@ func main() {
3234

3335
article, _ := model.SQLArticleGetByID(app.GormDB, app.RedisDB, 4)
3436
article.CleanCache()
35-
fmt.Println(article.GetCommentIDList(app.RedisDB))
3637
// cmt, _ := model.SQLCommentByID(app.GormDB, , app.RedisDB, 158, 1)
3738
// pageInfo := model.SQLCommentListByTopic(
3839
// app.GormDB, app.RedisDB, article.ID, 100, app.Cf.Site.TimeZone)
3940
// for _, c := range pageInfo.Items {
4041
// fmt.Println(c.ID, c.CreatedAt.UTC().String())
4142
// }
4243

44+
for _, e := range strings.Split(*export, ",") {
45+
switch e {
46+
case "article":
47+
if err := model.ExportArticles(app.GormDB, "./export/articles.json"); err != nil {
48+
logger.Error("Export articles failed:", err)
49+
} else {
50+
logger.Info("Export articles successfully")
51+
}
52+
case "comment":
53+
if err := model.ExportComments(app.GormDB, "./export/comments.json"); err != nil {
54+
logger.Error("Export comments failed:", err)
55+
} else {
56+
logger.Info("Export comments successfully")
57+
}
58+
case "user":
59+
if err := model.ExportUsers(app.GormDB, "./export/users.json"); err != nil {
60+
logger.Error("Export users failed:", err)
61+
} else {
62+
logger.Info("Export users successfully")
63+
}
64+
case "tag":
65+
if err := model.ExportTags(app.GormDB, "./export/tags.json"); err != nil {
66+
logger.Error("Export tags failed:", err)
67+
} else {
68+
logger.Info("Export tags successfully")
69+
}
70+
default:
71+
logger.Warningf("Unknown export type: %s", e)
72+
}
73+
}
74+
75+
for _, i := range strings.Split(*importer, ",") {
76+
switch i {
77+
case "article":
78+
if err := model.ImportArticles(app.GormDB, "./export/articles.json"); err != nil {
79+
logger.Error("Import articles failed:", err)
80+
} else {
81+
logger.Info("Import articles successfully")
82+
}
83+
case "comment":
84+
if err := model.ImportComments(app.GormDB, "./export/comments.json"); err != nil {
85+
logger.Error("Import comments failed:", err)
86+
} else {
87+
logger.Info("Import comments successfully")
88+
}
89+
case "user":
90+
if err := model.ImportUsers(app.GormDB, "./export/users.json"); err != nil {
91+
logger.Error("Import users failed:", err)
92+
} else {
93+
logger.Info("Import users successfully")
94+
}
95+
case "tag":
96+
if err := model.ImportTags(app.GormDB, "./export/tags.json"); err != nil {
97+
logger.Error("Import tags failed:", err)
98+
} else {
99+
logger.Info("Import tags successfully")
100+
}
101+
102+
default:
103+
logger.Warningf("Unknown import type: %s", i)
104+
}
105+
}
106+
43107
// 调试tags
44108
// fmt.Println(model.SQLGetTags(app.GormDB))
45109
// fmt.Println(model.SQLGetTagByUrlName(app.GormDB, "r_funny"))

controller/blog.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controller
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io"
76
"net/http"
87
"strconv"
@@ -72,7 +71,7 @@ func FlarumBlogMeta(w http.ResponseWriter, r *http.Request) {
7271
}
7372
logger.Debugf("Update blog meta with: %+v", diss)
7473
if _mid != "" && diss.Data.ID != "" && diss.Data.ID != _mid {
75-
h.flarumErrorJsonify(w, createSimpleFlarumError("mid not match: "+_mid+" != "+fmt.Sprintf("%d", diss.Data.ID)))
74+
h.flarumErrorJsonify(w, createSimpleFlarumError("mid not match: "+_mid+" != "+diss.Data.ID))
7675
return
7776
}
7877
bmID := uint64(0)

model/contentfmt.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/gomarkdown/markdown"
1111
"github.com/gomarkdown/markdown/html"
12+
"github.com/gomarkdown/markdown/parser"
1213
)
1314

1415
var (
@@ -69,6 +70,20 @@ type urlInfo struct {
6970
Click string
7071
}
7172

73+
func mdToHTML(md []byte) []byte {
74+
// create markdown parser with extensions
75+
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
76+
p := parser.NewWithExtensions(extensions)
77+
doc := p.Parse(md)
78+
79+
// create HTML renderer with extensions
80+
htmlFlags := html.CommonFlags | html.HrefTargetBlank
81+
opts := html.RendererOptions{Flags: htmlFlags}
82+
renderer := html.NewRenderer(opts)
83+
84+
return markdown.Render(doc, renderer)
85+
}
86+
7287
// ContentRich 用来转换文本, 转义以及允许用户添加一些富文本样式
7388
// 该函数效率奇差, 但不会优化
7489
func ContentRich(input string) string {
@@ -142,13 +157,12 @@ func ContentRich(input string) string {
142157
input = strings.ReplaceAll(input, k, v)
143158
}
144159
}
160+
// 将原有的字符串中的<>全部进行转义
161+
input = htmlEscape(input)
145162

146163
// 对markdown文本进行解析
147164
input = string(markdown.ToHTML([]byte(input), nil, renderer))
148165

149-
// 将原有的字符串中的<>全部进行转义
150-
// input = htmlEscape(input)
151-
152166
// 将原有被替换成uuid的内容进行恢复
153167
for k, v := range replaceDict {
154168
input = strings.ReplaceAll(input, k, v)

model/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Topic struct {
3333
ClientIP string `json:"clientip"`
3434
IsSticky bool
3535

36-
Tags []Tag `gorm:"many2many:topic_tags;"`
36+
Tags []Tag `gorm:"many2many:topic_tags;",json:"tags"`
3737

3838
BlogMetaData BlogMeta
3939
// `gorm:"foreignKey:TopicID"`

0 commit comments

Comments
 (0)