main.go
5.14 KB
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
package main
import (
"encoding/json"
"github.com/aarongao/tools"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"letu/Api"
"letu/Config"
"letu/DB"
"letu/Lib/Cache"
"letu/Lib/DelayMessage"
"log"
"os"
"time"
)
// @APIVersion 1.0.0
// @APITitle 乐游图后端接口文档
// @BasePath 正式 leyoutu.st-i.com.cn; 测试 letu.api.imagchina.com
func main() {
// 读取配置文件
dir, _ := os.Getwd()
file, _ := os.Open(dir + "/Config/config.json")
defer file.Close()
decoder := json.NewDecoder(file)
conf := Config.Config{}
err := decoder.Decode(&conf)
tools.CheckError(err)
// 连接数据库
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://" + conf.DbPath)
clientOptions.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的
clientOptions.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数
clientOptions.SetMaxPoolSize(4096) //使用最大的连接数
// Connect to MongoDB
client, err := mongo.Connect(tools.GetContext(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(tools.GetContext(), nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected to MongoDB!")
//获取文档集
DB.DB = client.Database("LeYouTu")
//DB.DB.Login(conf.DbUser, conf.DbPassword)
DB.CItem = DB.DB.Collection("Item")
DB.CComplaint = DB.DB.Collection("Complaint")
DB.CInvestigation = DB.DB.Collection("Investigation")
DB.CMember = DB.DB.Collection("Member")
DB.CCommodity = DB.DB.Collection("Commodity")
DB.CTags = DB.DB.Collection("Tags")
DB.CScenic = DB.DB.Collection("Scenic")
DB.CLine = DB.DB.Collection("Line")
DB.CUserLog = DB.DB.Collection("UserLog")
DB.CSystemLog = DB.DB.Collection("SystemLog")
DB.CInvestigation = DB.DB.Collection("Investigation")
DB.CTrajectory = DB.DB.Collection("Trajectory")
DB.CIcons = DB.DB.Collection("Icons")
DB.CTopMenus = DB.DB.Collection("TopMenus")
DB.CDevice = DB.DB.Collection("Device")
DelayMessage.CDelayMessage = DB.DB.Collection("DelayMessage")
DelayMessage.CDelayErrorLog = DB.DB.Collection("DelayErrorLog")
// 连接redis
DB.Redis = Cache.NewRedis(&Cache.RedisOpts{
conf.RedisPath,
"",
0,
200,
20,
0,
})
r := gin.Default()
//r.Static("/.well-known", "./.well-known/")
r.GET("/AllItems", Api.AllItems)
r.GET("/AllItemTime", Api.AllItemTime)
r.GET("/AllCommodity", Api.AllCommodity)
r.GET("/AllLine", Api.AllLine)
r.GET("/ItemInfo", Api.ItemInfo)
r.GET("/CommodityInfo", Api.CommodityInfo)
r.POST("/CreateComplaint", Api.CreateComplaint)
r.GET("/AllComplaint", Api.AllComplaint)
//r.POST("/CreateUser", Api.CreateUser)
r.POST("/LoginUser", Api.LoginUser)
r.POST("/UpdateUser", Api.UpdateUser)
r.GET("/UserInfo", Api.UserInfo)
r.GET("/ScenicInfo", Api.ScenicInfo)
r.GET("/LineInfo", Api.LineInfo)
r.GET("/AllTag", Api.AllTag)
r.GET("/AllTagGroup", Api.AllTagGroup)
r.POST("/Tag/Create", Api.CreateTag)
r.POST("/Tag/Remove", Api.RemoveTag)
r.POST("/Upload", Api.Upload)
r.POST("/UpdateItem", Api.UpdateItem)
r.POST("/UpdateCommodity", Api.UpdateCommodity)
r.POST("/UpdateLine", Api.UpdateLine)
r.POST("/UpdateScenic", Api.UpdateScenic)
r.POST("/UpdateItemTime", Api.UpdateItemTime)
r.GET("/AllScenic", Api.AllScenic)
r.POST("/UserLog", Api.UserLog)
r.GET("/AllUserLog", Api.AllUserLog)
r.POST("/Sms/Send", Api.Send)
r.POST("/Investigation/Save", Api.SaveInvestigation)
r.GET("/Investigation/List", Api.AllInvestigation)
r.POST("/Trajectory/Save", Api.SaveTrajectory)
r.POST("/DealyMessage/Create", Api.CreateDealyMessage)
r.GET("/DealyMessage/Info", Api.DealyMessageInfo)
r.POST("/DealyMessage/Remove", Api.RemoveDealyMessage)
r.POST("/Icon/Update", Api.UpdateIcon)
r.GET("/Icon/All", Api.AllIcons)
r.GET("/Icon/Info", Api.IconInfo)
r.POST("/CheckToken", Api.CheckToken)
//r.GET("/Tiles", Api.Tiles)
r.POST("/TopMenus/Update", Api.UpdateTopMenus)
r.GET("/TopMenus/All", Api.AllTopMenus)
r.POST("/RegisterDevice", Api.RegisterDevice)
r.POST("/RemoveUser", Api.RemoveUser)
//r.GET("/ws", Api.WsPage)
r.Static("/Upload", "./Upload")
r.Static("/Console", "./Console")
r.Static("/Policy", dir+"/Policy")
r.GET("MP_verify_R9xuhLXYcVbdDDNk.txt", func(c *gin.Context) {
c.String(200, "R9xuhLXYcVbdDDNk")
})
//r.Static("/tiles2", dir+"/tiles")
// go Ws.Manager.Start()
// 创建延迟消息
DelayMessage.GlobalDM = DelayMessage.NewDelayMessage()
go func() {
DelayMessage.GlobalDM.Start()
}()
// -初始化数据
if cur, err := DelayMessage.CDelayMessage.Find(tools.GetContext(), bson.M{}); err == nil {
defer cur.Close(tools.GetContext())
for cur.Next(tools.GetContext()) {
var message DelayMessage.Message
err := cur.Decode(&message)
tools.CheckError(err)
nowTimeU := time.Now().Unix()
iDelayTIme := message.DelayTime - nowTimeU
if iDelayTIme < 0 {
iDelayTIme = 1
}
DelayMessage.GlobalDM.AddTask(time.Now().Add(time.Second*time.Duration(iDelayTIme)), DelayMessage.Callback, &message)
log.Println("增加提醒任务", message)
}
} else {
spew.Dump(err)
}
r.Run(":8080")
}