From ec2659a341a2412e0aed31fa4a0e43c076fc1bbf Mon Sep 17 00:00:00 2001 From: Jonathan KUMA Date: Mon, 29 May 2023 15:13:15 +0200 Subject: [PATCH] Update gapi exemples with version v0.2.0 and go 1.19 --- examples/1-hello-world/go.mod | 4 ++-- examples/1-hello-world/go.sum | 4 ++-- examples/1-hello-world/main.go | 8 +++++--- examples/2-encoders/config.go | 3 +-- examples/2-encoders/go.mod | 4 ++-- examples/2-encoders/go.sum | 4 ++-- examples/2-encoders/main.go | 9 ++++++--- examples/3-params/go.mod | 4 ++-- examples/3-params/go.sum | 4 ++-- examples/3-params/main.go | 9 ++++++--- examples/4-doc-openapi/go.mod | 4 ++-- examples/4-doc-openapi/go.sum | 4 ++-- examples/4-doc-openapi/main.go | 11 ++++++----- 13 files changed, 40 insertions(+), 32 deletions(-) diff --git a/examples/1-hello-world/go.mod b/examples/1-hello-world/go.mod index 92bb0d1..2fb763e 100644 --- a/examples/1-hello-world/go.mod +++ b/examples/1-hello-world/go.mod @@ -1,8 +1,8 @@ module github.com/mwm-io/gapi/examples/1-hello-world -go 1.18 +go 1.19 -require github.com/mwm-io/gapi v0.1.0 +require github.com/mwm-io/gapi v0.2.0 require ( github.com/elnormous/contenttype v1.0.3 // indirect diff --git a/examples/1-hello-world/go.sum b/examples/1-hello-world/go.sum index a99fef7..b3f536f 100644 --- a/examples/1-hello-world/go.sum +++ b/examples/1-hello-world/go.sum @@ -15,8 +15,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA= -github.com/mwm-io/gapi v0.1.0 h1:KT4J6uwNX9uSZaeHcZzM2eR3zgSqGhyUY7UY4Tf9ZRc= -github.com/mwm-io/gapi v0.1.0/go.mod h1:kGkSeloZf2LJK1tqdgbUFK10MzBQVFjolpOCMnwEXRM= +github.com/mwm-io/gapi v0.2.0 h1:jhpCAHIZkuftPk5nIOISag2nC4Eq4FwjzwSHYIlSib4= +github.com/mwm-io/gapi v0.2.0/go.mod h1:gPTxM9Fhgn7m3+5angXq8+63tTToHbYb9JDTibJJYek= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/examples/1-hello-world/main.go b/examples/1-hello-world/main.go index 5108f9c..83c92d1 100644 --- a/examples/1-hello-world/main.go +++ b/examples/1-hello-world/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "net/http" "github.com/mwm-io/gapi/handler" @@ -9,16 +10,17 @@ import ( ) func main() { + ctx := context.Background() r := server.NewMux() server.AddHandler(r, "GET", "/", handler.Func(HelloWorldHandler)) - gLog.Info("Starting http server") + gLog.Info(ctx, "Starting http server") if err := server.ServeAndHandleShutdown(r); err != nil { - gLog.Emergency(err.Error()) + gLog.Emergency(ctx, err.Error()) } - gLog.Info("Server stopped") + gLog.Info(ctx, "Server stopped") } // HelloWorldHandler is the simplest handler with core middlewares. diff --git a/examples/2-encoders/config.go b/examples/2-encoders/config.go index 6b5eb39..19ced9e 100644 --- a/examples/2-encoders/config.go +++ b/examples/2-encoders/config.go @@ -2,14 +2,13 @@ package main import ( "github.com/mwm-io/gapi/handler" - gLog "github.com/mwm-io/gapi/log" "github.com/mwm-io/gapi/middleware" ) func init() { middleware.Defaults = []handler.Middleware{ // We removed the default response writer to control response encoding for each handler - middleware.Log{Logger: gLog.Logger()}, + middleware.Log{}, middleware.Recover{}, } } diff --git a/examples/2-encoders/go.mod b/examples/2-encoders/go.mod index ba3435c..934271a 100644 --- a/examples/2-encoders/go.mod +++ b/examples/2-encoders/go.mod @@ -1,8 +1,8 @@ module github.com/mwm-io/gapi/examples/2-encoders -go 1.18 +go 1.19 -require github.com/mwm-io/gapi v0.1.0 +require github.com/mwm-io/gapi v0.2.0 require ( github.com/elnormous/contenttype v1.0.3 // indirect diff --git a/examples/2-encoders/go.sum b/examples/2-encoders/go.sum index a99fef7..b3f536f 100644 --- a/examples/2-encoders/go.sum +++ b/examples/2-encoders/go.sum @@ -15,8 +15,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA= -github.com/mwm-io/gapi v0.1.0 h1:KT4J6uwNX9uSZaeHcZzM2eR3zgSqGhyUY7UY4Tf9ZRc= -github.com/mwm-io/gapi v0.1.0/go.mod h1:kGkSeloZf2LJK1tqdgbUFK10MzBQVFjolpOCMnwEXRM= +github.com/mwm-io/gapi v0.2.0 h1:jhpCAHIZkuftPk5nIOISag2nC4Eq4FwjzwSHYIlSib4= +github.com/mwm-io/gapi v0.2.0/go.mod h1:gPTxM9Fhgn7m3+5angXq8+63tTToHbYb9JDTibJJYek= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/examples/2-encoders/main.go b/examples/2-encoders/main.go index 8d63c36..eaf887e 100644 --- a/examples/2-encoders/main.go +++ b/examples/2-encoders/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + gLog "github.com/mwm-io/gapi/log" "github.com/mwm-io/gapi/server" @@ -9,6 +11,7 @@ import ( ) func main() { + ctx := context.Background() r := server.NewMux() server.AddHandler(r, "GET", "/hello/json", hello.MakeJSONResponseHandler()) @@ -19,10 +22,10 @@ func main() { server.AddHandler(r, "GET", "/error/xml", err.MakeXMLResponseHandler()) server.AddHandler(r, "GET", "/error/auto", err.MakeAutoResponseHandler()) - gLog.Info("Starting http server") + gLog.Info(ctx, "Starting http server") if errServe := server.ServeAndHandleShutdown(r); errServe != nil { - gLog.Emergency(errServe.Error()) + gLog.Emergency(ctx, errServe.Error()) } - gLog.Info("Server stopped") + gLog.Info(ctx, "Server stopped") } diff --git a/examples/3-params/go.mod b/examples/3-params/go.mod index 3d1739a..7e99567 100644 --- a/examples/3-params/go.mod +++ b/examples/3-params/go.mod @@ -1,8 +1,8 @@ module github.com/mwm-io/gapi/examples/3-params -go 1.18 +go 1.19 -require github.com/mwm-io/gapi v0.1.0 +require github.com/mwm-io/gapi v0.2.0 require ( github.com/elnormous/contenttype v1.0.3 // indirect diff --git a/examples/3-params/go.sum b/examples/3-params/go.sum index a99fef7..b3f536f 100644 --- a/examples/3-params/go.sum +++ b/examples/3-params/go.sum @@ -15,8 +15,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA= -github.com/mwm-io/gapi v0.1.0 h1:KT4J6uwNX9uSZaeHcZzM2eR3zgSqGhyUY7UY4Tf9ZRc= -github.com/mwm-io/gapi v0.1.0/go.mod h1:kGkSeloZf2LJK1tqdgbUFK10MzBQVFjolpOCMnwEXRM= +github.com/mwm-io/gapi v0.2.0 h1:jhpCAHIZkuftPk5nIOISag2nC4Eq4FwjzwSHYIlSib4= +github.com/mwm-io/gapi v0.2.0/go.mod h1:gPTxM9Fhgn7m3+5angXq8+63tTToHbYb9JDTibJJYek= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/examples/3-params/main.go b/examples/3-params/main.go index c9bb4bd..9560101 100644 --- a/examples/3-params/main.go +++ b/examples/3-params/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + gLog "github.com/mwm-io/gapi/log" "github.com/mwm-io/gapi/server" @@ -8,6 +10,7 @@ import ( ) func main() { + ctx := context.Background() r := server.NewMux() server.AddHandlerFactory(r, "POST", "/body", internal.NewBodyHandler) @@ -16,10 +19,10 @@ func main() { server.AddHandlerFactory(r, "GET", "/path-params/{first}/{second}", internal.NewPathParamsHandler) server.AddHandlerFactory(r, "GET", "/query-params", internal.NewQueryParamsHandler) - gLog.Info("Starting http server") + gLog.Info(ctx, "Starting http server") if err := server.ServeAndHandleShutdown(r); err != nil { - gLog.Emergency(err.Error()) + gLog.Emergency(ctx, err.Error()) } - gLog.Info("Server stopped") + gLog.Info(ctx, "Server stopped") } diff --git a/examples/4-doc-openapi/go.mod b/examples/4-doc-openapi/go.mod index 3296b99..a857d87 100644 --- a/examples/4-doc-openapi/go.mod +++ b/examples/4-doc-openapi/go.mod @@ -1,8 +1,8 @@ module github.com/mwm-io/gapi/examples/4-doc-openapi -go 1.18 +go 1.19 -require github.com/mwm-io/gapi v0.1.0 +require github.com/mwm-io/gapi v0.2.0 require ( github.com/elnormous/contenttype v1.0.3 // indirect diff --git a/examples/4-doc-openapi/go.sum b/examples/4-doc-openapi/go.sum index a99fef7..b3f536f 100644 --- a/examples/4-doc-openapi/go.sum +++ b/examples/4-doc-openapi/go.sum @@ -15,8 +15,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA= -github.com/mwm-io/gapi v0.1.0 h1:KT4J6uwNX9uSZaeHcZzM2eR3zgSqGhyUY7UY4Tf9ZRc= -github.com/mwm-io/gapi v0.1.0/go.mod h1:kGkSeloZf2LJK1tqdgbUFK10MzBQVFjolpOCMnwEXRM= +github.com/mwm-io/gapi v0.2.0 h1:jhpCAHIZkuftPk5nIOISag2nC4Eq4FwjzwSHYIlSib4= +github.com/mwm-io/gapi v0.2.0/go.mod h1:gPTxM9Fhgn7m3+5angXq8+63tTToHbYb9JDTibJJYek= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/examples/4-doc-openapi/main.go b/examples/4-doc-openapi/main.go index c6a4982..e9dc14f 100644 --- a/examples/4-doc-openapi/main.go +++ b/examples/4-doc-openapi/main.go @@ -1,7 +1,7 @@ package main import ( - "log" + "context" gLog "github.com/mwm-io/gapi/log" "github.com/mwm-io/gapi/server" @@ -12,6 +12,7 @@ import ( // This simple example show you how to make a CRUD for the users model. // Each handler has a type. func main() { + ctx := context.Background() r := server.NewMux() // All following handler are complete example to know how to make a handler with request-scope data @@ -29,14 +30,14 @@ func main() { // server.AddDocHandlers add handler to expose API documentation. // Go to http://localhost:8080 to see the result if err := server.AddDocHandlers(r); err != nil { - log.Printf("error while adding rapidoc %+v\n", err) + gLog.Error(ctx, err.Error()) } - gLog.Info("Starting http server") + gLog.Info(ctx, "Starting http server") if err := server.ServeAndHandleShutdown(r); err != nil { - gLog.Emergency(err.Error()) + gLog.Emergency(ctx, err.Error()) } - gLog.Info("Server stopped") + gLog.Info(ctx, "Server stopped") }