blob: b05ff2f5b031eb6692b5fd3a075ff03bf2356e82 [file] [log] [blame]
package main
import (
"context"
"flag"
"io/ioutil"
"net/http"
"time"
"code.hackerspace.pl/hscloud/go/mirko"
"github.com/golang/glog"
)
var (
flagListen = "127.0.0.1:8080"
flagConfig = ""
flagTLSKey = ""
flagTLSCert = ""
)
func main() {
flag.StringVar(&flagListen, "admitomatic_listen", flagListen, "Address to listen on for HTTP traffic")
flag.StringVar(&flagTLSKey, "admitomatic_tls_key", flagTLSKey, "TLS key to serve HTTP with")
flag.StringVar(&flagTLSCert, "admitomatic_tls_cert", flagTLSCert, "TLS certificate to serve HTTP with")
flag.StringVar(&flagConfig, "admitomatic_config", flagConfig, "Config path (prototext format)")
flag.Parse()
if flagConfig == "" {
glog.Exitf("-admitomatic_config must be set")
}
if flagTLSKey == "" {
glog.Exitf("-admitomatic_tls_key must be set")
}
if flagTLSCert == "" {
glog.Exitf("-admitomatic_tls_cert must be set")
}
m := mirko.New()
if err := m.Listen(); err != nil {
glog.Exitf("Listen(): %v", err)
}
if err := m.Serve(); err != nil {
glog.Exitf("Serve(): %v", err)
}
configData, err := ioutil.ReadFile(flagConfig)
if err != nil {
glog.Exitf("Could not read config: %v", err)
}
s, err := newService(configData)
if err != nil {
glog.Exitf("Could not start service: %v", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", s.handler)
// TODO(q3k): implement admission controller
srv := &http.Server{Addr: flagListen, Handler: mux}
glog.Infof("Listening on %q...", flagListen)
go func() {
if err := srv.ListenAndServeTLS(flagTLSCert, flagTLSKey); err != nil {
glog.Error(err)
}
}()
<-m.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv.Shutdown(ctx)
}