-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
222 lines (219 loc) · 6.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"Lealra/JMComic"
"Lealra/bilibili"
"Lealra/config"
"Lealra/data"
"Lealra/funnyReply"
"Lealra/handler"
"Lealra/learnResp"
"Lealra/myUtil"
"Lealra/note"
"Lealra/returnStruct"
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/mongo"
"html/template"
"io"
"log"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"time"
)
var upgrade = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func main() {
defer func(client *mongo.Client, ctx context.Context) {
err := client.Disconnect(ctx)
if err != nil {
log.Fatalln(err)
}
}(data.Db.Client(), context.TODO())
go myUtil.RenewLoggers()
go funnyReply.CleanOldRecord()
go bilibili.StartSubscribeScanner()
go learnResp.StartExtendExpirationTimeLoop()
gin.DefaultWriter = io.MultiWriter(os.Stdout)
g := gin.Default()
g.LoadHTMLGlob("diary/template/*")
g.Static("/static", "diary/static")
g.Static("/something/study", "nsfw")
g.Static("/something/review", "pixiv/h")
g.Static("/something/plan", "pixiv/safe")
g.GET("/msg", func(c *gin.Context) {
//进行一个websocket的升
ws, err := upgrade.Upgrade(c.Writer, c.Request, nil)
myUtil.PublicWs = ws
if err != nil {
log.Fatalln(err)
}
defer ws.Close()
go func() {
<-c.Done()
fmt.Println("连接已断开", err.Error())
}()
for {
// 读取客户端发送过来的消息,如果没发就会一直阻塞住
_, message, err := ws.ReadMessage()
if err != nil {
fmt.Println("消息读取异常:", err.Error())
fmt.Println(err)
break
}
go func() {
res, err := handler.MsgHandler(ws, message, []int{config.Settings.Mode.GroupFilterMode, config.Settings.Mode.PrivateFilterMode})
if res != nil {
myUtil.WsLock.Lock()
err := ws.WriteMessage(returnStruct.MsgType, res)
myUtil.WsLock.Unlock()
if err != nil {
myUtil.ErrLog.Println("发送失败!: ", err.Error())
}
}
if err != nil {
myUtil.ErrLog.Println("发生了神奇的错误: ", err.Error())
}
}()
//err = ws.WriteMessage(mt, []byte("{\"action\": \"send_private_msg\",\"params\": {\"user_id\": \"1730483316\",\"message\":"+"\"sd\""+"}}"))
//if err != nil {
// fmt.Println(err)
// break
//}
}
})
g.GET("/diary/:uid/:date", func(c *gin.Context) {
strUid := c.Param("uid")
date := c.Param("date")
atoi, err := strconv.Atoi(strUid)
if err != nil {
return
}
if config.Settings.Func.PrivateDiary {
ok, _ := myUtil.IsInArray(config.Settings.Auth.Admin, int64(atoi))
if ok {
return
}
}
uid, err := strconv.ParseInt(strUid, 10, 64)
if err != nil {
myUtil.ErrLog.Println("访问每日实时记录时参数错误: ", err.Error())
return
}
notes, err := note.GetNotes(uid, date)
if err != nil {
myUtil.ErrLog.Println("获取每日实时记录失败!error: ", err)
return
}
c.HTML(http.StatusOK, "diary.tmpl", struct {
Content []template.HTML
Date string
}{Content: note.Analyse(notes), Date: date})
})
g.GET("/MyDiary", func(c *gin.Context) {
date := time.Now().Format("2006-01-02")
notes, err := note.GetNotes(1730483316, date)
if err != nil {
myUtil.ErrLog.Println("获取每日实时记录失败!")
return
}
c.HTML(http.StatusOK, "diary.tmpl", struct {
Content []template.HTML
Date string
}{Content: note.Analyse(notes), Date: date})
})
g.GET("/getComic/:id", func(c *gin.Context) {
pageNum := JMComic.GetPageNum(c.Param("id"))
println(pageNum)
c.HTML(http.StatusOK, "comic.tmpl", gin.H{
"id": c.Param("id"),
"pageNum": pageNum,
})
})
g.GET("/getComic/:id/:page", func(c *gin.Context) {
page, err := strconv.Atoi(c.Param("page"))
var statusCode int
var res JMComic.ComicPage
if err != nil {
statusCode = http.StatusBadRequest
res = JMComic.ComicPage{StatusCode: 400}
} else {
statusCode = http.StatusOK
res = JMComic.GetSexyComicPage(c.Param("id"), page+1)
}
c.JSON(statusCode, gin.H{
"statusCode": res.StatusCode,
"index": res.Index,
"comicImage": res.ComicImage,
})
})
g.GET("/comicWs/:id", func(c *gin.Context) {
var err error
var ws *websocket.Conn
var message []byte
var req, step int
ws, err = upgrade.Upgrade(c.Writer, c.Request, nil)
if err != nil {
myUtil.ErrLog.Println(c.Param("id") + "号本子传输通道建立失败" + err.Error())
return
}
defer ws.Close()
for {
_, message, err = ws.ReadMessage()
if err != nil {
myUtil.ErrLog.Println("看本子交互消息读取异常:", err.Error())
myUtil.ErrLog.Println(err)
break
}
pack := strings.Split(string(message), " ")
req, err = strconv.Atoi(pack[0])
if err != nil {
return
}
step, err = strconv.Atoi(pack[1])
if err != nil {
return
}
if JMComic.GetSexyComic(c.Param("id"), ws, req, req+step) {
break
}
}
myUtil.MsgLog.Println(c.Param("id") + "号本子传输已结束")
})
g.GET("/comic", func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, "/comic/"+c.Query("query")+"/1")
})
g.GET("/comic/:query/:page", func(c *gin.Context) {
comic, page, err := JMComic.FindComic(c.Param("query"), c.Param("page"))
var content []template.HTML
var unit string
for _, v := range comic {
unit = ""
unit += "<div><img src=\"" + strings.Replace(v.Img, "cdn-msp2", "cdn-msp", 1) + "\" onerror=\"loadFailed(this)\"></div>"
unit += "<div class=\"title\">" + v.Title + "</div>"
unit += "<div class=\"author\">作者:" + strings.Join(v.Author, "、") + "</div>"
unit += "<div class=\"tags\">标签:" + strings.Join(v.Tags, " · ") + "</div>"
unit += "<div class=\"id\">车牌号:<a href=\"/getComic/" + v.Id + "\">" + v.Id + "</a></div>"
content = append(content, template.HTML(unit))
}
c.HTML(http.StatusOK, "comicSearch.tmpl", struct {
Topic string
Content []template.HTML
Page int
Index string
Err string
}{Topic: c.Param("query"), Content: content, Page: page, Index: c.Param("page"), Err: err})
})
err := g.Run(":" + config.Settings.Server.Port)
if err != nil {
fmt.Println("Failed to start...,err:", err)
return
}
}