diff --git a/main.go b/main.go index c49882c..288703b 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,6 @@ package main import ( "fmt" log "github.com/Sirupsen/logrus" - "github.com/iris-contrib/middleware/cors" - "github.com/kataras/iris" "os" ) @@ -42,34 +40,8 @@ func main() { // DNS server startDNS(DNSConf.General.Listen) - // API server and endpoints - api := iris.New() - api.Config.DisableBanner = true - crs := cors.New(cors.Options{ - AllowedOrigins: DNSConf.API.CorsOrigins, - AllowedMethods: []string{"GET", "POST"}, - OptionsPassthrough: false, - Debug: DNSConf.General.Debug, - }) - api.Use(crs) - var ForceAuth = authMiddleware{} - api.Get("/register", webRegisterGet) - api.Post("/register", webRegisterPost) - api.Post("/update", ForceAuth.Serve, webUpdatePost) - // TODO: migrate to api.Serve(iris.LETSENCRYPTPROD("mydomain.com")) - switch DNSConf.API.TLS { - case "letsencrypt": - host := DNSConf.API.Domain + ":" + DNSConf.API.Port - api.Listen(host) - case "cert": - host := DNSConf.API.Domain + ":" + DNSConf.API.Port - api.ListenTLS(host, DNSConf.API.TLSCertFullchain, DNSConf.API.TLSCertPrivkey) - default: - host := DNSConf.API.Domain + ":" + DNSConf.API.Port - api.Listen(host) - } - if err != nil { - log.Errorf("Error in HTTP server [%v]", err) - } + // HTTP API + startHTTPAPI() + log.Debugf("Shutting down...") } diff --git a/util.go b/util.go index 282ba99..69adca6 100644 --- a/util.go +++ b/util.go @@ -6,6 +6,8 @@ import ( "fmt" "github.com/BurntSushi/toml" log "github.com/Sirupsen/logrus" + "github.com/iris-contrib/middleware/cors" + "github.com/kataras/iris" "github.com/miekg/dns" "github.com/satori/go.uuid" "math/big" @@ -63,7 +65,7 @@ func setupLogging(format string, level string) { if DNSConf.Logconfig.Format == "json" { log.SetFormatter(&log.JSONFormatter{}) } - switch DNSConf.Logconfig.Level { + switch level { default: log.SetLevel(log.WarnLevel) case "debug": @@ -89,3 +91,33 @@ func startDNS(listen string) *dns.Server { }() return server } + +func startHTTPAPI() { + api := iris.New() + api.Config.DisableBanner = true + crs := cors.New(cors.Options{ + AllowedOrigins: DNSConf.API.CorsOrigins, + AllowedMethods: []string{"GET", "POST"}, + OptionsPassthrough: false, + Debug: DNSConf.General.Debug, + }) + api.Use(crs) + var ForceAuth = authMiddleware{} + api.Get("/register", webRegisterGet) + api.Post("/register", webRegisterPost) + api.Post("/update", ForceAuth.Serve, webUpdatePost) + switch DNSConf.API.TLS { + case "letsencrypt": + listener, err := iris.LETSENCRYPTPROD(DNSConf.API.Domain) + err = api.Serve(listener) + if err != nil { + log.Errorf("Error in HTTP server [%v]", err) + } + case "cert": + host := DNSConf.API.Domain + ":" + DNSConf.API.Port + api.ListenTLS(host, DNSConf.API.TLSCertFullchain, DNSConf.API.TLSCertPrivkey) + default: + host := DNSConf.API.Domain + ":" + DNSConf.API.Port + api.Listen(host) + } +} diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..381bb86 --- /dev/null +++ b/util_test.go @@ -0,0 +1,25 @@ +package main + +import ( + log "github.com/Sirupsen/logrus" + "testing" +) + +func TestSetupLogging(t *testing.T) { + for i, test := range []struct { + format string + level string + expected string + }{ + {"text", "warning", "warning"}, + {"json", "debug", "debug"}, + {"text", "info", "info"}, + {"json", "error", "error"}, + {"text", "something", "warning"}, + } { + setupLogging(test.format, test.level) + if log.GetLevel().String() != test.expected { + t.Errorf("Test %d: Expected loglevel %s but got %s", i, test.expected, log.GetLevel().String()) + } + } +}