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

View File

@@ -2,7 +2,8 @@
<configuration default="false" name="Generate VAPID" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="7am" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<parameters value="--generate-vapid-keys" />
<kind value="PACKAGE" />
<package value="code.nym.sh/7am" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/generate_vapid.go" />

View File

@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="go build code.nym.sh/7am" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<configuration default="false" name="Start server on 8080" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="7am" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />

View File

@@ -1,30 +0,0 @@
package main
import (
"github.com/SherClockHolmes/webpush-go"
"log"
"os"
)
func main() {
pub, priv, err := webpush.GenerateVAPIDKeys()
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("vapid_public_key", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
f.Write([]byte(pub))
f, err = os.OpenFile("vapid_private_key", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
f.Write([]byte(priv))
}

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, "/")