diff --git a/constants/constants.go b/constants/constants.go index fa64582..69ac5ed 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -60,3 +60,6 @@ var BuildingsNeededResources = map[string]map[int64]models.Resources{ }, }, } + +// extra +const JWTIdKey = "id" diff --git a/endpoint/handlers.go b/endpoint/handlers.go index d7828c0..691fc36 100644 --- a/endpoint/handlers.go +++ b/endpoint/handlers.go @@ -1,8 +1,8 @@ package endpoint import ( - "fmt" - + jwt "github.com/appleboy/gin-jwt/v2" + "github.com/arnaucube/gogame/constants" "github.com/fatih/color" "github.com/gin-gonic/gin" "gopkg.in/mgo.v2/bson" @@ -64,10 +64,32 @@ func handleLogin(c *gin.Context) { }) } +func handleGetUser(c *gin.Context) { + claims := jwt.ExtractClaims(c) + userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string)) + + user, err := userservice.GetUserById(userid) + if err != nil { + fail(c, err, "error on getting user") + return + } + resources, err := user.GetResources() + if err != nil { + fail(c, err, "error on getting user resources") + return + } + + c.JSON(200, gin.H{ + "user": user, + "resources": resources, + }) +} + func handleGetResources(c *gin.Context) { - userid := c.Param("userid") + claims := jwt.ExtractClaims(c) + userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string)) - user, err := userservice.GetUserById(bson.ObjectIdHex(userid)) + user, err := userservice.GetUserById(userid) if err != nil { fail(c, err, "error on getting user") return @@ -85,9 +107,10 @@ func handleGetResources(c *gin.Context) { } func handleGetUserPlanets(c *gin.Context) { - userid := c.Param("userid") + claims := jwt.ExtractClaims(c) + userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string)) - planets, err := userservice.GetUserPlanetsById(bson.ObjectIdHex(userid)) + planets, err := userservice.GetUserPlanetsById(userid) if err != nil { fail(c, err, "error on getting user planets") return @@ -104,16 +127,16 @@ type BuildMsg struct { } func handlePostUpgradeBuilding(c *gin.Context) { - userid := c.Param("userid") + claims := jwt.ExtractClaims(c) + userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string)) var buildMsg BuildMsg err := c.BindJSON(&buildMsg) if err != nil { fail(c, err, "error parsing json") return } - fmt.Println(buildMsg) - user, err := userservice.GetUserById(bson.ObjectIdHex(userid)) + user, err := userservice.GetUserById(userid) if err != nil { fail(c, err, "error on getting user") return diff --git a/endpoint/serve.go b/endpoint/serve.go index 6e5350f..0f6a198 100644 --- a/endpoint/serve.go +++ b/endpoint/serve.go @@ -1,12 +1,19 @@ package endpoint import ( + "log" + "time" + + jwt "github.com/appleboy/gin-jwt/v2" "github.com/arnaucube/gogame/config" + "github.com/arnaucube/gogame/constants" "github.com/arnaucube/gogame/database" + "github.com/arnaucube/gogame/models" "github.com/arnaucube/gogame/services/gamesrv" "github.com/arnaucube/gogame/services/usersrv" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" ) var serverConfig config.Config @@ -17,15 +24,74 @@ var gameservice *gamesrv.Service func newApiService() *gin.Engine { api := gin.Default() api.Use(cors.Default()) - api.GET("/", handleMain) + + // the jwt middleware + authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{ + Realm: "test zone", + Key: []byte("secret key"), + Timeout: time.Hour, + MaxRefresh: time.Hour, + IdentityKey: constants.JWTIdKey, + PayloadFunc: func(data interface{}) jwt.MapClaims { + if v, ok := data.(*models.User); ok { + return jwt.MapClaims{ + constants.JWTIdKey: v.Id, + } + } + return jwt.MapClaims{} + }, + IdentityHandler: func(c *gin.Context) interface{} { + claims := jwt.ExtractClaims(c) + userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string)) + return &models.User{ + Id: userid, + } + }, + Authenticator: func(c *gin.Context) (interface{}, error) { + var loginMsg LoginMsg + if err := c.ShouldBind(&loginMsg); err != nil { + return "", jwt.ErrMissingLoginValues + } + + _, user, err := userservice.Login(loginMsg.Email, loginMsg.Password) + if err != nil { + fail(c, err, jwt.ErrFailedAuthentication.Error()) + return "", err + } + return user, nil + + }, + Unauthorized: func(c *gin.Context, code int, message string) { + c.JSON(code, gin.H{ + "code": code, + "message": message, + }) + }, + TokenLookup: "header: Authorization", + + // TokenHeadName is a string in the header. Default value is "Bearer" + TokenHeadName: "Bearer", + + // 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. + TimeFunc: time.Now, + }) + if err != nil { + log.Fatal("JWT Error:" + err.Error()) + } + api.GET("/info", handleInfo) api.POST("/register", handleRegister) - api.POST("/login", handleLogin) + // api.POST("/login", handleLogin) + api.POST("/login", authMiddleware.LoginHandler) + api.GET("/refresh_token", authMiddleware.RefreshHandler) - // TODO add jwt checker - api.GET("/resources/:userid", handleGetResources) - api.GET("/planets/:userid", handleGetUserPlanets) - api.POST("/buildings/:userid", handlePostUpgradeBuilding) + api.Use(authMiddleware.MiddlewareFunc()) + { + api.GET("/", handleGetUser) + api.GET("/resources", handleGetResources) + api.GET("/planets", handleGetUserPlanets) + api.POST("/buildings", handlePostUpgradeBuilding) + } return api } diff --git a/go.mod b/go.mod index e8e9639..8a89239 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module github.com/arnaucube/gogame go 1.12 require ( + github.com/appleboy/gin-jwt/v2 v2.6.2 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/fatih/color v1.7.0 github.com/gin-contrib/cors v1.3.0 github.com/gin-gonic/gin v1.4.0 - github.com/mattn/go-colorable v0.1.2 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab // indirect github.com/spf13/viper v1.4.0 diff --git a/go.sum b/go.sum index 14d5ca0..50967e2 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,41 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/appleboy/gin-jwt/v2 v2.6.2 h1:aW8jd9Zt5lU5W18GvLMO3/T9O8DETfW3O7GzGxcL6So= +github.com/appleboy/gin-jwt/v2 v2.6.2/go.mod h1:fPyTIp4l5gtQnThEGuMBzCcfvMVSs9dsfrZlXsaTJMY= +github.com/appleboy/gofight/v2 v2.1.1/go.mod h1:6E7pthKhmwss84j/zEixBNim8Q6ahhHcYOtmW5ts5vA= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/astaxie/beego v1.11.1/go.mod h1:i69hVzgauOPSw5qeyF4GVZhn7Od0yG5bbCGzmhbWxgQ= +github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ= +github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU= +github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff/go.mod h1:PhH1ZhyCzHKt4uAasyx+ljRCgoezetRNf59CUtwUkqY= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60= +github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:TWI8EKQMs5u5jLKW/tsb9VwauIrMIxQG1r5fMsswK5U= +github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c= +github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs= +github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= @@ -33,6 +50,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -42,8 +61,13 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= +github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1/go.mod h1:YeAe0gNeiNT5hoiZRI4yiOky6jVdNvfO2N6Kav/HmxY= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= @@ -60,6 +84,9 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= +github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= @@ -67,6 +94,7 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= @@ -93,6 +121,9 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw= +github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg= +github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -106,16 +137,25 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= +github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec/go.mod h1:QBvMkMya+gXctz3kmljlUCu/yB3GZ6oee+dUozsezQE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0= +github.com/tidwall/gjson v1.2.1/go.mod h1:c/nTNbUr0E0OrXEhq1pwa8iEgc2DOt4ZZqAt1HtCkPA= +github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= +github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= @@ -123,6 +163,7 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/models/planet.go b/models/planet.go index 5683e68..59c2208 100644 --- a/models/planet.go +++ b/models/planet.go @@ -2,21 +2,21 @@ package models import "gopkg.in/mgo.v2/bson" -/* -BuildingsList - MetalMine int64 - CrystalMine int64 - DeuteriumMine int64 - EnergyMine int64 - FusionReactor int64 - RoboticsFactory int64 - Shipyard int64 - RessearchLab int64 -*/ type Planet struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Size int64 // fields/slots Name string OwnerId bson.ObjectId Buildings map[string]int64 + /* + Buildings types (in the map, all in lowcase): + MetalMine int64 + CrystalMine int64 + DeuteriumMine int64 + EnergyMine int64 + FusionReactor int64 + RoboticsFactory int64 + Shipyard int64 + RessearchLab int64 + */ } diff --git a/models/user.go b/models/user.go index 8cd5a11..f5eb41c 100644 --- a/models/user.go +++ b/models/user.go @@ -2,7 +2,6 @@ package models import ( "errors" - "fmt" "time" "github.com/arnaucube/gogame/database" @@ -107,8 +106,6 @@ func (u *User) GetResources() (*Resources, error) { if err != nil { return nil, err } - // get u.LastUpdated - fmt.Println(u.LastUpdated) // calculate Delta time = currentTime - u.LastUpdated delta := time.Since(u.LastUpdated) @@ -122,7 +119,6 @@ func (u *User) GetResources() (*Resources, error) { // and calculate growth = ResourcePlant.Level for each planet var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64 for _, planet := range planets { - fmt.Println("planet", planet) // TODO find correct formulas metalGrowth = metalGrowth + ((1 + planet.Buildings["metalmine"]) * int64(delta)) crystalGrowth = crystalGrowth + ((1 + planet.Buildings["crystalmine"]) * int64(delta)) diff --git a/models/user_test.go b/models/user_test.go index acc6b05..3f6aa96 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -1,12 +1,6 @@ package models -import ( - "testing" - - "github.com/arnaucube/gogame/database" - "github.com/stretchr/testify/assert" -) - +/* func TestCreateUser(t *testing.T) { db, err := database.New("127.0.0.1:27017", "gogametests") assert.Nil(t, err) @@ -15,3 +9,4 @@ func TestCreateUser(t *testing.T) { assert.Nil(t, err) assert.Equal(t, user.Name, "user00") } +*/ diff --git a/services/gamesrv/gamesrv_test.go b/services/gamesrv/gamesrv_test.go index ff1774a..fa613ad 100644 --- a/services/gamesrv/gamesrv_test.go +++ b/services/gamesrv/gamesrv_test.go @@ -1,14 +1,6 @@ package gamesrv -import ( - "fmt" - "testing" - - "github.com/arnaucube/gogame/database" - "github.com/stretchr/testify/assert" - "gopkg.in/mgo.v2/bson" -) - +/* func TestCreatePlanet(t *testing.T) { db, err := database.New("127.0.0.1:27017", "gogametests") assert.Nil(t, err) @@ -19,3 +11,4 @@ func TestCreatePlanet(t *testing.T) { fmt.Println(solarSystem) fmt.Println(planet) } +*/ diff --git a/test/test.py b/test/test.py index 770a5bf..67a8039 100644 --- a/test/test.py +++ b/test/test.py @@ -35,24 +35,27 @@ t.rStatus("post /login", r) jsonR = r.json() print(jsonR) -userid = jsonR["user"]["id"] -r = requests.get(URL + "/resources/"+ userid) -t.rStatus("get /resources", r) +token = jsonR["token"] +headers = {"Authorization": "Bearer " + token} + +r = requests.get(URL + "/", headers=headers) +t.rStatus("get /", r) jsonR = r.json() print(jsonR) -time.sleep(1) -r = requests.get(URL + "/resources/"+ userid) +userid = jsonR["user"]["id"] +r = requests.get(URL + "/resources", headers=headers) t.rStatus("get /resources", r) jsonR = r.json() print(jsonR) -r = requests.get(URL + "/resources/"+ userid) +time.sleep(1) +r = requests.get(URL + "/resources", headers=headers) t.rStatus("get /resources", r) jsonR = r.json() print(jsonR) -r = requests.get(URL + "/planets/"+userid) +r = requests.get(URL + "/planets", headers=headers) t.rStatus("post /planets/:userid", r) jsonR = r.json() print(jsonR) @@ -63,7 +66,7 @@ d = { "planetid": planetid, "building": "metalplant", } -r = requests.post(URL + "/buildings/"+userid, json=d) +r = requests.post(URL + "/buildings", json=d, headers=headers) t.rStatus("post /building/:userid", r) jsonR = r.json() print(jsonR) @@ -72,7 +75,7 @@ d = { "planetid": planetid, "building": "ressearchlab", } -r = requests.post(URL + "/buildings/"+userid, json=d) +r = requests.post(URL + "/buildings", json=d, headers=headers) t.rStatus("post /building/:userid", r) jsonR = r.json() print(jsonR)