63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"go-nkode/core"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
//db := nkode.NewInMemoryDb()
|
|
db := core.NewSqliteDB("nkode.db")
|
|
defer db.CloseDb()
|
|
nkodeApi := core.NewNKodeAPI(db)
|
|
AddDefaultCustomer(nkodeApi)
|
|
handler := core.NKodeHandler{Api: nkodeApi}
|
|
mux := http.NewServeMux()
|
|
mux.Handle(core.CreateNewCustomer, &handler)
|
|
mux.Handle(core.GenerateSignupInterface, &handler)
|
|
mux.Handle(core.SetNKode, &handler)
|
|
mux.Handle(core.ConfirmNKode, &handler)
|
|
mux.Handle(core.GetLoginInterface, &handler)
|
|
mux.Handle(core.Login, &handler)
|
|
mux.Handle(core.RenewAttributes, &handler)
|
|
mux.Handle(core.RandomSvgInterface, &handler)
|
|
fmt.Println("Running on localhost:8080...")
|
|
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 core.NKodeAPI) {
|
|
newId, err := uuid.Parse("ed9ed6e0-082c-4b57-8d8c-f00ed6493457")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
customerId := core.CustomerId(newId)
|
|
nkode_policy := core.NewDefaultNKodePolicy()
|
|
_, err = api.CreateNewCustomer(nkode_policy, &customerId)
|
|
if err != nil {
|
|
log.Print(err)
|
|
} else {
|
|
log.Print("created new customer: ", newId)
|
|
}
|
|
}
|