add cors; add username to signup interface

This commit is contained in:
2024-09-09 12:28:26 -05:00
parent 0e8a6fd814
commit 8ba7ae206f
7 changed files with 83 additions and 51 deletions

22
main.go
View File

@@ -20,7 +20,7 @@ func main() {
AddDefaultCustomer(nkodeApi)
handler := model.NKodeHandler{Api: &nkodeApi}
mux := http.NewServeMux()
//mux.Handle(api.CreateNewCustomer, &handler)
mux.Handle(api.CreateNewCustomer, &handler)
mux.Handle(api.GenerateSignupInterface, &handler)
mux.Handle(api.SetNKode, &handler)
mux.Handle(api.ConfirmNKode, &handler)
@@ -28,7 +28,25 @@ func main() {
mux.Handle(api.Login, &handler)
mux.Handle(api.RenewAttributes, &handler)
fmt.Println("Running on localhost:8080...")
log.Fatal(http.ListenAndServe("localhost:8080", mux))
log.Fatal(http.ListenAndServe("localhost:8080", corsMiddleware(mux)))
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set the CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Handle preflight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
// Call the next handler
next.ServeHTTP(w, r)
})
}
func AddDefaultCustomer(api nkode.NKodeAPI) {