middlewares all now have the same signature
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jeffrey Paul 2020-09-30 01:12:59 -07:00
parent 7bf1aee60f
commit 5526397247
5 changed files with 21 additions and 15 deletions

View File

@ -26,7 +26,7 @@ RUN go mod download
COPY ./ ./ COPY ./ ./
#RUN make lint #RUN make lint
RUN make build RUN make httpd
RUN go mod vendor RUN go mod vendor
RUN tar -c . | bzip2 > /src.tbz2 RUN tar -c . | bzip2 > /src.tbz2

View File

@ -35,27 +35,23 @@ lint:
golangci-lint run golangci-lint run
sh -c 'test -z "$$(gofmt -l .)"' sh -c 'test -z "$$(gofmt -l .)"'
debug: build debug: ./$(FN)d
DEBUG=1 GOTRACEBACK=all ./$(FN)d DEBUG=1 GOTRACEBACK=all ./$(FN)d
debugger: debugger:
cd cmd/$(FN)d && dlv debug main.go cd cmd/$(FN)d && dlv debug main.go
run: build run: ./$(FN)d
./$(FN)d ./$(FN)d
clean: clean:
-rm -f ./$(FN)d debug.log -rm -f ./$(FN)d debug.log
build: ./$(FN)d
docker: docker:
docker build . docker build .
get: get:
cd cmd/$(FN)d && go get -v 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) . 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

View File

@ -1,5 +1,7 @@
# gohttpserver # 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. 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 Many ideas are taken from Mat Ryer's talk How I Write HTTP Web Services

View File

@ -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 func (s *server) AuthMiddleware() func(http.Handler) http.Handler {
// on the mux. return func(next http.Handler) http.Handler {
func (s *server) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { // FIXME you'll want to change this to do stuff.
s.log.Info().Msg("AUTH: before request") s.log.Info().Msg("AUTH: before request")
next(w, r) next.ServeHTTP(w, r)
})
} }
} }

View File

@ -7,8 +7,15 @@ import (
func (s *server) routes() { func (s *server) routes() {
s.router = mux.NewRouter() s.router = mux.NewRouter()
authMiddleware := s.AuthMiddleware()
s.router.HandleFunc("/", s.handleIndex()).Methods("GET") 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.HandleFunc("/.well-known/healthcheck.json", s.handleHealthCheck()).Methods("GET")
s.router.Use(s.LoggingMiddleware()) s.router.Use(s.LoggingMiddleware())