gogame-old/endpoint/serve.go
endpoint/serve.goRaw
1package endpoint
2
3import (
4 "log"
5 "time"
6
7 jwt "github.com/appleboy/gin-jwt/v2"
8 "github.com/arnaucube/gogame/config"
9 "github.com/arnaucube/gogame/constants"
10 "github.com/arnaucube/gogame/database"
11 "github.com/arnaucube/gogame/models"
12 "github.com/arnaucube/gogame/services/gamesrv"
13 "github.com/arnaucube/gogame/services/usersrv"
14 "github.com/gin-contrib/cors"
15 "github.com/gin-gonic/gin"
16 "gopkg.in/mgo.v2/bson"
17)
18
19var serverConfig config.Config
20var db *database.Db
21var userservice *usersrv.Service
22var gameservice *gamesrv.Service
23
24func newApiService() *gin.Engine {
25 api := gin.Default()
26 api.Use(cors.Default())
27
28 // the jwt middleware
29 authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
30 Realm: "test zone",
31 Key: []byte("secret key"),
32 Timeout: time.Hour,
33 MaxRefresh: time.Hour,
34 IdentityKey: constants.JWTIdKey,
35 PayloadFunc: func(data interface{}) jwt.MapClaims {
36 if v, ok := data.(*models.User); ok {
37 return jwt.MapClaims{
38 constants.JWTIdKey: v.Id,
39 }
40 }
41 return jwt.MapClaims{}
42 },
43 IdentityHandler: func(c *gin.Context) interface{} {
44 claims := jwt.ExtractClaims(c)
45 userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
46 return &models.User{
47 Id: userid,
48 }
49 },
50 Authenticator: func(c *gin.Context) (interface{}, error) {
51 var loginMsg LoginMsg
52 if err := c.ShouldBind(&loginMsg); err != nil {
53 return "", jwt.ErrMissingLoginValues
54 }
55
56 _, user, err := userservice.Login(loginMsg.Email, loginMsg.Password)
57 if err != nil {
58 fail(c, err, jwt.ErrFailedAuthentication.Error())
59 return "", err
60 }
61 return user, nil
62
63 },
64 Unauthorized: func(c *gin.Context, code int, message string) {
65 c.JSON(code, gin.H{
66 "code": code,
67 "message": message,
68 })
69 },
70 TokenLookup: "header: Authorization",
71
72 // TokenHeadName is a string in the header. Default value is "Bearer"
73 TokenHeadName: "Bearer",
74
75 // TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
76 TimeFunc: time.Now,
77 })
78 if err != nil {
79 log.Fatal("JWT Error:" + err.Error())
80 }
81
82 api.GET("/info", handleInfo)
83 api.POST("/register", handleRegister)
84 // api.POST("/login", handleLogin)
85 api.POST("/login", authMiddleware.LoginHandler)
86 api.GET("/refresh_token", authMiddleware.RefreshHandler)
87
88 api.Use(authMiddleware.MiddlewareFunc())
89 {
90 api.GET("/", handleGetUser)
91 api.GET("/resources", handleGetResources)
92 api.GET("/planets", handleGetUserPlanets)
93 api.POST("/buildings", handlePostUpgradeBuilding)
94 }
95 return api
96}
97
98func Serve(cnfg config.Config, _db *database.Db, _userservice *usersrv.Service, _gameservice *gamesrv.Service) *gin.Engine {
99 serverConfig = cnfg
100 db = _db
101 userservice = _userservice
102 gameservice = _gameservice
103 return newApiService()
104}