mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 01:02:29 +00:00
add http middleware, add option to use custom mux, improved logger kvs
This commit is contained in:
@ -9,10 +9,12 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"go.linka.cloud/grpc/client"
|
||||
"go.linka.cloud/grpc/logger"
|
||||
"go.linka.cloud/grpc/registry/mdns"
|
||||
"go.linka.cloud/grpc/service"
|
||||
)
|
||||
@ -39,6 +41,20 @@ func (g *GreeterHandler) SayHelloStream(req *HelloStreamRequest, s Greeter_SayHe
|
||||
return nil
|
||||
}
|
||||
|
||||
func httpLogger(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
start := time.Now()
|
||||
log := logger.From(request.Context()).WithFields(
|
||||
"method", request.Method,
|
||||
"host", request.Host,
|
||||
"path", request.URL.Path,
|
||||
"remoteAddress", request.RemoteAddr,
|
||||
)
|
||||
next.ServeHTTP(writer, request)
|
||||
log.WithField("duration", time.Since(start)).Info()
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
name := "greeter"
|
||||
secure := true
|
||||
@ -71,6 +87,7 @@ func main() {
|
||||
service.WithGatewayPrefix("/rest"),
|
||||
service.WithGRPCWeb(true),
|
||||
service.WithGRPCWebPrefix("/grpc"),
|
||||
service.WithMiddlewares(httpLogger),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -137,9 +154,8 @@ func main() {
|
||||
}
|
||||
logrus.Info(string(b))
|
||||
}
|
||||
do(scheme+address+"/rest/api/v1/greeter/hello", "application/json")
|
||||
do(scheme+address+"/rest/api/v1/greeter/hello", "application/json")
|
||||
do(scheme+address+"/grpc/helloworld.Greeter/SayHello", "application/grpc-web+json")
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user