123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package cmd
- import (
- "context"
- "demo/internal/controller"
- "demo/internal/middleware/jwt"
- "encoding/json"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/gogf/gf/v2/os/gcmd"
- "github.com/siddontang/go/log"
- )
- var (
- Main = gcmd.Command{
- Name: "main",
- Usage: "main",
- Brief: "start http server",
- Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
- s := g.Server()
- s.BindStatusHandler(404, func(r *ghttp.Request) {
- r.Response.Writeln("This is customized 404 page")
- })
- s.SetIndexFolder(true)
- //s.SetServerRoot("")
- //s.SetIndexFiles([]string{"data/index.html"})
- s.AddSearchPath("dist")
- s.AddSearchPath("")
- s.Use(MiddlewareErrorHandler)
- s.Use(MiddlewareCORS)
- s.Group("/", func(group *ghttp.RouterGroup) {
- group.Middleware(MiddlewareCORS)
- group.Middleware(ghttp.MiddlewareHandlerResponse)
- group.POST("/auth", controller.Auth)
- group.ALL("/download", func(r *ghttp.Request) {
- r.Response.ServeFileDownload("file/" + r.Get("filename").String())
- })
- })
- //s.BindHandler("/download", func(r *ghttp.Request) {
- // r.Response.ServeFileDownload("file/" + r.Get("filename").String())
- //})
- s.Group("/", func(group *ghttp.RouterGroup) {
- group.Middleware(ghttp.MiddlewareHandlerResponse)
- group.Middleware(MiddlewareCORS)
- group.Middleware(jwt.JWT)
- //脖环清单
- group.GET("/ring/listing", controller.GetNeckRingListing)
- //group.POST("/ring/listing", controller.AddNeckRingListing)
- //接收器管理
- group.GET("/receiver/list", controller.GetReceiver)
- group.POST("/receiver/add", controller.AddReceiver)
- group.POST("/receiver/edit", controller.EditReceiver)
- //sim卡管理
- group.GET("/sim/list", controller.GetSim)
- group.POST("/sim/add", controller.AddSim)
- group.POST("/sim/edit", controller.EditSim)
- //脖环出厂登记
- group.POST("/factory/add", controller.AddFactory)
- group.GET("/factory/list", controller.GetFactory)
- //脖环召回记录
- group.POST("/recall/add", controller.AddRecall)
- group.GET("/recall/list", controller.GetRecall)
- //脖环管理
- group.GET("/management/list", controller.GetManagement)
- group.POST("/management/add", controller.AddManagement)
- group.GET("/managementbh/list", controller.GetManagementBh)
- //group.GET("/usetj", controller.GetUseTj)
- //图片查看
- group.GET("/image", controller.GetImage)
- //图片上传
- group.POST("/housephy/image", controller.UploadImage)
- //查看接收器坐标
- group.GET("/receiver/coordinates", controller.GetReceiverInfo)
- group.POST("/receiver/coordinates/edit", controller.EditReceiverCoordinates)
- //公共接口
- group.GET("/public/bar", controller.GetBarList)
- group.GET("/public/pasture", controller.GetPastureList)
- group.GET("/public/housephy", controller.GetHousephyList)
- group.GET("/sim/pull", controller.GetSimPull)
- group.GET("/receiver/pull", controller.GetReceiverPull)
- //权限等接口
- group.POST("/authdata/GetDataByNames", controller.GetDataByNames)
- group.POST("/authdata/GetRecuDataByName", controller.GetRecuDataByName)
- group.GET("/authdata/userinfo", controller.GetUserInfo)
- group.POST("/authdata/ExecDataByConfig", controller.ExecDataByConfig)
- group.POST("/authdata/GetDataByName", controller.GetDataByName)
- group.POST("/authdata/PostDataByName", controller.PostDataByName)
- group.POST("/authdata/logout", controller.UserLogout)
- })
- conf, _ := g.Cfg().Get(context.Background(), "server")
- port, _ := conf.Map()["address"].(json.Number).Int64()
- s.SetPort(int(port))
- s.Run()
- return nil
- },
- }
- )
- func MiddlewareErrorHandler(r *ghttp.Request) {
- r.Middleware.Next()
- if err := r.GetError(); err != nil {
- // 记录到自定义错误日志文件
- g.Log("exception").Error(context.Background(), err)
- //返回固定的友好信息
- r.Response.ClearBuffer()
- r.Response.Writeln("服务器居然开小差了,请稍后再试吧!")
- }
- }
- func MiddlewareCORS(r *ghttp.Request) {
- log.Info(r.Router.Method, " ", r.Router.Uri)
- r.Response.CORSDefault()
- r.Middleware.Next()
- }
|