From 891f089d31da483f6f887715e779cdab58561966c704c5345ebc21d42e503ce9 Mon Sep 17 00:00:00 2001 From: Alexandra Knopf Date: Sun, 9 Aug 2026 05:30:55 -0700 Subject: [PATCH] more db stuff --- go.mod | 1 + go.sum | 2 ++ main.go | 22 ++++++++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index cb6891a..ce98026 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/go-webauthn/x v0.1.9 // indirect github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/google/go-tpm v0.9.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.21.0 // indirect diff --git a/go.sum b/go.sum index 1796c86..fbb104f 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/google/go-tpm v0.9.0 h1:sQF6YqWMi+SCXpsmS3fd21oPy/vSddwZry4JnmltHVk= github.com/google/go-tpm v0.9.0/go.mod h1:FkNVkc6C+IsvDI9Jw1OveJmxGZUUaKxtrpOS47QWKfU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/main.go b/main.go index 15b3269..625ee88 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "strings" "time" "github.com/go-webauthn/webauthn/webauthn" + "github.com/joho/godotenv" ) var ( @@ -42,10 +43,15 @@ type PasskeyStore interface { func main() { l = log.Default() + godotenv.Load() + proto := getEnv("PROTO", "http") host := getEnv("HOST", "localhost") - port := getEnv("PORT", ":8080") + port := ":" + getEnv("PORT", "8080") origin := fmt.Sprintf("%s://%s%s", proto, host, port) + dbUser := getEnv("DB_USER") + dbPassword := getEnv("DB_PASSWORD") + dbName := getEnv("DB_NAME") l.Printf("[INFO] make webauthn config") wconfig := &webauthn.Config{ @@ -56,10 +62,19 @@ func main() { l.Printf("[INFO] create webauthn") if webAuthn, err = webauthn.New(wconfig); err != nil { - fmt.Printf("[FATA] %s", err.Error()) + fmt.Printf("[FATAL] %s", err.Error()) os.Exit(1) } + db, err := sql.Open("mysql", dbUser + ":" + dbPassword + "@/") + if err != nil { + panic(err) + } + // See "Important settings" section. + db.SetConnMaxLifetime(time.Minute * 3) + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(10) + l.Printf("[INFO] create datastore") datastore = NewInMem(l) @@ -67,7 +82,7 @@ func main() { // Serve the web files http.Handle("/", http.FileServer(http.Dir("./web"))) - // Add auth the routes + // Add auth routes http.HandleFunc("/api/passkey/registerStart", BeginRegistration) http.HandleFunc("/api/passkey/registerFinish", FinishRegistration) http.HandleFunc("/api/passkey/loginStart", BeginLogin) @@ -154,7 +169,6 @@ func FinishRegistration(w http.ResponseWriter, r *http.Request) { }) l.Printf(msg) JSONResponse(w, msg, http.StatusBadRequest) - return }