mirror of
https://github.com/joohoi/acme-dns.git
synced 2025-07-13 17:27:51 +07:00
Refactoring main.go
This commit is contained in:
34
main.go
34
main.go
@ -3,8 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/iris-contrib/middleware/cors"
|
|
||||||
"github.com/kataras/iris"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,34 +40,8 @@ func main() {
|
|||||||
// DNS server
|
// DNS server
|
||||||
startDNS(DNSConf.General.Listen)
|
startDNS(DNSConf.General.Listen)
|
||||||
|
|
||||||
// API server and endpoints
|
// HTTP API
|
||||||
api := iris.New()
|
startHTTPAPI()
|
||||||
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)
|
|
||||||
}
|
|
||||||
log.Debugf("Shutting down...")
|
log.Debugf("Shutting down...")
|
||||||
}
|
}
|
||||||
|
34
util.go
34
util.go
@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
"github.com/iris-contrib/middleware/cors"
|
||||||
|
"github.com/kataras/iris"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"github.com/satori/go.uuid"
|
"github.com/satori/go.uuid"
|
||||||
"math/big"
|
"math/big"
|
||||||
@ -63,7 +65,7 @@ func setupLogging(format string, level string) {
|
|||||||
if DNSConf.Logconfig.Format == "json" {
|
if DNSConf.Logconfig.Format == "json" {
|
||||||
log.SetFormatter(&log.JSONFormatter{})
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
}
|
}
|
||||||
switch DNSConf.Logconfig.Level {
|
switch level {
|
||||||
default:
|
default:
|
||||||
log.SetLevel(log.WarnLevel)
|
log.SetLevel(log.WarnLevel)
|
||||||
case "debug":
|
case "debug":
|
||||||
@ -89,3 +91,33 @@ func startDNS(listen string) *dns.Server {
|
|||||||
}()
|
}()
|
||||||
return 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user