wttr.in/internal/config/config.go

159 lines
3.2 KiB
Go
Raw Normal View History

2022-11-29 20:38:39 +00:00
package config
2022-11-19 18:19:42 +00:00
2022-12-03 16:53:50 +00:00
import (
"log"
"os"
"gopkg.in/yaml.v3"
2022-12-06 17:52:26 +00:00
"github.com/chubin/wttr.in/internal/types"
2022-12-03 16:53:50 +00:00
"github.com/chubin/wttr.in/internal/util"
)
2022-11-19 18:19:42 +00:00
// Config of the program.
type Config struct {
2022-12-02 19:10:32 +00:00
Cache
2022-12-04 15:55:14 +00:00
Geo
2022-11-19 18:19:42 +00:00
Logging
2022-11-19 18:34:11 +00:00
Server
2022-12-02 19:10:32 +00:00
Uplink
2022-11-19 18:19:42 +00:00
}
// Logging configuration.
type Logging struct {
// AccessLog path.
2022-12-03 16:53:50 +00:00
AccessLog string `yaml:"accessLog,omitempty"`
2022-11-19 18:19:42 +00:00
2022-11-20 16:53:51 +00:00
// ErrorsLog path.
2022-12-03 16:53:50 +00:00
ErrorsLog string `yaml:"errorsLog,omitempty"`
2022-11-20 16:53:51 +00:00
2022-11-19 18:19:42 +00:00
// Interval between access log flushes, in seconds.
2022-12-03 16:53:50 +00:00
Interval int `yaml:"interval,omitempty"`
2022-11-19 18:19:42 +00:00
}
2022-11-19 18:34:11 +00:00
// Server configuration.
type Server struct {
// PortHTTP is port where HTTP server must listen.
// If 0, HTTP is disabled.
2022-12-03 16:53:50 +00:00
PortHTTP int `yaml:"portHTTP,omitempty"`
2022-11-19 18:34:11 +00:00
// PortHTTP is port where the HTTPS server must listen.
// If 0, HTTPS is disabled.
2022-12-03 16:53:50 +00:00
PortHTTPS int `yaml:"portHTTPS,omitempty"`
2022-11-20 09:03:31 +00:00
// TLSCertFile contains path to cert file for TLS Server.
2022-12-03 16:53:50 +00:00
TLSCertFile string `yaml:"tlsCertFile,omitempty"`
2022-11-20 09:03:31 +00:00
// TLSCertFile contains path to key file for TLS Server.
2022-12-03 16:53:50 +00:00
TLSKeyFile string `yaml:"tlsKeyFile,omitempty"`
2022-11-19 18:34:11 +00:00
}
2022-12-02 19:10:32 +00:00
// Uplink configuration.
type Uplink struct {
// Address contains address of the uplink server in form IP:PORT.
2022-12-03 16:53:50 +00:00
Address string `yaml:"address,omitempty"`
2022-12-02 19:10:32 +00:00
// Timeout for upstream queries.
2022-12-03 16:53:50 +00:00
Timeout int `yaml:"timeout,omitempty"`
2022-12-02 19:10:32 +00:00
// PrefetchInterval contains time (in milliseconds) indicating,
// how long the prefetch procedure should take.
2022-12-03 16:53:50 +00:00
PrefetchInterval int `yaml:"prefetchInterval,omitempty"`
2022-12-02 19:10:32 +00:00
}
// Cache configuration.
type Cache struct {
// Size of the main cache.
2022-12-03 16:53:50 +00:00
Size int `yaml:"size,omitempty"`
2022-11-19 18:19:42 +00:00
}
2022-12-02 19:10:32 +00:00
2022-12-04 15:55:14 +00:00
// Geo contains geolocation configuration.
type Geo struct {
// IPCache contains the path to the IP Geodata cache.
IPCache string `yaml:"ipCache,omitempty"`
2022-12-04 20:16:23 +00:00
// IPCacheDB contains the path to the SQLite DB with the IP Geodata cache.
IPCacheDB string `yaml:"ipCacheDB,omitempty"`
2022-12-06 17:52:26 +00:00
CacheType types.CacheType `yaml:"cacheType,omitempty"`
2022-12-09 20:02:47 +00:00
Nominatim []Nominatim
}
type Nominatim struct {
Name string
URL string
Token string
2022-12-04 15:55:14 +00:00
}
2022-12-02 19:10:32 +00:00
// Default contains the default configuration.
func Default() *Config {
return &Config{
Cache{
Size: 12800,
},
2022-12-04 15:55:14 +00:00
Geo{
2022-12-04 20:16:23 +00:00
IPCache: "/wttr.in/cache/ip2l",
IPCacheDB: "/wttr.in/cache/geoip.db",
2022-12-07 19:10:38 +00:00
CacheType: types.CacheTypeDB,
2022-12-09 20:02:47 +00:00
Nominatim: []Nominatim{
{
Name: "locationiq",
URL: "https://eu1.locationiq.com/v1/search",
Token: os.Getenv("NOMINATIM_LOCATIONIQ"),
},
},
2022-12-04 15:55:14 +00:00
},
2022-12-02 19:10:32 +00:00
Logging{
AccessLog: "/wttr.in/log/access.log",
ErrorsLog: "/wttr.in/log/errors.log",
Interval: 300,
},
Server{
PortHTTP: 8083,
PortHTTPS: 8084,
TLSCertFile: "/wttr.in/etc/fullchain.pem",
TLSKeyFile: "/wttr.in/etc/privkey.pem",
},
Uplink{
Address: "127.0.0.1:9002",
Timeout: 30,
PrefetchInterval: 300,
},
}
}
2022-12-03 16:53:50 +00:00
// Load config from file.
func Load(filename string) (*Config, error) {
var (
config Config
data []byte
err error
)
data, err = os.ReadFile(filename)
if err != nil {
return nil, err
}
err = util.YamlUnmarshalStrict(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func (c *Config) Dump() []byte {
data, err := yaml.Marshal(c)
if err != nil {
// should never happen.
log.Fatalln("config.Dump():", err)
}
return data
}