feat: autogénérer certificat TLS si non-fourni

This commit is contained in:
Victor Lacasse-Beaudoin 2025-05-13 16:43:27 -04:00
parent 940c6d8a25
commit 47b8c2b766
3 changed files with 102 additions and 3 deletions

View file

@ -3,7 +3,9 @@ package presences
import (
"context"
"crypto/subtle"
"crypto/tls"
"fmt"
"log"
"net/http"
"git.agecem.com/bottin/bottin/v11"
@ -69,7 +71,30 @@ func RunUIServer(ctx context.Context, cfg Config, bottinClient *bottin.APIClient
address := fmt.Sprintf(":%d", cfg.Port)
return e.StartTLS(address, cfg.TLS.Cert, cfg.TLS.Key)
}
switch {
case cfg.TLS.Cert != "" && cfg.TLS.Key != "":
return e.StartTLS(address, cfg.TLS.Cert, cfg.TLS.Key)
case cfg.TLS.Cert != "" && cfg.TLS.Key == "":
return fmt.Errorf("found TLS certificate but missing associated TLS private key")
case cfg.TLS.Cert == "" && cfg.TLS.Key != "":
return fmt.Errorf("found TLS private key but missing associated TLS certificate")
default:
log.Println("No TLS pair was provided. Generating self-signed pair.")
tlsPair, err := newTLSPair()
if err != nil {
return err
}
server := &http.Server{
Addr: address,
Handler: e,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{tlsPair},
},
}
return server.ListenAndServeTLS("", "")
}
}
}