2020-02-12 18:27:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// See https://github.com/kataras/iris/issues/1449
|
2020-02-29 13:18:15 +01:00
|
|
|
// Iris automatically binds the standard "context" context.Context to `iris.Context.Request().Context()`
|
|
|
|
// and any other structure that is not mapping to a registered dependency
|
|
|
|
// as a payload depends on the request, e.g XML, YAML, Query, Form, JSON.
|
|
|
|
//
|
|
|
|
// Useful to use gRPC services as Iris controllers fast and without wrappers.
|
2020-02-12 18:27:11 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-02-29 13:18:15 +01:00
|
|
|
mvc.New(app).Handle(&myController{})
|
2020-02-12 18:27:11 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|