Split rest of srv into packages

This commit is contained in:
Igor Chubin 2022-12-03 16:08:43 +01:00
parent 0d42c23d5b
commit 12df32b07c
6 changed files with 58 additions and 44 deletions

View file

@ -9,26 +9,12 @@ import (
"time" "time"
"github.com/chubin/wttr.in/internal/config" "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 " 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) { func copyHeader(dst, src http.Header) {
for k, vv := range src { for k, vv := range src {
for _, v := range vv { for _, v := range vv {
@ -77,11 +63,11 @@ func main() {
mux *http.ServeMux = http.NewServeMux() mux *http.ServeMux = http.NewServeMux()
// logger is optimized requests logger. // logger is optimized requests logger.
logger *RequestLogger = NewRequestLogger( logger *logging.RequestLogger = logging.NewRequestLogger(
config.Conf.Logging.AccessLog, config.Conf.Logging.AccessLog,
time.Duration(config.Conf.Logging.Interval)*time.Second) time.Duration(config.Conf.Logging.Interval)*time.Second)
rp *RequestProcessor rp *processor.RequestProcessor
// errs is the servers errors channel. // errs is the servers errors channel.
errs chan error = make(chan error, 1) errs chan error = make(chan error, 1)
@ -89,7 +75,7 @@ func main() {
// numberOfServers started. If 0, exit. // numberOfServers started. If 0, exit.
numberOfServers int numberOfServers int
errorsLog *LogSuppressor = NewLogSuppressor( errorsLog *logging.LogSuppressor = logging.NewLogSuppressor(
config.Conf.Logging.ErrorsLog, config.Conf.Logging.ErrorsLog,
[]string{ []string{
"error reading preface from client", "error reading preface from client",
@ -101,7 +87,7 @@ func main() {
err error err error
) )
rp, err = NewRequestProcessor(config.Conf) rp, err = processor.NewRequestProcessor(config.Conf)
if err != nil { if err != nil {
log.Fatalln("log processor initialization:", err) log.Fatalln("log processor initialization:", err)
} }

View file

@ -1,4 +1,4 @@
package main package logging
import ( import (
"fmt" "fmt"
@ -6,6 +6,8 @@ import (
"os" "os"
"sync" "sync"
"time" "time"
"github.com/chubin/wttr.in/internal/util"
) )
// Logging request. // Logging request.
@ -43,7 +45,7 @@ func NewRequestLogger(filename string, period time.Duration) *RequestLogger {
func (rl *RequestLogger) Log(r *http.Request) error { func (rl *RequestLogger) Log(r *http.Request) error {
le := logEntry{ le := logEntry{
Proto: "http", Proto: "http",
IP: readUserIP(r), IP: util.ReadUserIP(r),
URI: r.RequestURI, URI: r.RequestURI,
UserAgent: r.Header.Get("User-Agent"), UserAgent: r.Header.Get("User-Agent"),
} }

View file

@ -1,4 +1,4 @@
package main package logging
import ( import (
"os" "os"

View file

@ -1,4 +1,4 @@
package main package processor
import ( import (
"log" "log"

View file

@ -1,4 +1,4 @@
package main package processor
import ( import (
"context" "context"
@ -12,13 +12,30 @@ import (
"sync" "sync"
"time" "time"
lru "github.com/hashicorp/golang-lru"
"github.com/chubin/wttr.in/internal/config" "github.com/chubin/wttr.in/internal/config"
"github.com/chubin/wttr.in/internal/routing" "github.com/chubin/wttr.in/internal/routing"
"github.com/chubin/wttr.in/internal/stats" "github.com/chubin/wttr.in/internal/stats"
"github.com/chubin/wttr.in/internal/util"
lru "github.com/hashicorp/golang-lru"
) )
// 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 { type responseWithHeader struct {
InProgress bool // true if the request is being processed InProgress bool // true if the request is being processed
Expires time.Time // expiration time of the cache entry Expires time.Time // expiration time of the cache entry
@ -213,7 +230,7 @@ func getCacheDigest(req *http.Request) string {
queryHost := req.Host queryHost := req.Host
queryString := req.RequestURI queryString := req.RequestURI
clientIPAddress := readUserIP(req) clientIPAddress := util.ReadUserIP(req)
lang := req.Header.Get("Accept-Language") lang := req.Header.Get("Accept-Language")
@ -279,22 +296,6 @@ func isPlainTextAgent(userAgent string) bool {
return false 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 { func randInt(min int, max int) int {
return min + rand.Intn(max-min) return min + rand.Intn(max-min)
} }

25
internal/util/http.go Normal file
View file

@ -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
}