diff --git a/cmd/httpserver/http.go b/cmd/httpserver/http.go index 3400cbb..8f6de30 100644 --- a/cmd/httpserver/http.go +++ b/cmd/httpserver/http.go @@ -21,7 +21,7 @@ func NewAccountServer(version string, port ...string) *AccountServer { return &AccountServer{IHttp: components.NewHttpServer(version, port...)} } -func (s *AccountServer) Init() error { +func (s *AccountServer) Init() error { //mgo init err := db.ConnectMongo(common.GlobalConf.AccountConf.MongoConf) @@ -40,7 +40,7 @@ func (s *AccountServer) Init() error { return nil } -func (s *AccountServer) Start() error { +func (s *AccountServer) Start() error { if err := s.Init(); err != nil { return err } @@ -60,10 +60,10 @@ func main() { }() select { - case e := <- err: - logger.Error("game server error: %v", e) + case e := <-err: + logger.Error("http server error: %v", e) case <-stopChan: - logger.Debug("game stop") + logger.Debug("http stop") web.Stop() } -} \ No newline at end of file +} diff --git a/common/components/http.go b/common/components/http.go index 9fe119a..dbc9202 100644 --- a/common/components/http.go +++ b/common/components/http.go @@ -10,11 +10,11 @@ import ( type HttpServer struct { IHttp version string - port []string + port []string Handler interface{} } -func Pong (c *gin.Context) { +func Pong(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) @@ -24,22 +24,27 @@ func NewHttpServer(version string, port ...string) *HttpServer { return &HttpServer{version: version, port: port} } -func GetRoutePath(objName, objFunc string) string { +func GetRoutePath(objName, objFunc string) string { return strings.ToLower(objName + "/" + objFunc) } -func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { +func (h *HttpServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { return func(c *gin.Context) { v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) if len(v) != 2 { - c.JSON(http.StatusNotFound, gin.H{"code": -100, "data": ""}) + c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error", "data": ""}) return } - c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "data": v[1].Interface()}) + code := v[0].Int() + if code == 0 { + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": "success", "data": v[1].Interface()}) + } else { + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "message": v[1].Interface()}) + } } } -func (h *HttpServer) BindHandler(handler interface{}) { +func (h *HttpServer) BindHandler(handler interface{}) { h.Handler = handler } @@ -59,4 +64,4 @@ func (h *HttpServer) Start() error { r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val)) } return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") -} \ No newline at end of file +} -- libgit2 0.21.2