diff --git a/internal/config/config.go b/internal/config/config.go index c6366ea..de4024d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,6 +12,7 @@ import ( // Config of the program. type Config struct { Cache + Geo Logging Server Uplink @@ -67,12 +68,21 @@ type Cache struct { Size int `yaml:"size,omitempty"` } +// Geo contains geolocation configuration. +type Geo struct { + // IPCache contains the path to the IP Geodata cache. + IPCache string `yaml:"ipCache,omitempty"` +} + // Default contains the default configuration. func Default() *Config { return &Config{ Cache{ Size: 12800, }, + Geo{ + IPCache: "/wttr.in/cache/ip2l", + }, Logging{ AccessLog: "/wttr.in/log/access.log", ErrorsLog: "/wttr.in/log/errors.log", diff --git a/internal/processor/processor.go b/internal/processor/processor.go index da829fa..6448d9a 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -15,6 +15,7 @@ import ( lru "github.com/hashicorp/golang-lru" "github.com/chubin/wttr.in/internal/config" + geoip "github.com/chubin/wttr.in/internal/geo/ip" "github.com/chubin/wttr.in/internal/routing" "github.com/chubin/wttr.in/internal/stats" "github.com/chubin/wttr.in/internal/util" @@ -54,6 +55,7 @@ type RequestProcessor struct { router routing.Router upstreamTransport *http.Transport config *config.Config + geoIPCache *geoip.Cache } // NewRequestProcessor returns new RequestProcessor. @@ -80,6 +82,7 @@ func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) { stats: stats.New(), upstreamTransport: transport, config: config, + geoIPCache: geoip.NewCache(config), } // Initialize routes. @@ -161,6 +164,13 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader } } + // How many IP addresses are known. + ip := util.ReadUserIP(r) + _, err = rp.geoIPCache.Read(ip) + if err == nil { + rp.stats.Inc("geoip") + } + rp.lruCache.Add(cacheDigest, responseWithHeader{InProgress: true}) response, err = get(r, rp.upstreamTransport) diff --git a/internal/stats/stats.go b/internal/stats/stats.go index a030add..220c0ec 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -75,6 +75,7 @@ func (c *Stats) Show() []byte { fmt.Fprintf(&b, "%-20s: %d\n", "Upstream queries", c.v["total"]-c.v["cache1"]) fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format", c.v["format"]) fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format=j1", c.v["format=j1"]) + fmt.Fprintf(&b, "%-20s: %d\n", "Queries with known IP", c.v["geoip"]) return b.Bytes() }