diff --git a/Dockerfile b/Dockerfile index 59c6bf4..5317dac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,7 @@ RUN go mod download COPY ./ ./ #RUN make lint -RUN make build +RUN make httpd RUN go mod vendor RUN tar -c . | bzip2 > /src.tbz2 diff --git a/Makefile b/Makefile index 2a0edf7..4702d27 100644 --- a/Makefile +++ b/Makefile @@ -35,27 +35,23 @@ lint: golangci-lint run sh -c 'test -z "$$(gofmt -l .)"' -debug: build +debug: ./$(FN)d DEBUG=1 GOTRACEBACK=all ./$(FN)d debugger: cd cmd/$(FN)d && dlv debug main.go -run: build +run: ./$(FN)d ./$(FN)d clean: -rm -f ./$(FN)d debug.log -build: ./$(FN)d - docker: docker build . get: cd cmd/$(FN)d && go get -v -./$(FN)d: */*.go cmd/*/*.go get +./$(FN)d: cmd/$(FN)d/main.go */*.go get cd cmd/$(FN)d && go build -o ../../$(FN)d $(GOFLAGS) . - -.PHONY: build fmt test is_uncommitted docker dist hub upload-docker-image clean run rundebug default build-docker-image-dist diff --git a/README.md b/README.md index 29a837c..059d580 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # gohttpserver +[![Build Status](https://drone.datavi.be/api/badges/sneak/gohttpserver/status.svg)](https://drone.datavi.be/sneak/gohttpserver) + This is my boilerplate for starting a new go HTTP server repository. Many ideas are taken from Mat Ryer's talk How I Write HTTP Web Services diff --git a/httpserver/middlewares.go b/httpserver/middlewares.go index 7ea14f3..7757740 100644 --- a/httpserver/middlewares.go +++ b/httpserver/middlewares.go @@ -65,11 +65,12 @@ func (s *server) LoggingMiddleware() func(http.Handler) http.Handler { } } -// this is the HandleFunc wrapper type for a *specific route*, it doesn't go -// on the mux. -func (s *server) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - s.log.Info().Msg("AUTH: before request") - next(w, r) +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. + s.log.Info().Msg("AUTH: before request") + next.ServeHTTP(w, r) + }) } } diff --git a/httpserver/routes.go b/httpserver/routes.go index e1eb736..53d0c8a 100644 --- a/httpserver/routes.go +++ b/httpserver/routes.go @@ -7,8 +7,15 @@ import ( func (s *server) routes() { s.router = mux.NewRouter() + authMiddleware := s.AuthMiddleware() + s.router.HandleFunc("/", s.handleIndex()).Methods("GET") - s.router.HandleFunc("/login", s.AuthMiddleware(s.handleLogin())).Methods("GET") + + // 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 a HandleFunc, viz: + s.router.HandleFunc("/login", authMiddleware(s.handleLogin()).ServeHTTP).Methods("GET") + s.router.HandleFunc("/.well-known/healthcheck.json", s.handleHealthCheck()).Methods("GET") s.router.Use(s.LoggingMiddleware())