Refactoring main.go

This commit is contained in:
Joona Hoikkala 2016-11-27 22:09:13 +02:00
parent 74b82c87a6
commit e9f18c99d8
No known key found for this signature in database
GPG Key ID: C14AAE0F5ADCB854
3 changed files with 61 additions and 32 deletions

34
main.go
View File

@ -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...")
}

34
util.go
View File

@ -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)
}
}

25
util_test.go Normal file
View File

@ -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())
}
}
}