-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from hearchco/go-chi-image-proxy
feat(router)!: switch to go-chi and add image proxy
- Loading branch information
Showing
43 changed files
with
575 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package router | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/andybalholm/brotli" | ||
"github.com/go-chi/chi/v5/middleware" | ||
) | ||
|
||
func compress(level int, types ...string) [](func(http.Handler) http.Handler) { | ||
// deflate & gzip | ||
dig := middleware.Compress(level, types...) | ||
|
||
// brotli | ||
br := middleware.NewCompressor(level, types...) | ||
br.SetEncoder("br", func(w io.Writer, level int) io.Writer { | ||
return brotli.NewWriterOptions(w, brotli.WriterOptions{ | ||
Quality: level, | ||
}) | ||
}) | ||
|
||
return [](func(http.Handler) http.Handler){dig, br.Handler} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package router | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/hlog" | ||
) | ||
|
||
func ignoredPath(path string, skipPaths []string) bool { | ||
for _, p := range skipPaths { | ||
if p == path { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func zerologMiddleware(lgr zerolog.Logger, skipPaths []string) [](func(http.Handler) http.Handler) { | ||
newHandler := hlog.NewHandler(lgr) | ||
fieldsHandler := hlog.AccessHandler(func(r *http.Request, status int, size int, duration time.Duration) { | ||
// skip logging for ignored paths | ||
if ignoredPath(r.URL.Path, skipPaths) { | ||
return | ||
} | ||
|
||
// get logger from context | ||
lgr := hlog.FromRequest(r) | ||
|
||
// decide on log level | ||
event := lgr.Info() | ||
if status >= 500 { | ||
event = lgr.Error() | ||
} else if status >= 400 { | ||
event = lgr.Warn() | ||
} | ||
|
||
// log | ||
event. | ||
Str("method", r.Method). | ||
Str("path", r.URL.Path). | ||
Int("status", status). | ||
Dur("duration", duration). | ||
Str("ip", r.RemoteAddr). | ||
Msg("Request") | ||
}) | ||
|
||
return [](func(http.Handler) http.Handler){ | ||
newHandler, | ||
fieldsHandler, | ||
} | ||
} |
Oops, something went wrong.