forked from Mereithhh/van-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
470 lines (424 loc) · 24.1 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package main
import (
"database/sql"
"embed"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/mereith/nav/goscraper"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
_ "modernc.org/sqlite"
// _ "github.com/mattn/go-sqlite3"
)
const INDEX = "index.html"
func getIcon(url string) string {
fmt.Println("getIcon: " + url)
s, err := goscraper.Scrape(url, 5)
if err != nil {
fmt.Println(err)
return ""
}
var result string = ""
if strings.Contains(s.Preview.Icon, "http:") || strings.Contains(s.Preview.Icon, "https:") {
result = s.Preview.Icon
} else {
// 如果 link 最后一个是 /
var first string = s.Preview.Link
var second string = s.Preview.Icon
if !strings.Contains(s.Preview.Link[len(s.Preview.Link)-1:len(s.Preview.Link)], "/") {
first = s.Preview.Link + "/"
}
// 如果 icon 第一个是 /
if strings.Contains(s.Preview.Icon[0:1], "/") {
second = s.Preview.Icon[1:len(s.Preview.Icon)]
}
result = first + second
}
fmt.Println(result)
return result
}
func updateCatelog(data updateCatelogDto, db *sql.DB) {
// 查询分类原名称
sql_select_old_catelog_name := `select name from nav_catelog where id = ?;`
var oldName string
err := db.QueryRow(sql_select_old_catelog_name, data.Id).Scan(&oldName)
checkErr(err)
fmt.Println(oldName)
// 开启事务
tx, err := db.Begin()
checkErr(err)
// 更新分类新名称
sql_update_catelog := `
UPDATE nav_catelog
SET name = ?, sort = ?
WHERE id = ?;
`
stmt, err := tx.Prepare(sql_update_catelog)
checkTxErr(err, tx)
res, err := stmt.Exec(data.Name, data.Sort, data.Id)
checkTxErr(err, tx)
_, err = res.RowsAffected()
checkTxErr(err, tx)
// fmt.Println(affect)
// 更新工具分类新名称
sql_update_tools := `
UPDATE nav_table
SET catelog = ?
WHERE catelog = ?;
`
stmt2, err := tx.Prepare(sql_update_tools)
checkTxErr(err, tx)
res2, err := stmt2.Exec(data.Name, oldName)
checkTxErr(err, tx)
_, err = res2.RowsAffected()
checkTxErr(err, tx)
// 提交事务
err = tx.Commit()
checkErr(err)
}
func getImgFromDB(url1 string, db *sql.DB) Img {
urlEncoded := url.QueryEscape(url1)
sql_get_img := `
SELECT * FROM nav_img
WHERE url=?;
`
rows, err := db.Query(sql_get_img, urlEncoded)
checkErr(err)
var result Img
var has bool = false
for rows.Next() {
err = rows.Scan(&result.Id, &result.Url, &result.Value)
checkErr(err)
has = true
}
if !has {
var nullImg string
l := strings.Split(url1, ".")
suffix := l[len(l)-1]
if strings.Contains(suffix, "svg") {
nullImg = "PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNjQ5OTM0MTE0OTg2IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjI2NjEiIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCI+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj5AZm9udC1mYWNlIHsgZm9udC1mYW1pbHk6IGZlZWRiYWNrLWljb25mb250OyBzcmM6IHVybCgiLy9hdC5hbGljZG4uY29tL3QvZm9udF8xMDMxMTU4X3U2OXc4eWh4ZHUud29mZjI/dD0xNjMwMDMzNzU5OTQ0IikgZm9ybWF0KCJ3b2ZmMiIpLCB1cmwoIi8vYXQuYWxpY2RuLmNvbS90L2ZvbnRfMTAzMTE1OF91Njl3OHloeGR1LndvZmY/dD0xNjMwMDMzNzU5OTQ0IikgZm9ybWF0KCJ3b2ZmIiksIHVybCgiLy9hdC5hbGljZG4uY29tL3QvZm9udF8xMDMxMTU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9hvMn2+z0+SiRZ5ncB4BtEJEmPXJmFgCNIm+EwPj4um3FvYGY5v7QcAE7o8hH0BDPfg4j3VCqV74yMjDzW5e3J1X1HEEt4wzA8Nc5p8UYAkNwWz7c0UbT4LwHgbgD4tpxQDoJAjoG4YoiAI4ghUPOJjY2NHV+pVJYiokStl49EHpfvl6c0basuR1pkT0KO5u9g5h2NRmPHunXrdtoacvLPIeAIktNomJycrDzzzDNLtdYtwhyLiIuZ+ShmXoyIrW/Z5T8KAFrf4tGzALBHvpl5DyLKjvWz8o2Izf8DAMnmusPzvEeXLFmyY+XKlY2cmtLXZv8/whqfIFfJyDkAAAAASUVORK5CYII="
}
return Img{Id: 0, Url: url1, Value: nullImg}
}
defer rows.Close()
return result
}
func updateImg(url1 string, db *sql.DB) {
// 除了更新工具本身之外,也要更新 img 表
// 先看有没有,有的话就不管了,没有的话就创建
urlEncoded := url.QueryEscape(url1)
// fmt.Println("创建时编码:", urlEncoded)\
base64ImgValue := getImgBase64FromUrl(url1)
if base64ImgValue == "" {
return
}
sql_get_img := `
SELECT * FROM nav_img
WHERE url = ?;
`
rows, err := db.Query(sql_get_img, urlEncoded)
checkErr(err)
defer rows.Close()
if !rows.Next() {
sql_add_img := `
INSERT INTO nav_img (url, value)
VALUES (?, ?);
`
stmt, err := db.Prepare(sql_add_img)
checkErr(err)
_, err = stmt.Exec(urlEncoded, base64ImgValue)
checkErr(err)
}
}
func updateTool(data updateToolDto, db *sql.DB) {
// 除了更新工具本身之外,也要更新 img 表
sql_update_tool := `
UPDATE nav_table
SET name = ?, url = ?, logo = ?, catelog = ?, desc = ?, sort = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_tool)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort, data.Id)
checkErr(err)
_, err = res.RowsAffected()
checkErr(err)
// 更新 img
updateImg(data.Logo, db)
// fmt.Println(affect)
}
func updateSetting(data Setting, db *sql.DB) {
sql_update_setting := `
UPDATE nav_setting
SET favicon = ?, title = ?, logo192 = ?, logo512 = ?, hideAdmin = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_setting)
checkErr(err)
res, err := stmt.Exec(data.Favicon, data.Title, data.Logo192, data.Logo512, data.HideAdmin, 0)
checkErr(err)
_, err = res.RowsAffected()
checkErr(err)
// fmt.Println(affect)
}
func addApiTokenInDB(data Token, db *sql.DB) {
sql_add_api_token := `
INSERT INTO nav_api_token (id,name,value,disabled)
VALUES (?,?,?,?);
`
// fmt.Println("增加分类:",data)
stmt, err := db.Prepare(sql_add_api_token)
checkErr(err)
res, err := stmt.Exec(data.Id, data.Name, data.Value, data.Disabled)
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
}
func updateUser(data updateUserDto, db *sql.DB) {
sql_update_user := `
UPDATE nav_user
SET name = ?, password = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_user)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Password, data.Id)
checkErr(err)
_, err = res.RowsAffected()
checkErr(err)
// fmt.Println(affect)
}
func addCatelog(data addCatelogDto, db *sql.DB) {
// 先检查重复不重复
existCatelogs := getAllCatelog(db)
var existCatelogsArr []string
for _, catelogDto := range existCatelogs {
existCatelogsArr = append(existCatelogsArr, catelogDto.Name)
}
if in(data.Name, existCatelogsArr) {
return
}
sql_add_catelog := `
INSERT INTO nav_catelog (name,sort)
VALUES (?,?);
`
// fmt.Println("增加分类:",data)
stmt, err := db.Prepare(sql_add_catelog)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Sort)
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
// fmt.Println(id)
}
func addTool(data addToolDto, db *sql.DB) int64 {
sql_add_tool := `
INSERT INTO nav_table (name, url, logo, catelog, desc, sort)
VALUES (?, ?, ?, ?, ?, ?);
`
stmt, err := db.Prepare(sql_add_tool)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort)
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
// fmt.Println(id)
updateImg(data.Logo, db)
return id
}
func getAllTool(db *sql.DB) []Tool {
sql_get_all := `
SELECT * FROM nav_table order by sort;
`
results := make([]Tool, 0)
rows, err := db.Query(sql_get_all)
checkErr(err)
for rows.Next() {
var tool Tool
err = rows.Scan(&tool.Id, &tool.Name, &tool.Url, &tool.Logo, &tool.Catelog, &tool.Desc, &tool.Sort)
checkErr(err)
results = append(results, tool)
}
defer rows.Close()
return results
}
func getAllCatelog(db *sql.DB) []Catelog {
sql_get_all := `
SELECT * FROM nav_catelog order by sort;
`
results := make([]Catelog, 0)
rows, err := db.Query(sql_get_all)
checkErr(err)
for rows.Next() {
var catelog Catelog
err = rows.Scan(&catelog.Id, &catelog.Name, &catelog.Sort)
checkErr(err)
results = append(results, catelog)
}
defer rows.Close()
return results
}
func generateId() int {
// 生成一个随机 id
id := int(time.Now().Unix())
return id
}
var db *sql.DB
func PathExistsOrCreate(path string) {
_, err := os.Stat(path)
if err == nil {
return
}
os.Mkdir(path, os.ModePerm)
}
//go:embed public
var fs embed.FS
type binaryFileSystem struct {
fs http.FileSystem
root string
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
// fmt.Println("打开文件",name)
openPath := path.Join(b.root, name)
return b.fs.Open(openPath)
}
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
var name string
if p == "" {
// fmt.Println("找 index")
name = path.Join(b.root, p, INDEX)
} else {
name = path.Join(b.root, p)
}
// 判断
// fmt.Println("文件是否存在?",name)
if _, err := b.fs.Open(name); err != nil {
return false
}
return true
}
return false
}
func BinaryFileSystem(data embed.FS, root string) *binaryFileSystem {
fs := http.FS(data)
return &binaryFileSystem{
fs,
root,
}
}
func main() {
initDB()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(gzip.Gzip(gzip.DefaultCompression))
// 嵌入文件夹
// public,_ := fs.ReadDir("./public")
// router.StaticFS("/",http.FS(fs))
router.GET("/manifest.json", ManifastHanlder)
router.Use(static.Serve("/", BinaryFileSystem(fs, "public")))
// router.Use(static.Serve("/", static.LocalFile("./public", true)))
api := router.Group("/api")
{
// 获取数据的路由
api.GET("/", GetAllHandler)
// 获取用户信息
api.POST("/login", LoginHandler)
api.GET("/logout", LogoutHandler)
api.GET("/img", getLogoImgHandler)
// 管理员用的
admin := api.Group("/admin")
admin.Use(JWTMiddleware())
{
admin.POST("/apiToken", AddApiTokenHandler)
admin.DELETE("/apiToken/:id", DeleteApiTokenHandler)
admin.GET("/all", GetAdminAllDataHandler)
admin.GET("/exportTools", ExportToolsHandler)
admin.POST("/importTools", ImportToolsHandler)
admin.PUT("/user", UpdateUserHandler)
admin.PUT("/setting", UpdateSettingHandler)
admin.POST("/tool", AddToolHandler)
admin.DELETE("/tool/:id", DeleteToolHandler)
admin.PUT("/tool/:id", UpdateToolHandler)
admin.POST("/catelog", AddCatelogHandler)
admin.DELETE("/catelog/:id", DeleteCatelogHandler)
admin.PUT("/catelog/:id", UpdateCatelogHandler)
}
}
fmt.Println("应用启动成功,网址: http://localhost:6412")
router.Run(":6412")
}
func importTools(data []Tool) {
var catelogs []string
for _, v := range data {
// if ()
if !in(v.Catelog, catelogs) {
catelogs = append(catelogs, v.Catelog)
}
sql_add_tool := `
INSERT INTO nav_table (id, name, catelog, url, logo, desc)
VALUES (?, ?, ?, ?, ?, ?);
`
stmt, err := db.Prepare(sql_add_tool)
checkErr(err)
res, err := stmt.Exec(v.Id, v.Name, v.Catelog, v.Url, v.Logo, v.Desc)
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
}
for _, catelog := range catelogs {
var addCatelogDto addCatelogDto
addCatelogDto.Name = catelog
addCatelog(addCatelogDto, db)
}
// 转存所有图片,异步
go func(data []Tool, db *sql.DB) {
for _, v := range data {
updateImg(v.Logo, db)
}
}(data, db)
}
func getSetting(db *sql.DB) Setting {
sql_get_user := `
SELECT * FROM nav_setting WHERE id = ?;
`
var setting Setting
row := db.QueryRow(sql_get_user, 0)
err := row.Scan(&setting.Id, &setting.Favicon, &setting.Title, &setting.Logo192, &setting.Logo512, &setting.HideAdmin)
checkErr(err)
return setting
}
func getApiTokens(db *sql.DB) []Token {
sql_get_api_tokens := `
SELECT * FROM nav_api_token WHERE disabled = 0;
`
results := make([]Token, 0)
rows, err := db.Query(sql_get_api_tokens)
checkErr(err)
for rows.Next() {
var token Token
err = rows.Scan(&token.Id, &token.Name, &token.Value, &token.Disabled)
checkErr(err)
results = append(results, token)
}
defer rows.Close()
return results
}
func getUser(name string, db *sql.DB) User {
sql_get_user := `
SELECT * FROM nav_user WHERE name = ?;
`
var user User
row := db.QueryRow(sql_get_user, name)
err := row.Scan(&user.Id, &user.Name, &user.Password)
checkErr(err)
return user
}