example: gRPC-compatible controller

as requested at: https://github.com/kataras/iris/issues/1449


Former-commit-id: a0af1a78bcfef85f297c5087c8cbb00124226036
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-02-12 19:27:11 +02:00
parent 29b360dd26
commit 09a410c6cb
4 changed files with 77 additions and 1 deletions

View File

@ -168,6 +168,7 @@ Navigate through examples for a better understanding.
- [Websocket Controller](mvc/websocket) - [Websocket Controller](mvc/websocket)
- [Register Middleware](mvc/middleware) - [Register Middleware](mvc/middleware)
- [Vue.js Todo MVC](tutorial/vuejs-todo-mvc) - [Vue.js Todo MVC](tutorial/vuejs-todo-mvc)
- [gRPC-compatible controller](mvc/grpc-compatible/main.go) **NEW**
### Subdomains ### Subdomains

View File

@ -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
}

View File

@ -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"})
}

2
go.mod
View File

@ -21,7 +21,7 @@ require (
github.com/iris-contrib/schema v0.0.1 github.com/iris-contrib/schema v0.0.1
github.com/json-iterator/go v1.1.9 github.com/json-iterator/go v1.1.9
github.com/kataras/golog v0.0.10 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/kataras/sitemap v0.0.5
github.com/klauspost/compress v1.9.7 github.com/klauspost/compress v1.9.7
github.com/mediocregopher/radix/v3 v3.4.2 github.com/mediocregopher/radix/v3 v3.4.2