Skip to content

Commit

Permalink
Add TLS webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
kpetremann committed Oct 22, 2022
1 parent 16965f7 commit 63413a7
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,31 @@ func quit() {

func main() {
defer quit()
logging.ConfigureLogging()

listenAddress := flag.String("host", "", "listen address")
listenPort := flag.Int("port", 2112, "listen port")
tlsEnabled := flag.Bool("tls", false, "enable TLS")
tlsCert := flag.String("tls-cert", "", "TLS certificated")
tlsKey := flag.String("tls-key", "", "TLS private key")
flag.Parse()
listenSocket := fmt.Sprint(*listenAddress, ":", *listenPort)

logging.ConfigureLogging()
if *tlsEnabled {
missingFlag := false
if *tlsCert == "" {
missingFlag = true
log.Error().Msg("TLS certificate not specified")
}
if *tlsCert == "" {
missingFlag = true
log.Error().Msg("TLS private key not specified")
}
if missingFlag {
return
}
}

listenSocket := fmt.Sprint(*listenAddress, ":", *listenPort)

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
Expand All @@ -50,7 +68,15 @@ func main() {
httpServer := http.Server{Addr: listenSocket, Handler: mux}

go func() {
if err := httpServer.ListenAndServe(); err != nil {
var err error

if !*tlsEnabled {
err = httpServer.ListenAndServe()
} else {
err = httpServer.ListenAndServeTLS(*tlsCert, *tlsKey)
}

if err != nil {
log.Error().Err(err).Send()
stop()
}
Expand Down

0 comments on commit 63413a7

Please sign in to comment.