mirror of
https://github.com/arnaucube/gogame.git
synced 2026-02-06 19:16:40 +01:00
minimal register&login working
This commit is contained in:
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
@@ -1,6 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/arnaucube/gogame/config"
|
||||
"github.com/arnaucube/gogame/database"
|
||||
"github.com/arnaucube/gogame/endpoint"
|
||||
"github.com/arnaucube/gogame/services/usersrv"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -9,16 +13,28 @@ var ServerCommands = []cli.Command{
|
||||
Name: "start",
|
||||
Aliases: []string{},
|
||||
Usage: "start the server",
|
||||
Action: cmdStart,
|
||||
Action: start,
|
||||
},
|
||||
}
|
||||
|
||||
func cmdStart(c *cli.Context) error {
|
||||
func start(c *cli.Context) error {
|
||||
if err := config.MustRead(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endpoint.Serve()
|
||||
db, err := database.New(config.C.Mongodb.Url, config.C.Mongodb.Database)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// services
|
||||
userservice := usersrv.New(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiService := endpoint.Serve(config.C, db, userservice)
|
||||
apiService.Run(config.C.Server.ServiceApi)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
6
config.yaml
Normal file
6
config.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
server:
|
||||
serviceapi: 0.0.0.0:5000
|
||||
adminapi: 127.0.0.1:5001
|
||||
mongodb:
|
||||
url: 127.0.0.1:27017
|
||||
database: "gogame"
|
||||
@@ -14,6 +14,7 @@ type Config struct {
|
||||
}
|
||||
Mongodb struct {
|
||||
Url string
|
||||
Database string
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,13 +39,5 @@ func MustRead(c *cli.Context) error {
|
||||
if err := viper.Unmarshal(&C); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
if C.Id, err = core.IDFromString(C.IdRaw); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := C.KeyStoreBaby.PubKey.UnmarshalText([]byte(C.KeyStoreBaby.PubKeyRaw)); err != nil {
|
||||
return err
|
||||
}
|
||||
C.KeyStoreBaby.PubKeyComp = C.KeyStoreBaby.PubKey.Compress()
|
||||
return nil
|
||||
}
|
||||
|
||||
61
endpoint/handlers.go
Normal file
61
endpoint/handlers.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package endpoint
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func fail(c *gin.Context, err error, msg string) {
|
||||
color.Red(msg + ": " + err.Error())
|
||||
c.JSON(400, gin.H{
|
||||
"error": msg,
|
||||
})
|
||||
}
|
||||
func handleMain(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "wellcome to gogame",
|
||||
})
|
||||
}
|
||||
|
||||
func handleInfo(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"info": "info",
|
||||
})
|
||||
}
|
||||
|
||||
type RegisterMsg struct {
|
||||
Name string `binding:"required"`
|
||||
Password string `binding:"required"`
|
||||
Email string `binding:"required"`
|
||||
}
|
||||
|
||||
func handleRegister(c *gin.Context) {
|
||||
var registerMsg RegisterMsg
|
||||
c.BindJSON(®isterMsg)
|
||||
user, err := userservice.Register(registerMsg.Name, registerMsg.Password, registerMsg.Email)
|
||||
if err != nil {
|
||||
fail(c, err, "error on register")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
type LoginMsg struct {
|
||||
Email string `binding:"required"`
|
||||
Password string `binding:"required"`
|
||||
}
|
||||
|
||||
func handleLogin(c *gin.Context) {
|
||||
var loginMsg LoginMsg
|
||||
c.BindJSON(&loginMsg)
|
||||
user, err := userservice.Login(loginMsg.Email, loginMsg.Password)
|
||||
if err != nil {
|
||||
fail(c, err, "error on login")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
30
endpoint/serve.go
Normal file
30
endpoint/serve.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package endpoint
|
||||
|
||||
import (
|
||||
"github.com/arnaucube/gogame/config"
|
||||
"github.com/arnaucube/gogame/database"
|
||||
"github.com/arnaucube/gogame/services/usersrv"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var serverConfig config.Config
|
||||
var db *database.Db
|
||||
var userservice *usersrv.Service
|
||||
|
||||
func newApiService() *gin.Engine {
|
||||
api := gin.Default()
|
||||
api.Use(cors.Default())
|
||||
api.GET("/", handleMain)
|
||||
api.GET("/info", handleInfo)
|
||||
api.POST("/register", handleRegister)
|
||||
api.POST("/login", handleLogin)
|
||||
return api
|
||||
}
|
||||
|
||||
func Serve(cnfg config.Config, _db *database.Db, _userservice *usersrv.Service) *gin.Engine {
|
||||
serverConfig = cnfg
|
||||
db = _db
|
||||
userservice = _userservice
|
||||
return newApiService()
|
||||
}
|
||||
10
go.mod
10
go.mod
@@ -1,12 +1,20 @@
|
||||
module gogame
|
||||
module github.com/arnaucube/gogame
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
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
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/urfave/cli v1.20.0
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
|
||||
)
|
||||
|
||||
replace githoub.com/arnaucube/gogame => ./
|
||||
|
||||
25
go.sum
25
go.sum
@@ -16,6 +16,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
|
||||
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/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
@@ -23,6 +24,12 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/gin-contrib/cors v1.3.0 h1:PolezCc89peu+NgkIWt9OB01Kbzt6IP0J/JvkG6xxlg=
|
||||
github.com/gin-contrib/cors v1.3.0/go.mod h1:artPvLlhkF7oG06nK8v3U8TNz6IeX+w1uzCSEId5/Vc=
|
||||
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
|
||||
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
|
||||
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
|
||||
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
|
||||
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=
|
||||
@@ -33,6 +40,7 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
|
||||
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
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/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
@@ -43,6 +51,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
@@ -55,11 +64,18 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
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/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=
|
||||
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab h1:n8cgpHzJ5+EDyDri2s/GC7a9+qK3/YEGnBsd0uS/8PY=
|
||||
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab/go.mod h1:y1pL58r5z2VvAjeG1VLGc8zOQgSOzbKN7kMHPvFXJ+8=
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
|
||||
@@ -96,6 +112,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
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=
|
||||
@@ -106,6 +123,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-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=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
@@ -113,6 +131,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -122,10 +141,11 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
@@ -139,6 +159,9 @@ google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
|
||||
2
main.go
2
main.go
@@ -3,8 +3,8 @@ package main
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/arnaucube/gogame/cmd"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gogame/cmd"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ type Resource struct {
|
||||
type User struct {
|
||||
Id string
|
||||
Name string
|
||||
Password string
|
||||
Email string
|
||||
Resources struct {
|
||||
Metal Resource
|
||||
|
||||
1
parts.md
1
parts.md
@@ -10,3 +10,4 @@
|
||||
- planet engine
|
||||
- building engine
|
||||
- battle engine
|
||||
- log system (console print and file save)
|
||||
|
||||
@@ -1,5 +1,87 @@
|
||||
package usersrv
|
||||
|
||||
func New() {
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/arnaucube/gogame/database"
|
||||
"github.com/arnaucube/gogame/models"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *database.Db
|
||||
}
|
||||
|
||||
func New(db *database.Db) *Service {
|
||||
return &Service{
|
||||
db,
|
||||
}
|
||||
}
|
||||
|
||||
func hashPassword(password string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
|
||||
return string(hash), err
|
||||
}
|
||||
func checkPasswordHash(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (srv Service) Register(name, password, email string) (*models.User, error) {
|
||||
var user models.User
|
||||
err := srv.db.Users.Find(bson.M{"email": email}).One(&user)
|
||||
if err == nil {
|
||||
return nil, errors.New("user already exist")
|
||||
}
|
||||
|
||||
hashedPassword, err := hashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newUser := models.User{
|
||||
Name: name,
|
||||
Password: hashedPassword,
|
||||
Email: email,
|
||||
}
|
||||
err = srv.db.Users.Insert(newUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = srv.db.Users.Find(bson.M{"email": email}).One(&user)
|
||||
user.Password = ""
|
||||
return &user, err
|
||||
}
|
||||
|
||||
var signingKey = []byte("TODO") // TODO
|
||||
|
||||
func (srv Service) Login(email, password string) (*models.User, error) {
|
||||
var user models.User
|
||||
err := srv.db.Users.Find(bson.M{"email": email}).One(&user)
|
||||
if err != nil {
|
||||
return nil, errors.New("user not exist")
|
||||
}
|
||||
if !checkPasswordHash(password, user.Password) {
|
||||
return nil, errors.New("error with password")
|
||||
}
|
||||
|
||||
// create jwt
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
claims := make(jwt.MapClaims)
|
||||
claims["user"] = true
|
||||
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
|
||||
token.Claims = claims
|
||||
|
||||
tokenString, err := token.SignedString(signingKey)
|
||||
if err != nil {
|
||||
return nil, errors.New("error creating token")
|
||||
}
|
||||
|
||||
// TODO
|
||||
// reuse Password parameter, to put there the token
|
||||
user.Password = tokenString
|
||||
|
||||
return &user, err
|
||||
}
|
||||
|
||||
1
test/.gitignore
vendored
Normal file
1
test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
56
test/package-lock.json
generated
Normal file
56
test/package-lock.json
generated
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "gogame-test",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"axios": {
|
||||
"version": "0.19.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz",
|
||||
"integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==",
|
||||
"requires": {
|
||||
"follow-redirects": "1.5.10",
|
||||
"is-buffer": "^2.0.2"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.5.10",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
|
||||
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
|
||||
"requires": {
|
||||
"debug": "=3.1.0"
|
||||
}
|
||||
},
|
||||
"is-buffer": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz",
|
||||
"integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw=="
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"nan": {
|
||||
"version": "2.14.0",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
|
||||
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg=="
|
||||
},
|
||||
"sleep": {
|
||||
"version": "6.1.0",
|
||||
"resolved": "https://registry.npmjs.org/sleep/-/sleep-6.1.0.tgz",
|
||||
"integrity": "sha512-Z1x4JjJxsru75Tqn8F4tnOFeEu3HjtITTsumYUiuz54sGKdISgLCek9AUlXlVVrkhltRFhNUsJDJE76SFHTDIQ==",
|
||||
"requires": {
|
||||
"nan": "^2.13.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
test/package.json
Normal file
23
test/package.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "gogame-test",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "test.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/arnaucube/gogame.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/arnaucube/gogame/issues"
|
||||
},
|
||||
"homepage": "https://github.com/arnaucube/gogame#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"sleep": "^6.1.0"
|
||||
}
|
||||
}
|
||||
31
test/test.js
Normal file
31
test/test.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const axios = require('axios');
|
||||
const sleep = require('sleep');
|
||||
|
||||
const url = 'http://127.0.0.1:5000';
|
||||
|
||||
let newUser = {
|
||||
name: 'user00',
|
||||
password: 'user00password',
|
||||
email: 'user00@email.com'
|
||||
};
|
||||
axios.post(url + '/register', newUser)
|
||||
.then(function (res) {
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error.response.data);
|
||||
});
|
||||
|
||||
// sleep.sleep(3);
|
||||
|
||||
let user = {
|
||||
email: 'user00@email.com',
|
||||
password: 'user00password'
|
||||
};
|
||||
axios.post(url + '/login', user)
|
||||
.then(function (res) {
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error.response.data);
|
||||
});
|
||||
Reference in New Issue
Block a user