diff --git a/middleware/IPToHost.go b/middleware/IPToHost.go index f702bf3b..09eab148 100644 --- a/middleware/IPToHost.go +++ b/middleware/IPToHost.go @@ -23,7 +23,11 @@ func IPToHost() aero.Middleware { func GetHostsForIP(ip string) []string { hosts, found := ipToHosts.Get(ip) - if !found || hosts == nil { + if !found { + hosts = findHostsForIP(ip) + } + + if hosts == nil { return nil } @@ -31,17 +35,19 @@ func GetHostsForIP(ip string) []string { } // Finds all host names for the given IP -func findHostsForIP(ip string) { +func findHostsForIP(ip string) []string { hosts, err := net.LookupAddr(ip) if err != nil { - return + return nil } if len(hosts) == 0 { - return + return nil } // Cache host names ipToHosts.Set(ip, hosts, cache.DefaultExpiration) + + return hosts } diff --git a/middleware/Log.go b/middleware/Log.go index 84c53f4d..a010d22a 100644 --- a/middleware/Log.go +++ b/middleware/Log.go @@ -12,52 +12,62 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -// Log middleware logs every request into logs/request.log and errors into logs/error.log. -func Log() aero.Middleware { - request := log.New() +var request = log.New() +var err = log.New() + +// Initialize log files +func init() { request.AddOutput(log.File("logs/request.log")) - err := log.New() err.AddOutput(log.File("logs/error.log")) err.AddOutput(os.Stderr) +} +// Log middleware logs every request into logs/request.log and errors into logs/error.log. +func Log() aero.Middleware { return func(ctx *aero.Context, next func()) { start := time.Now() next() responseTime := time.Since(start) - responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms" - responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString - user := utils.GetUser(ctx) - ip := ctx.RealIP() - - hostName := "" - hostNames := GetHostsForIP(ip) - - if len(hostNames) != 0 { - hostName = hostNames[0] - } - - // Log every request - if user != nil { - request.Info(user.Nick, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) - } else { - request.Info("[guest]", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) - } - - // Log all requests that failed - switch ctx.StatusCode { - case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect: - // Ok. - - default: - err.Error(http.StatusText(ctx.StatusCode), ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) - } - - // Notify us about long requests. - // However ignore requests under /auth/ because those depend on 3rd party servers. - if responseTime >= 200*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") { - err.Error("Long response time", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) - } + go logRequest(ctx, responseTime) + } +} + +// Logs a single request +func logRequest(ctx *aero.Context, responseTime time.Duration) { + responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms" + responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString + + user := utils.GetUser(ctx) + ip := ctx.RealIP() + + hostName := "" + hostNames := GetHostsForIP(ip) + + if len(hostNames) != 0 { + hostName = hostNames[0] + } + + // Log every request + if user != nil { + request.Info(user.Nick, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) + } else { + request.Info("[guest]", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) + } + + // Log all requests that failed + switch ctx.StatusCode { + case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect: + // Ok. + + default: + err.Error(http.StatusText(ctx.StatusCode), ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) + } + + // Notify us about long requests. + // However ignore requests under /auth/ because those depend on 3rd party servers. + if responseTime >= 200*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") { + err.Error("Long response time", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) } }