From 12df32b07c5fdf97f7155edbe9df886037953a02 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sat, 3 Dec 2022 16:08:43 +0100 Subject: [PATCH] Split rest of srv into packages --- cmd/srv.go | 26 +++--------- cmd/log.go => internal/logging/logging.go | 6 ++- .../logging/suppress.go | 2 +- .../processor/peak.go | 2 +- .../processor/processor.go | 41 ++++++++++--------- internal/util/http.go | 25 +++++++++++ 6 files changed, 58 insertions(+), 44 deletions(-) rename cmd/log.go => internal/logging/logging.go (95%) rename cmd/logsuppress.go => internal/logging/suppress.go (98%) rename cmd/peakHandling.go => internal/processor/peak.go (98%) rename cmd/processRequest.go => internal/processor/processor.go (94%) create mode 100644 internal/util/http.go diff --git a/cmd/srv.go b/cmd/srv.go index 6246eb4..e9ae7e1 100644 --- a/cmd/srv.go +++ b/cmd/srv.go @@ -9,26 +9,12 @@ import ( "time" "github.com/chubin/wttr.in/internal/config" + "github.com/chubin/wttr.in/internal/logging" + "github.com/chubin/wttr.in/internal/processor" ) const logLineStart = "LOG_LINE_START " -// plainTextAgents contains signatures of the plain-text agents -var plainTextAgents = []string{ - "curl", - "httpie", - "lwp-request", - "wget", - "python-httpx", - "python-requests", - "openbsd ftp", - "powershell", - "fetch", - "aiohttp", - "http_get", - "xh", -} - func copyHeader(dst, src http.Header) { for k, vv := range src { for _, v := range vv { @@ -77,11 +63,11 @@ func main() { mux *http.ServeMux = http.NewServeMux() // logger is optimized requests logger. - logger *RequestLogger = NewRequestLogger( + logger *logging.RequestLogger = logging.NewRequestLogger( config.Conf.Logging.AccessLog, time.Duration(config.Conf.Logging.Interval)*time.Second) - rp *RequestProcessor + rp *processor.RequestProcessor // errs is the servers errors channel. errs chan error = make(chan error, 1) @@ -89,7 +75,7 @@ func main() { // numberOfServers started. If 0, exit. numberOfServers int - errorsLog *LogSuppressor = NewLogSuppressor( + errorsLog *logging.LogSuppressor = logging.NewLogSuppressor( config.Conf.Logging.ErrorsLog, []string{ "error reading preface from client", @@ -101,7 +87,7 @@ func main() { err error ) - rp, err = NewRequestProcessor(config.Conf) + rp, err = processor.NewRequestProcessor(config.Conf) if err != nil { log.Fatalln("log processor initialization:", err) } diff --git a/cmd/log.go b/internal/logging/logging.go similarity index 95% rename from cmd/log.go rename to internal/logging/logging.go index 3de1b1c..e58b6e1 100644 --- a/cmd/log.go +++ b/internal/logging/logging.go @@ -1,4 +1,4 @@ -package main +package logging import ( "fmt" @@ -6,6 +6,8 @@ import ( "os" "sync" "time" + + "github.com/chubin/wttr.in/internal/util" ) // Logging request. @@ -43,7 +45,7 @@ func NewRequestLogger(filename string, period time.Duration) *RequestLogger { func (rl *RequestLogger) Log(r *http.Request) error { le := logEntry{ Proto: "http", - IP: readUserIP(r), + IP: util.ReadUserIP(r), URI: r.RequestURI, UserAgent: r.Header.Get("User-Agent"), } diff --git a/cmd/logsuppress.go b/internal/logging/suppress.go similarity index 98% rename from cmd/logsuppress.go rename to internal/logging/suppress.go index d8ca9e6..76cecce 100644 --- a/cmd/logsuppress.go +++ b/internal/logging/suppress.go @@ -1,4 +1,4 @@ -package main +package logging import ( "os" diff --git a/cmd/peakHandling.go b/internal/processor/peak.go similarity index 98% rename from cmd/peakHandling.go rename to internal/processor/peak.go index 0195c3c..55f8ae6 100644 --- a/cmd/peakHandling.go +++ b/internal/processor/peak.go @@ -1,4 +1,4 @@ -package main +package processor import ( "log" diff --git a/cmd/processRequest.go b/internal/processor/processor.go similarity index 94% rename from cmd/processRequest.go rename to internal/processor/processor.go index 0aa6085..da829fa 100644 --- a/cmd/processRequest.go +++ b/internal/processor/processor.go @@ -1,4 +1,4 @@ -package main +package processor import ( "context" @@ -12,13 +12,30 @@ import ( "sync" "time" + lru "github.com/hashicorp/golang-lru" + "github.com/chubin/wttr.in/internal/config" "github.com/chubin/wttr.in/internal/routing" "github.com/chubin/wttr.in/internal/stats" - - lru "github.com/hashicorp/golang-lru" + "github.com/chubin/wttr.in/internal/util" ) +// plainTextAgents contains signatures of the plain-text agents. +var plainTextAgents = []string{ + "curl", + "httpie", + "lwp-request", + "wget", + "python-httpx", + "python-requests", + "openbsd ftp", + "powershell", + "fetch", + "aiohttp", + "http_get", + "xh", +} + type responseWithHeader struct { InProgress bool // true if the request is being processed Expires time.Time // expiration time of the cache entry @@ -213,7 +230,7 @@ func getCacheDigest(req *http.Request) string { queryHost := req.Host queryString := req.RequestURI - clientIPAddress := readUserIP(req) + clientIPAddress := util.ReadUserIP(req) lang := req.Header.Get("Accept-Language") @@ -279,22 +296,6 @@ func isPlainTextAgent(userAgent string) bool { return false } -func readUserIP(r *http.Request) string { - IPAddress := r.Header.Get("X-Real-Ip") - if IPAddress == "" { - IPAddress = r.Header.Get("X-Forwarded-For") - } - if IPAddress == "" { - IPAddress = r.RemoteAddr - var err error - IPAddress, _, err = net.SplitHostPort(IPAddress) - if err != nil { - log.Printf("ERROR: userip: %q is not IP:port\n", IPAddress) - } - } - return IPAddress -} - func randInt(min int, max int) int { return min + rand.Intn(max-min) } diff --git a/internal/util/http.go b/internal/util/http.go new file mode 100644 index 0000000..ab3f40b --- /dev/null +++ b/internal/util/http.go @@ -0,0 +1,25 @@ +package util + +import ( + "log" + "net" + "net/http" +) + +// ReadUserIP returns IP address of the client from http.Request, +// taking into account the HTTP headers. +func ReadUserIP(r *http.Request) string { + IPAddress := r.Header.Get("X-Real-Ip") + if IPAddress == "" { + IPAddress = r.Header.Get("X-Forwarded-For") + } + if IPAddress == "" { + IPAddress = r.RemoteAddr + var err error + IPAddress, _, err = net.SplitHostPort(IPAddress) + if err != nil { + log.Printf("ERROR: userip: %q is not IP:port\n", IPAddress) + } + } + return IPAddress +}