Extract startServer method from main()

This commit is contained in:
Jan Dittberner 2021-09-19 19:54:00 +02:00 committed by Jan Dittberner
parent 88bfe0a5df
commit 26447f99c9

View file

@ -33,6 +33,7 @@ import (
"github.com/go-openapi/runtime/client" "github.com/go-openapi/runtime/client"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
"github.com/knadh/koanf"
hydra "github.com/ory/hydra-client-go/client" hydra "github.com/ory/hydra-client-go/client"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -145,22 +146,32 @@ func main() {
logger.Fatalf("could not initialize request error handling: %v", err) logger.Fatalf("could not initialize request error handling: %v", err)
} }
handlerChain := tracing(logging(hsts(errorMiddleware(csrfProtect(router)))))
startServer(ctx, handlerChain, logger, config)
}
func startServer(ctx context.Context, handlerChain http.Handler, logger *log.Logger, config *koanf.Koanf) {
clientCertificateCAFile := config.MustString("security.client.ca-file")
serverName := config.String("server.name")
serverPort := config.Int("server.port")
clientCertPool := x509.NewCertPool() clientCertPool := x509.NewCertPool()
pemBytes, err := ioutil.ReadFile(config.MustString("security.client.ca-file")) pemBytes, err := ioutil.ReadFile(clientCertificateCAFile)
if err != nil { if err != nil {
logger.Fatalf("could not load client CA certificates: %v", err) logger.Fatalf("could not load client CA certificates: %v", err)
} }
clientCertPool.AppendCertsFromPEM(pemBytes) clientCertPool.AppendCertsFromPEM(pemBytes)
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
ServerName: config.String("server.name"), ServerName: serverName,
MinVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12,
ClientAuth: tls.VerifyClientCertIfGiven, ClientAuth: tls.VerifyClientCertIfGiven,
ClientCAs: clientCertPool, ClientCAs: clientCertPool,
} }
server := &http.Server{ server := &http.Server{
Addr: fmt.Sprintf("%s:%d", config.String("server.name"), config.Int("server.port")), Addr: fmt.Sprintf("%s:%d", serverName, serverPort),
Handler: tracing(logging(hsts(errorMiddleware(csrfProtect(router))))), Handler: handlerChain,
ReadTimeout: 20 * time.Second, ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second, WriteTimeout: 20 * time.Second,
IdleTimeout: 30 * time.Second, IdleTimeout: 30 * time.Second,