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"
"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)
}

View file

@ -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"),
}

View file

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

View file

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

View file

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

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
}