Skip to content

Commit 09a410c

Browse files
committed
example: gRPC-compatible controller
as requested at: #1449 Former-commit-id: a0af1a78bcfef85f297c5087c8cbb00124226036
1 parent 29b360d commit 09a410c

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

_examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Navigate through examples for a better understanding.
168168
- [Websocket Controller](mvc/websocket)
169169
- [Register Middleware](mvc/middleware)
170170
- [Vue.js Todo MVC](tutorial/vuejs-todo-mvc)
171+
- [gRPC-compatible controller](mvc/grpc-compatible/main.go) **NEW**
171172

172173
### Subdomains
173174

_examples/mvc/grpc-compatible/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/kataras/iris/v12"
7+
"github.com/kataras/iris/v12/mvc"
8+
)
9+
10+
// See https://github.com/kataras/iris/issues/1449
11+
// for more details but in-short you can convert Iris MVC to gRPC methods by
12+
// binding the `context.Context` from `iris.Context.Request().Context()` and gRPC input and output data.
13+
14+
func main() {
15+
app := newApp()
16+
app.Logger().SetLevel("debug")
17+
18+
// POST: http://localhost:8080/login
19+
// with request data: {"username": "makis"}
20+
// and expected output: {"message": "makis logged"}
21+
app.Listen(":8080")
22+
}
23+
24+
func newApp() *iris.Application {
25+
app := iris.New()
26+
27+
mvc.New(app).
28+
// Request-scope binding for context.Context-type controller's method or field.
29+
// (or import github.com/kataras/iris/v12/hero and hero.Register(...))
30+
Register(func(ctx iris.Context) context.Context {
31+
return ctx.Request().Context()
32+
}).
33+
// Bind loginRequest.
34+
Register(func(ctx iris.Context) loginRequest {
35+
var req loginRequest
36+
ctx.ReadJSON(&req)
37+
return req
38+
}).
39+
Handle(&myController{})
40+
41+
return app
42+
}
43+
44+
type myController struct{}
45+
46+
type loginRequest struct {
47+
Username string `json:"username"`
48+
}
49+
50+
type loginResponse struct {
51+
Message string `json:"message"`
52+
}
53+
54+
func (c *myController) PostLogin(ctx context.Context, input loginRequest) (loginResponse, error) {
55+
// [use of ctx to call a gRpc method or a database call...]
56+
return loginResponse{
57+
Message: input.Username + " logged",
58+
}, nil
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/kataras/iris/v12/httptest"
7+
)
8+
9+
func TestBindContextContext(t *testing.T) {
10+
app := newApp()
11+
12+
e := httptest.New(t, app)
13+
e.POST("/login").WithJSON(map[string]string{"username": "makis"}).Expect().
14+
Status(httptest.StatusOK).
15+
JSON().Equal(map[string]string{"message": "makis logged"})
16+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/iris-contrib/schema v0.0.1
2222
github.com/json-iterator/go v1.1.9
2323
github.com/kataras/golog v0.0.10
24-
github.com/kataras/neffos v0.0.12
24+
github.com/kataras/neffos v0.0.14
2525
github.com/kataras/sitemap v0.0.5
2626
github.com/klauspost/compress v1.9.7
2727
github.com/mediocregopher/radix/v3 v3.4.2

0 commit comments

Comments
 (0)