limit upload size, update build info

This commit is contained in:
AJ ONeal 2020-09-29 22:34:20 -06:00
parent ec9b29f680
commit 1b84a99001
2 changed files with 16 additions and 0 deletions

View File

@ -2,7 +2,10 @@
> Boilerplate for how I like to write a backend web service > Boilerplate for how I like to write a backend web service
## Build
```bash ```bash
export GOFLAGS="-mod=vendor"
go mod tidy go mod tidy
go mod vendor go mod vendor
go generate -mod=vendor ./... go generate -mod=vendor ./...

13
main.go
View File

@ -92,6 +92,8 @@ func main() {
} }
} }
var defaultMaxBytes int64 = 1 << 20
func serve() { func serve() {
r := chi.NewRouter() r := chi.NewRouter()
@ -105,6 +107,10 @@ func serve() {
r.Use(middleware.Logger) r.Use(middleware.Logger)
r.Use(middleware.Recoverer) r.Use(middleware.Recoverer)
r.Route("/api", func(r chi.Router) {
r.Use(limitResponseSize)
})
var staticHandler http.HandlerFunc var staticHandler http.HandlerFunc
pub := http.FileServer(assets.Assets) pub := http.FileServer(assets.Assets)
@ -142,3 +148,10 @@ func serve() {
return return
} }
} }
func limitResponseSize(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, defaultMaxBytes)
next.ServeHTTP(w, r)
})
}