lxg 4 rokov pred
rodič
commit
a70f0f4821
5 zmenil súbory, kde vykonal 54 pridanie a 8 odobranie
  1. 5 1
      .idea/workspace.xml
  2. 18 4
      cmd/main.go
  3. 12 0
      gateway/rpc/context.go
  4. 12 0
      micro.go
  5. 7 3
      service.go

+ 5 - 1
.idea/workspace.xml

@@ -3,7 +3,10 @@
   <component name="ChangeListManager">
     <list default="true" id="cd58867b-089e-4508-9033-393b8939261c" name="Default Changelist" comment="">
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/cmd/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/cmd/main.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/gateway/rpc/context.go" beforeDir="false" afterPath="$PROJECT_DIR$/gateway/rpc/context.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/micro.go" beforeDir="false" afterPath="$PROJECT_DIR$/micro.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/service.go" beforeDir="false" afterPath="$PROJECT_DIR$/service.go" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -132,6 +135,7 @@
         </entry>
       </map>
     </option>
+    <option name="oldMeFiltersMigrated" value="true" />
   </component>
   <component name="VgoProject">
     <integration-enabled>true</integration-enabled>

+ 18 - 4
cmd/main.go

@@ -6,12 +6,26 @@ import (
 	"git.nspix.com/golang/micro/log"
 )
 
+type (
+	mathRequest struct {
+		NumA int `json:"num_a"`
+		NumB int `json:"num_b"`
+	}
+
+	mathResponse struct {
+		Value int `json:"value"`
+	}
+)
+
 func main() {
 	svr := micro.New(micro.WithName("test", "0.1.01"))
-
 	svr.Handle("math.add", func(ctx gateway.Context) (err error) {
-		return ctx.Success(100)
-	})
-
+		var req mathRequest
+		if err = ctx.Bind(&req); err == nil {
+			return ctx.Success(mathResponse{Value: req.NumA + req.NumB})
+		} else {
+			return ctx.Error(100, err.Error())
+		}
+	},micro.WithHttpMethod("GET"))
 	log.Debug(svr.Run())
 }

+ 12 - 0
gateway/rpc/context.go

@@ -9,3 +9,15 @@ func (c *Context) Reset(req *Request, res *Response) {
 	c.req = req
 	c.res = res
 }
+
+func (c *Context) Bind(i interface{}) (err error) {
+	return
+}
+
+func (c *Context) Success(i interface{}) (err error) {
+	return
+}
+
+func (c *Context) Error(code int, message string) (err error) {
+	return
+}

+ 12 - 0
micro.go

@@ -7,6 +7,12 @@ import (
 )
 
 type (
+	HandleOptions struct {
+		HttpMethod string
+	}
+
+	HandleOption func(o *HandleOptions)
+
 	HandleFunc func(ctx gateway.Context) (err error)
 
 	Application interface {
@@ -20,3 +26,9 @@ type (
 		Stop() (err error)
 	}
 )
+
+func WithHttpMethod(method string) HandleOption {
+	return func(o *HandleOptions) {
+		o.HttpMethod = method
+	}
+}

+ 7 - 3
service.go

@@ -60,17 +60,21 @@ func (svr *Service) eventLoop() {
 	}
 }
 
-func (svr *Service) Handle(method string, cb HandleFunc) {
+func (svr *Service) Handle(method string, cb HandleFunc, opts ...HandleOption) {
+	opt := &HandleOptions{HttpMethod: "POST"}
+	for _, f := range opts {
+		f(opt)
+	}
 	if svr.opts.EnableHttp {
-		//convert url
 		path := strings.ReplaceAll(method, ".", "/")
 		if path[0] != '/' {
 			path = "/" + path
 		}
-		svr.httpSvr.Handle("POST", path, func(ctx *http.Context) (err error) {
+		svr.httpSvr.Handle(opt.HttpMethod, path, func(ctx *http.Context) (err error) {
 			return cb(ctx)
 		})
 	}
+
 	//启动RPC功能
 	if svr.opts.EnableRPC {