-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"fmt"
"log"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http"
kiris "github.com/iris-contrib/kratos"
"github.com/kataras/iris/v12"
)
func customMiddleware(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if tr, ok := transport.FromServerContext(ctx); ok {
fmt.Println("operation:", tr.Operation())
}
reply, err = handler(ctx, req)
return
}
}
func main() {
router := iris.New()
router.Use(kiris.Middlewares(recovery.Recovery(), customMiddleware))
router.Get("/helloworld/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
if name == "error" {
kiris.Error(ctx, errors.Unauthorized("auth_error", "no authentication"))
} else {
ctx.JSON(iris.Map{"welcome": name})
}
})
if err := router.Build(); err != nil {
log.Fatal(err)
}
httpSrv := http.NewServer(http.Address(":8000"))
httpSrv.HandlePrefix("/", router)
kratosApp := kratos.New(
kratos.Name("iris"),
kratos.Server(
httpSrv,
),
)
// http://localhost:8000/helloworld/kataras.
if err := kratosApp.Run(); err != nil {
log.Fatal(err)
}
}