put vapid keygen behind a flag

This commit is contained in:
2025-05-10 15:28:55 +01:00
parent 93bf51a6f5
commit 0ffd6b9b94
4 changed files with 22 additions and 32 deletions

19
main.go
View File

@@ -102,6 +102,14 @@ var supportedLocations = map[string]location{
func main() {
port := flag.Int("port", 8080, "the port that the server should listen on")
genKeys := flag.Bool("generate-vapid-keys", false, "generate a new vapid key pair, which will be outputted to stdout.")
flag.Parse()
if *genKeys {
generateKeys()
return
}
err := godotenv.Load()
if err != nil {
@@ -201,6 +209,17 @@ func main() {
}
}
func generateKeys() {
priv, pub, err := webpush.GenerateVAPIDKeys()
if err != nil {
log.Fatal(err)
}
fmt.Println("all keys are base64 url encoded.")
fmt.Printf("public key: %v\n", pub)
fmt.Printf("private key: %v\n", priv)
}
func handleHTTPRequest(state *state) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
path := strings.TrimPrefix(request.URL.Path, "/")