From aa65ebfe1f1c89a61d3dff5627f61d9e6395e563 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 12 Feb 2020 19:27:11 +0200 Subject: [PATCH] example: gRPC-compatible controller as requested at: https://github.com/kataras/iris/issues/1449 --- _examples/README.md | 1 + _examples/mvc/grpc-compatible/main.go | 59 ++++++++++++++++++++++ _examples/mvc/grpc-compatible/main_test.go | 16 ++++++ go.mod | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 _examples/mvc/grpc-compatible/main.go create mode 100644 _examples/mvc/grpc-compatible/main_test.go diff --git a/_examples/README.md b/_examples/README.md index aa9380c83..4fe65a3aa 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -168,6 +168,7 @@ Navigate through examples for a better understanding. - [Websocket Controller](mvc/websocket) - [Register Middleware](mvc/middleware) - [Vue.js Todo MVC](tutorial/vuejs-todo-mvc) +- [gRPC-compatible controller](mvc/grpc-compatible/main.go) **NEW** ### Subdomains diff --git a/_examples/mvc/grpc-compatible/main.go b/_examples/mvc/grpc-compatible/main.go new file mode 100644 index 000000000..8d775b814 --- /dev/null +++ b/_examples/mvc/grpc-compatible/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/mvc" +) + +// See https://github.com/kataras/iris/issues/1449 +// for more details but in-short you can convert Iris MVC to gRPC methods by +// binding the `context.Context` from `iris.Context.Request().Context()` and gRPC input and output data. + +func main() { + app := newApp() + app.Logger().SetLevel("debug") + + // POST: http://localhost:8080/login + // with request data: {"username": "makis"} + // and expected output: {"message": "makis logged"} + app.Listen(":8080") +} + +func newApp() *iris.Application { + app := iris.New() + + mvc.New(app). + // Request-scope binding for context.Context-type controller's method or field. + // (or import github.com/kataras/iris/v12/hero and hero.Register(...)) + Register(func(ctx iris.Context) context.Context { + return ctx.Request().Context() + }). + // Bind loginRequest. + Register(func(ctx iris.Context) loginRequest { + var req loginRequest + ctx.ReadJSON(&req) + return req + }). + Handle(&myController{}) + + return app +} + +type myController struct{} + +type loginRequest struct { + Username string `json:"username"` +} + +type loginResponse struct { + Message string `json:"message"` +} + +func (c *myController) PostLogin(ctx context.Context, input loginRequest) (loginResponse, error) { + // [use of ctx to call a gRpc method or a database call...] + return loginResponse{ + Message: input.Username + " logged", + }, nil +} diff --git a/_examples/mvc/grpc-compatible/main_test.go b/_examples/mvc/grpc-compatible/main_test.go new file mode 100644 index 000000000..cfe369820 --- /dev/null +++ b/_examples/mvc/grpc-compatible/main_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "testing" + + "github.com/kataras/iris/v12/httptest" +) + +func TestBindContextContext(t *testing.T) { + app := newApp() + + e := httptest.New(t, app) + e.POST("/login").WithJSON(map[string]string{"username": "makis"}).Expect(). + Status(httptest.StatusOK). + JSON().Equal(map[string]string{"message": "makis logged"}) +} diff --git a/go.mod b/go.mod index bced6f933..8ff2797a1 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/iris-contrib/schema v0.0.1 github.com/json-iterator/go v1.1.9 github.com/kataras/golog v0.0.10 - github.com/kataras/neffos v0.0.12 + github.com/kataras/neffos v0.0.14 github.com/kataras/sitemap v0.0.5 github.com/klauspost/compress v1.9.7 github.com/mediocregopher/radix/v3 v3.4.2