60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"regexp"
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type (
|
|
CertPool struct{ *x509.CertPool }
|
|
CertList struct{ *pkix.CertificateList }
|
|
Targets struct {
|
|
targets []*url.URL
|
|
}
|
|
Config struct {
|
|
URLs *Targets `envconfig:"URLS" yaml:"urls" default:"http://localhost:9100/metrics"`
|
|
Listen string `envconfig:"LISTEN" yaml:"listen" default:"0.0.0.0:9443"`
|
|
|
|
DisableTLS bool `envconfig:"DISABLE_TLS" yaml:"disable_tls" default:"false"`
|
|
ClientCA *CertPool `envconfig:"CA_FILE" yaml:"ca_file"`
|
|
CRL *CertList `envconfig:"CRL_FILE" yaml:"crl_file"`
|
|
Cert string `envconfig:"CERT_FILE" yaml:"cert_file"`
|
|
Key string `envconfig:"KEY_FILE" yaml:"key_file"`
|
|
}
|
|
)
|
|
|
|
var re = regexp.MustCompile("^(?P<name>[^#][^ {]+)(?:{(?P<labels>.*)})? (?P<value>-?[0-9]+(?:\\.[0-9]+)?)")
|
|
|
|
func main() {
|
|
f, err := os.Open("test.yml")
|
|
config := &Config{}
|
|
if err == nil {
|
|
err := yaml.NewDecoder(f).Decode(config)
|
|
if err != nil {
|
|
log.Fatalf("unable to parse yaml: %s", err)
|
|
}
|
|
} else {
|
|
log.Printf("unable to open config file: %s", err)
|
|
log.Printf("fallback to environment")
|
|
err = envconfig.Process("", config)
|
|
if err != nil {
|
|
log.Fatalf("unable to parse environment: %s", err)
|
|
}
|
|
}
|
|
if !config.DisableTLS && (config.CRL == nil || config.ClientCA == nil || config.Cert == "" || config.Key == "") {
|
|
log.Fatalf("please specifiy all tls parameters or disable tls")
|
|
}
|
|
|
|
web := NewWeb(config)
|
|
err = web.ListenAndServe()
|
|
if err != nil {
|
|
log.Fatalf("unable to listen: %s", err)
|
|
}
|
|
}
|