forked from fiorix/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
115 lines (105 loc) · 4.72 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2009 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package apiserver
import (
"flag"
"io"
"log"
"os"
"time"
"github.com/fiorix/freegeoip"
)
// Config is the configuration of the freegeoip server.
type Config struct {
ServerAddr string
TLSServerAddr string
TLSCertFile string
TLSKeyFile string
APIPrefix string
CORSOrigin string
ReadTimeout time.Duration
WriteTimeout time.Duration
PublicDir string
DB string
UpdateInterval time.Duration
RetryInterval time.Duration
UseXForwardedFor bool
Silent bool
LogToStdout bool
LogTimestamp bool
RedisAddr string
RedisTimeout time.Duration
MemcacheAddr string
MemcacheTimeout time.Duration
RateLimitBackend string
RateLimitLimit uint64
RateLimitInterval time.Duration
InternalServerAddr string
errorLog *log.Logger
accessLog *log.Logger
}
// NewConfig creates and initializes a new Config with default values.
func NewConfig() *Config {
return &Config{
ServerAddr: ":8080",
TLSCertFile: "cert.pem",
TLSKeyFile: "key.pem",
APIPrefix: "/",
CORSOrigin: "*",
ReadTimeout: 30 * time.Second,
WriteTimeout: 15 * time.Second,
DB: freegeoip.MaxMindDB,
UpdateInterval: 24 * time.Hour,
RetryInterval: 2 * time.Hour,
LogTimestamp: true,
RedisAddr: "localhost:6379",
RedisTimeout: time.Second,
MemcacheAddr: "localhost:11211",
MemcacheTimeout: time.Second,
RateLimitBackend: "redis",
RateLimitInterval: time.Hour,
}
}
// AddFlags adds configuration flags to the given FlagSet.
func (c *Config) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&c.ServerAddr, "http", c.ServerAddr, "Address in form of ip:port to listen on for HTTP")
fs.StringVar(&c.TLSServerAddr, "https", c.TLSServerAddr, "Address in form of ip:port to listen on for HTTPS")
fs.StringVar(&c.TLSCertFile, "cert", c.TLSCertFile, "X.509 certificate file for HTTPS server")
fs.StringVar(&c.TLSKeyFile, "key", c.TLSKeyFile, "X.509 key file for HTTPS server")
fs.StringVar(&c.APIPrefix, "api-prefix", c.APIPrefix, "URL prefix for API endpoints")
fs.StringVar(&c.CORSOrigin, "cors-origin", c.CORSOrigin, "CORS origin API endpoints")
fs.DurationVar(&c.ReadTimeout, "read-timeout", c.ReadTimeout, "Read timeout for HTTP and HTTPS client conns")
fs.DurationVar(&c.WriteTimeout, "write-timeout", c.WriteTimeout, "Write timeout for HTTP and HTTPS client conns")
fs.StringVar(&c.PublicDir, "public", c.PublicDir, "Public directory to serve at the {prefix}/ endpoint")
fs.StringVar(&c.DB, "db", c.DB, "IP database file or URL")
fs.DurationVar(&c.UpdateInterval, "update", c.UpdateInterval, "Database update check interval")
fs.DurationVar(&c.RetryInterval, "retry", c.RetryInterval, "Max time to wait before retrying to download database")
fs.BoolVar(&c.UseXForwardedFor, "use-x-forwarded-for", c.UseXForwardedFor, "Use the X-Forwarded-For header when available (e.g. behind proxy)")
fs.BoolVar(&c.Silent, "silent", c.Silent, "Disable HTTP and HTTPS log request details")
fs.BoolVar(&c.LogToStdout, "logtostdout", c.LogToStdout, "Log to stdout instead of stderr")
fs.BoolVar(&c.LogTimestamp, "logtimestamp", c.LogTimestamp, "Prefix non-access logs with timestamp")
fs.StringVar(&c.RedisAddr, "redis", c.RedisAddr, "Redis address in form of host:port[,host:port] for quota")
fs.DurationVar(&c.RedisTimeout, "redis-timeout", c.RedisTimeout, "Redis read/write timeout")
fs.StringVar(&c.MemcacheAddr, "memcache", c.MemcacheAddr, "Memcache address in form of host:port[,host:port] for quota")
fs.DurationVar(&c.MemcacheTimeout, "memcache-timeout", c.MemcacheTimeout, "Memcache read/write timeout")
fs.StringVar(&c.RateLimitBackend, "quota-backend", c.RateLimitBackend, "Backend for rate limiter: map, redis, or memcache")
fs.Uint64Var(&c.RateLimitLimit, "quota-max", c.RateLimitLimit, "Max requests per source IP per interval; set 0 to turn quotas off")
fs.DurationVar(&c.RateLimitInterval, "quota-interval", c.RateLimitInterval, "Quota expiration interval, per source IP querying the API")
fs.StringVar(&c.InternalServerAddr, "internal-server", c.InternalServerAddr, "Address in form of ip:port to listen on for metrics and pprof")
}
func (c *Config) logWriter() io.Writer {
if c.LogToStdout {
return os.Stdout
}
return os.Stderr
}
func (c *Config) errorLogger() *log.Logger {
if c.LogTimestamp {
return log.New(c.logWriter(), "[error] ", log.LstdFlags)
}
return log.New(c.logWriter(), "[error] ", 0)
}
func (c *Config) accessLogger() *log.Logger {
return log.New(c.logWriter(), "[access] ", 0)
}