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") }