middlewares all now have the same signature
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
7bf1aee60f
commit
5526397247
@ -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
|
||||
|
||||
|
10
Makefile
10
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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user