Add TLS support

This commit is contained in:
Krystian Chachuła 2022-12-15 23:52:51 +01:00 committed by Conrad Hoffmann
parent 7b98e643e8
commit 0a5e6946fb

View File

@ -92,10 +92,14 @@ func main() {
debug bool debug bool
jsonLog bool jsonLog bool
storageURL string storageURL string
cert string
key string
) )
flag.StringVar(&addr, "addr", ":8080", "listening address") flag.StringVar(&addr, "addr", ":8080", "listening address")
flag.StringVar(&authURL, "auth.url", "", "auth backend URL (required)") flag.StringVar(&authURL, "auth.url", "", "auth backend URL (required)")
flag.StringVar(&storageURL, "storage.url", "", "storage backend URL (required)") flag.StringVar(&storageURL, "storage.url", "", "storage backend URL (required)")
flag.StringVar(&cert, "cert", "", "certificate file for TLS")
flag.StringVar(&key, "key", "", "key file for TLS")
flag.BoolVar(&debug, "log.debug", false, "enable debug logs") flag.BoolVar(&debug, "log.debug", false, "enable debug logs")
flag.BoolVar(&jsonLog, "log.json", false, "enable structured logs") flag.BoolVar(&jsonLog, "log.json", false, "enable structured logs")
flag.Parse() flag.Parse()
@ -140,6 +144,10 @@ func main() {
log.Fatal().Err(err).Msg("failed to load storage backend") log.Fatal().Err(err).Msg("failed to load storage backend")
} }
if (cert != "") != (key != "") {
log.Fatal().Msg("provide both cert and key for TLS")
}
carddavHandler := carddav.Handler{Backend: carddavBackend} carddavHandler := carddav.Handler{Backend: carddavBackend}
caldavHandler := caldav.Handler{Backend: caldavBackend} caldavHandler := caldav.Handler{Backend: caldavBackend}
handler := tokidokiHandler{ handler := tokidokiHandler{
@ -163,7 +171,11 @@ func main() {
log.Info().Str("address", addr).Msg("starting server") log.Info().Str("address", addr).Msg("starting server")
log.Debug().Msg("debug output enabled") log.Debug().Msg("debug output enabled")
if (cert != "") && (key != "") {
err = server.ListenAndServeTLS(cert, key)
} else {
err = server.ListenAndServe() err = server.ListenAndServe()
}
if err != http.ErrServerClosed { if err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("ListenAndServe() error") log.Fatal().Err(err).Msg("ListenAndServe() error")
} }