Commit b3d7971992cff398296f5663ff41786afd683106
1 parent
58e37bfe
fix: 返回值如果code=0则成功,返回data
如果code!=0则失败,返回message
Showing
2 changed files
with
19 additions
and
14 deletions
Show diff stats
cmd/httpserver/http.go
| ... | ... | @@ -21,7 +21,7 @@ func NewAccountServer(version string, port ...string) *AccountServer { |
| 21 | 21 | return &AccountServer{IHttp: components.NewHttpServer(version, port...)} |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | -func (s *AccountServer) Init() error { | |
| 24 | +func (s *AccountServer) Init() error { | |
| 25 | 25 | //mgo init |
| 26 | 26 | err := db.ConnectMongo(common.GlobalConf.AccountConf.MongoConf) |
| 27 | 27 | |
| ... | ... | @@ -40,7 +40,7 @@ func (s *AccountServer) Init() error { |
| 40 | 40 | return nil |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | -func (s *AccountServer) Start() error { | |
| 43 | +func (s *AccountServer) Start() error { | |
| 44 | 44 | if err := s.Init(); err != nil { |
| 45 | 45 | return err |
| 46 | 46 | } |
| ... | ... | @@ -60,10 +60,10 @@ func main() { |
| 60 | 60 | }() |
| 61 | 61 | |
| 62 | 62 | select { |
| 63 | - case e := <- err: | |
| 64 | - logger.Error("game server error: %v", e) | |
| 63 | + case e := <-err: | |
| 64 | + logger.Error("http server error: %v", e) | |
| 65 | 65 | case <-stopChan: |
| 66 | - logger.Debug("game stop") | |
| 66 | + logger.Debug("http stop") | |
| 67 | 67 | web.Stop() |
| 68 | 68 | } |
| 69 | -} | |
| 70 | 69 | \ No newline at end of file |
| 70 | +} | ... | ... |
common/components/http.go
| ... | ... | @@ -10,11 +10,11 @@ import ( |
| 10 | 10 | type HttpServer struct { |
| 11 | 11 | IHttp |
| 12 | 12 | version string |
| 13 | - port []string | |
| 13 | + port []string | |
| 14 | 14 | Handler interface{} |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | -func Pong (c *gin.Context) { | |
| 17 | +func Pong(c *gin.Context) { | |
| 18 | 18 | c.JSON(200, gin.H{ |
| 19 | 19 | "message": "pong", |
| 20 | 20 | }) |
| ... | ... | @@ -24,22 +24,27 @@ func NewHttpServer(version string, port ...string) *HttpServer { |
| 24 | 24 | return &HttpServer{version: version, port: port} |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | -func GetRoutePath(objName, objFunc string) string { | |
| 27 | +func GetRoutePath(objName, objFunc string) string { | |
| 28 | 28 | return strings.ToLower(objName + "/" + objFunc) |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | -func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { | |
| 31 | +func (h *HttpServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { | |
| 32 | 32 | return func(c *gin.Context) { |
| 33 | 33 | v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) |
| 34 | 34 | if len(v) != 2 { |
| 35 | - c.JSON(http.StatusNotFound, gin.H{"code": -100, "data": ""}) | |
| 35 | + c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error", "data": ""}) | |
| 36 | 36 | return |
| 37 | 37 | } |
| 38 | - c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "data": v[1].Interface()}) | |
| 38 | + code := v[0].Int() | |
| 39 | + if code == 0 { | |
| 40 | + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": "success", "data": v[1].Interface()}) | |
| 41 | + } else { | |
| 42 | + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": v[1].Interface()}) | |
| 43 | + } | |
| 39 | 44 | } |
| 40 | 45 | } |
| 41 | 46 | |
| 42 | -func (h *HttpServer) BindHandler(handler interface{}) { | |
| 47 | +func (h *HttpServer) BindHandler(handler interface{}) { | |
| 43 | 48 | h.Handler = handler |
| 44 | 49 | } |
| 45 | 50 | |
| ... | ... | @@ -59,4 +64,4 @@ func (h *HttpServer) Start() error { |
| 59 | 64 | r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val)) |
| 60 | 65 | } |
| 61 | 66 | return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") |
| 62 | -} | |
| 63 | 67 | \ No newline at end of file |
| 68 | +} | ... | ... |