Skip to content

Commit

Permalink
fix: use chi router with request logs (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Sep 18, 2020
1 parent e870642 commit 825acb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.13

require (
github.com/caarlos0/httperr v1.0.0
github.com/go-chi/chi v4.1.2+incompatible
gocloud.dev v0.19.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHqu
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
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-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
Expand Down
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"time"

"github.com/caarlos0/httperr"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"gocloud.dev/blob"

_ "gocloud.dev/blob/azureblob"
Expand All @@ -40,11 +42,13 @@ func main() {
}
defer bucket.Close()

var handler = http.NewServeMux()

handler.Handle("/", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
var path = strings.Replace(r.URL.EscapedPath(), "/", "", 1)
log.Println(path)
var r = chi.NewRouter()
r.Use(middleware.Logger, middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
})
r.Get("/{asset}", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
var path = chi.URLParam(r, "asset")

user, pwd, ok := r.BasicAuth()
if !(ok && isAuthorized(user+":"+pwd)) {
Expand All @@ -54,21 +58,21 @@ func main() {

reader, err := bucket.NewReader(ctx, path, nil)
if err != nil {
return httperr.Wrap(err, http.StatusBadRequest)
return httperr.Wrap(err, http.StatusNotFound)
}
defer reader.Close()
if _, err := io.Copy(w, reader); err != nil {
return httperr.Wrap(err, http.StatusInternalServerError)
}
return nil
}))
}).ServeHTTP)

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

srv := &http.Server{
Addr: listen,
Handler: handler,
Handler: r,
}

go func() {
Expand Down

0 comments on commit 825acb4

Please sign in to comment.