1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
Tony Chen 00088f644a
examples: clean examples (#1036)
* clean examples

* update yaml to v3
2021-06-12 20:20:18 +08:00

43 lines
994 B
Go

package main
import (
"log"
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http"
)
// this example shows how to add middlewares,
// execution order is globalFilter(http) --> routeFilter(http) --> pathFilter(http) --> serviceFilter(service)
func main() {
s := &server{}
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
// add service filter
serviceMiddleware,
serviceMiddleware2,
),
// add global filter
http.Filter(globalFilter, globalFilter2),
)
// register http hanlder to http server
helloworld.RegisterGreeterHTTPServer(httpSrv, s)
// add route filter
r := httpSrv.Route("/", routeFilter, routeFilter2)
// add path filter to custom route
r.GET("/hello/{name}", sayHelloHandler, pathFilter, pathFilter2)
app := kratos.New(
kratos.Name("helloworld"),
kratos.Server(
httpSrv,
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}