Linting fixes and fixed HTTP requests logger.

This commit is contained in:
Stanislav Nikitin 2020-04-25 13:47:19 +05:00
parent bbb68c48a1
commit 2e07cb9726
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
3 changed files with 15 additions and 2 deletions

View File

@ -10,6 +10,8 @@ linters:
- gocritic
# Complains about main() lengths, which isn't an issue.
- funlen
# Magic numbers might be everywhere. Disabled for now.
- gomnd
linters-settings:
lll:
line-length: 420

View File

@ -27,7 +27,9 @@ func checkAllowedIPs() echo.MiddlewareFunc {
// into comparable things.
// If IP address was specified without network mask - assume /32.
var subnets []*net.IPNet
allowedIPs := configuration.Cfg.GetAllowedIPs()
for _, ip := range allowedIPs {
ipToParse := ip
if !strings.Contains(ip, "/") {
@ -46,7 +48,9 @@ func checkAllowedIPs() echo.MiddlewareFunc {
// Check if requester's IP address are within allowed IP
// subnets.
ipToCheck := net.ParseIP(ec.RealIP())
var allowed bool
for _, subnet := range subnets {
if subnet.Contains(ipToCheck) {
allowed = true

View File

@ -2,12 +2,19 @@ package httpserver
import (
// other
"time"
"github.com/labstack/echo"
)
func requestLogger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error {
startTime := time.Now()
err := next(ec)
log.Info().
Str("From", ec.RealIP()).
Str("To", ec.Request().Host).
@ -15,10 +22,10 @@ func requestLogger() echo.MiddlewareFunc {
Str("Path", ec.Request().URL.Path).
Int64("Length", ec.Request().ContentLength).
Str("UA", ec.Request().UserAgent()).
TimeDiff("TimeMS", time.Now(), startTime).
Msg("HTTP request")
_ = next(ec)
return nil
return err
}
}
}