mirror of
https://github.com/joohoi/acme-dns.git
synced 2024-12-22 21:23:52 +07:00
Refactoring main.go
This commit is contained in:
parent
74b82c87a6
commit
e9f18c99d8
34
main.go
34
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...")
|
||||
}
|
||||
|
34
util.go
34
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)
|
||||
}
|
||||
}
|
||||
|
25
util_test.go
Normal file
25
util_test.go
Normal 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())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user