main.go 3.94 KB
package main

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"letu/Api"
	"letu/Config"
	"letu/DB"
	"letu/Lib/Cache"
	"letu/Lib/DelayMessage"
	"os"
	"time"
)

// @APIVersion 1.0.0
// @APITitle 乐游图后端接口文档
// @BasePath https://letu.api.imagchina.com
func main() {

	// 读取配置文件
	dir, _ := os.Getwd()
	//println(dir)
	file, _ := os.Open(dir + "/Config/config.json")
	defer file.Close()
	decoder := json.NewDecoder(file)
	conf := Config.Config{}
	err := decoder.Decode(&conf)
	tools.CheckError(err)

	// 连接数据库
	DB.DBSession, err = mgo.Dial(conf.DbPath)

	defer DB.DBSession.Close()

	// 连接redis
	DB.Redis = Cache.NewRedis(&Cache.RedisOpts{
		conf.RedisPath,
		"",
		0,
		20,
		20,
		0,
	})

	//设置模式
	DB.DBSession.SetMode(mgo.Monotonic, true)
	//获取文档集
	DB.DB = DB.DBSession.DB(conf.DbName)
	DB.DB.Login(conf.DbUser, conf.DbPassword)

	DB.CItem = DB.DB.C("Item")
	DB.CComplaint = DB.DB.C("Complaint")
	DB.CInvestigation = DB.DB.C("Investigation")
	DB.CMember = DB.DB.C("Member")
	DB.CCommodity = DB.DB.C("Commodity")
	DB.CTags = DB.DB.C("Tags")
	DB.CScenic = DB.DB.C("Scenic")
	DB.CLine = DB.DB.C("Line")
	DB.CUserLog = DB.DB.C("UserLog")
	DB.CSystemLog = DB.DB.C("SystemLog")
	DB.CInvestigation = DB.DB.C("Investigation")
	DB.CTrajectory = DB.DB.C("Trajectory")
	DB.CIcons = DB.DB.C("Icons")
	DelayMessage.CDelayMessage = DB.DB.C("DelayMessage")
	DelayMessage.CDelayErrorLog = DB.DB.C("DelayErrorLog")


	// 设置接口地址
	//controllers := LeYouTu.Controllers{}
	//controllers.SetLayout(Api.Layout)

	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.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.GET("/ws", Api.WsPage)

	r.Static("/Upload", "./Upload")
	r.Static("/Console", "./Console")
	r.Static("/Policy", dir+"/Policy")
	r.Static("/tiles2", dir+"/tiles")
	// go Ws.Manager.Start()

	// 创建延迟消息
	DelayMessage.GlobalDM = DelayMessage.NewDelayMessage()

	go func() {
		DelayMessage.GlobalDM.Start()
	}()

	// -初始化数据
	var aMessage []DelayMessage.Message
	DelayMessage.CDelayMessage.Find(bson.M{}).All(&aMessage)
	nowTimeU := time.Now().Unix()
	for i := 0; i < len(aMessage); i++ {
		iDelayTIme := aMessage[i].DelayTime - nowTimeU

		if iDelayTIme < 0 {
			iDelayTIme = 1
		}
		DelayMessage.GlobalDM.AddTask(time.Now().Add(time.Second*time.Duration(iDelayTIme)), DelayMessage.Callback, &aMessage[i])
	}
	println("增加", len(aMessage), "条提醒任务")

	r.Run(":8080")
}