mirror of
https://github.com/chubin/wttr.in
synced 2025-01-12 03:58:45 +00:00
Count number of queries with known IPs
This commit is contained in:
parent
074b8b6ec8
commit
3765dcbfbf
3 changed files with 21 additions and 0 deletions
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue