-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (133 loc) · 3.21 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
package main
import (
"context"
"flag"
"haru/user/model"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"github.com/gin-gonic/gin"
"haru/associate"
assoTypes "haru/associate/types"
"haru/common"
"haru/crawl"
"haru/crawl/weibo"
"haru/engine"
"haru/engine/types"
"haru/logs"
"haru/middlewares"
"haru/user"
userController "haru/user/controller"
)
func main() {
log.SetFlags(log.LstdFlags)
common.Init()
user.Init()
flag.Parse()
e := engine.GetEngine()
go crawl.IndexToEngine(e)
// 捕获ctrl-c
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
log.Print("捕获Ctrl-c,退出服务器")
e.Close()
os.Exit(0)
}
}()
r := gin.New()
v1 := r.Group("/api/v1")
v2 := r.Group("/api/v2")
v2.Use(middlewares.Middlewares()...)
user.InitRouter(v1)
user.InitUserRouter(v2)
associate.InitRouter(v1)
//r.Use(middlewares.Middlewares()...)
r.SetFuncMap(template.FuncMap{
"formatDate": Timestamp2String,
})
r.Static("/static", "web/public")
r.LoadHTMLFiles("web/index.html", "web/login.html", "web/company.html",
"web/register.html", "web/result.html", "web/user.html", "web/image_result.html")
render(r)
err := r.Run(":8080")
if err != nil {
logs.Error("run gin error: %v", err)
return
}
}
func render(r *gin.Engine) {
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
})
r.GET("/register.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "register.html", gin.H{})
})
r.GET("/login.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.html", gin.H{})
})
r.GET("/company.html", userController.Company)
r.GET("/user.html", userController.User)
r.GET("/result.html", func(ctx *gin.Context) {
query := ctx.Query("query")
assoTypes.GetTrie().Insert(query)
logs.Debug("query: %v", query)
e := engine.GetEngine()
output := e.Search(types.SearchRequest{
Text: query,
RankOptions: &types.RankOptions{
ScoringCriteria: &weibo.WeiboScoringCriteria{},
OutputOffset: 0,
MaxOutputs: 100,
},
})
advs := make([]*model.Advertise, 0)
for _, token := range output.Tokens {
tmp, err := model.FindAdvertiseByKeyword(token)
if err != nil {
continue
}
advs = append(advs, tmp...)
}
if len(advs) > 10 {
advs = advs[:10]
}
// 整理为输出格式
var docs []weibo.Weibo
for _, doc := range output.Docs {
wb := weibo.Wbs[doc.DocId]
docs = append(docs, wb)
}
ctx.HTML(http.StatusOK, "result.html", gin.H{
"result": docs,
"query": query,
"advs": advs,
})
})
r.GET("/image_result.html", func(c *gin.Context) {
logs.Info("visit image_result.html")
rdb := common.GetRDB()
ctx := context.Background()
length, _ := rdb.LLen(ctx, "names").Result()
//images, _ := rdb.LRange(ctx, "images", 0, length-1).Result()
names, _ := rdb.LRange(ctx, "names", 0, length-1).Result()
logs.Debug("get from redis: %v", names)
//type Image struct {
// Name string `json:"name"`
// Data string `json:"data"`
//}
//var results []Image
//for i, image := range images {
// results = append(results, Image{
// Name: names[i],
// Data: image,
// })
//}
c.HTML(http.StatusOK, "image_result.html", gin.H{
"results": names,
})
})
}