From a9887634ab0b8586b26d0618fa23721f40e62f38 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 5 Oct 2020 06:15:32 -0700 Subject: [PATCH 1/2] add simple json endpoint example --- go.mod | 2 ++ go.sum | 5 +++++ httpserver/handlers.go | 15 +++++++++++++++ httpserver/middlewares.go | 19 +++++++++++++++++-- httpserver/routes.go | 10 ++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 httpserver/handlers.go diff --git a/go.mod b/go.mod index fa3ac2f..776988f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require ( github.com/99designs/basicauth-go v0.0.0-20160802081356-2a93ba0f464d github.com/getsentry/sentry-go v0.7.0 github.com/go-chi/chi v4.1.2+incompatible + github.com/go-chi/cors v1.1.1 + github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5 // indirect github.com/joho/godotenv v1.3.0 github.com/prometheus/client_golang v1.6.0 github.com/rs/zerolog v1.20.0 diff --git a/go.sum b/go.sum index 7dcc68b..fcae8ea 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwv github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/go-chi/cors v1.1.1 h1:eHuqxsIw89iXcWnWUN8R72JMibABJTN/4IOYI5WERvw= +github.com/go-chi/cors v1.1.1/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I= github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -171,6 +173,8 @@ github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbk github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= +github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5 h1:LlI/THEqi2n5RNtIIplDTGNCQ6m81r2tem36s7XSxtU= +github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -460,6 +464,7 @@ golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc h1:NCy3Ohtk6Iny5V/reW2Ktypo4zIpWBdRJ1uFMjBxdg8= golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= diff --git a/httpserver/handlers.go b/httpserver/handlers.go new file mode 100644 index 0000000..0814135 --- /dev/null +++ b/httpserver/handlers.go @@ -0,0 +1,15 @@ +package httpserver + +import ( + "net/http" + "time" +) + +func (s *server) handleNow() http.HandlerFunc { + type response struct { + Now time.Time `json:"now"` + } + return func(w http.ResponseWriter, r *http.Request) { + s.respondJSON(w, r, &response{Now: time.Now()}, 200) + } +} diff --git a/httpserver/middlewares.go b/httpserver/middlewares.go index 70267a6..cf6024a 100644 --- a/httpserver/middlewares.go +++ b/httpserver/middlewares.go @@ -7,9 +7,9 @@ import ( basicauth "github.com/99designs/basicauth-go" "github.com/go-chi/chi/middleware" + "github.com/go-chi/cors" metrics "github.com/slok/go-http-metrics/metrics/prometheus" ghmm "github.com/slok/go-http-metrics/middleware" - "github.com/slok/go-http-metrics/middleware/std" "github.com/spf13/viper" ) @@ -75,10 +75,25 @@ func (s *server) LoggingMiddleware() func(http.Handler) http.Handler { } } +func (s *server) CORSMiddleware() func(http.Handler) http.Handler { + return cors.Handler(cors.Options{ + // CHANGEME! these are defaults, change them to suit your needs or + // read from environment/viper. + // AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts + AllowedOrigins: []string{"*"}, + // AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: false, + MaxAge: 300, // Maximum value not ignored by any of major browsers + }) +} + func (s *server) AuthMiddleware() func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // FIXME you'll want to change this to do stuff. + // CHANGEME you'll want to change this to do stuff. s.log.Info().Msg("AUTH: before request") next.ServeHTTP(w, r) }) diff --git a/httpserver/routes.go b/httpserver/routes.go index 7ab9ba6..19f2682 100644 --- a/httpserver/routes.go +++ b/httpserver/routes.go @@ -30,6 +30,10 @@ func (s *server) routes() { s.router.Use(s.MetricsMiddleware()) } + // set up CORS headers. you'll probably want to configure that + // in middlewares.go. + s.router.Use(s.CORSMiddleware()) + // CHANGEME to suit your needs, or pull from config. // timeout for request context; your handlers must finish within // this window: @@ -55,6 +59,12 @@ func (s *server) routes() { s.router.Get("/", s.handleIndex()) + s.router.Get("/", s.handleIndex()) + + s.router.Route("/api/v1", func(r chi.Router) { + r.Get("/now", s.handleNow()) + }) + // if you want to use a general purpose middleware (http.Handler // wrapper) on a specific HandleFunc route, you need to take the // .ServeHTTP of the http.Handler to get its HandleFunc, viz: From db919c10258a8a207ff46dd611c585c66d941d64 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 5 Oct 2020 06:46:13 -0700 Subject: [PATCH 2/2] go mod tidy --- go.mod | 1 - go.sum | 2 -- httpserver/main.go | 1 - 3 files changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 776988f..6ba8ba8 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/getsentry/sentry-go v0.7.0 github.com/go-chi/chi v4.1.2+incompatible github.com/go-chi/cors v1.1.1 - github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5 // indirect github.com/joho/godotenv v1.3.0 github.com/prometheus/client_golang v1.6.0 github.com/rs/zerolog v1.20.0 diff --git a/go.sum b/go.sum index fcae8ea..230b26c 100644 --- a/go.sum +++ b/go.sum @@ -173,8 +173,6 @@ github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbk github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= -github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5 h1:LlI/THEqi2n5RNtIIplDTGNCQ6m81r2tem36s7XSxtU= -github.com/jgautheron/goconst v0.0.0-20200920201509-8f5268ce89d5/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= diff --git a/httpserver/main.go b/httpserver/main.go index a3df137..b26de23 100644 --- a/httpserver/main.go +++ b/httpserver/main.go @@ -50,7 +50,6 @@ func NewServer(options ...func(s *server)) *server { for _, opt := range options { opt(n) } - return n }