diff options
author | Michael Hunteman <michael@huntm.net> | 2024-08-25 12:44:32 -0700 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-08-25 12:44:32 -0700 |
commit | 096a08708e2310becba56a237ef63b5cf6e3c4c4 (patch) | |
tree | 2924f9aecdcf035599558552cfdb20c2cc18f7d1 /server/cmd | |
parent | 6aee47e76d7e25206b3778aeebcc341d7b705035 (diff) |
Add admin dashboard
Diffstat (limited to 'server/cmd')
-rw-r--r-- | server/cmd/main.go | 58 |
1 files changed, 35 insertions, 23 deletions
diff --git a/server/cmd/main.go b/server/cmd/main.go index 439ed0b..38ccf37 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -10,53 +10,65 @@ import ( "github.com/jackc/pgx/v5/pgxpool" + "git.huntm.net/wedding/server/admin" "git.huntm.net/wedding/server/guest" ) var ( user = os.Getenv("USER") - pass = os.Getenv("PASS") + password = os.Getenv("PASS") host = "localhost" port = "5432" database = "postgres" ) func main() { - db, err := pgxpool.New(context.Background(), fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, database)) + databasePool, err := pgxpool.New(context.Background(), + fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, password, host, port, database)) if err != nil { log.Fatal(err) } - defer db.Close() + defer databasePool.Close() - store := guest.NewMemStore(db) - guestHandler := guest.NewGuestHandler(store) + guestStore := guest.NewStore(databasePool) + guestHandler := guest.NewGuestHandler(guestStore) + adminStore := admin.NewStore(databasePool) + adminHandler := admin.NewAdminHandler(adminStore, guestStore) mux := http.NewServeMux() mux.Handle("/guest/", guestHandler) - log.Fatal(http.ListenAndServe(":8080", enableCORS(mux))) + mux.Handle("/admin/", adminHandler) + log.Fatal(http.ListenAndServe(":8080", serveHTTP(mux))) } -func enableCORS(handler http.Handler) http.Handler { - allowedOrigins := []string{"http://localhost:5173", "http://192.168.1.41:5173"} - allowedMethods := []string{"OPTIONS", "POST", "PUT"} - return serveHTTP(handler, allowedOrigins, allowedMethods) +func serveHTTP(handler http.Handler) http.Handler { + return http.HandlerFunc(func(responseWriter http.ResponseWriter, + request *http.Request) { + writeMethods(responseWriter, request) + writeOrigins(responseWriter, request) + writeHeaders(responseWriter) + handler.ServeHTTP(responseWriter, request) + }) } -func serveHTTP(handler http.Handler, allowedOrigins []string, allowedMethods []string) http.Handler { - return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) { - method := request.Header.Get("Access-Control-Request-Method") - if isPreflight(request) && slices.Contains(allowedMethods, method) { - responseWriter.Header().Add("Access-Control-Allow-Methods", method) - } +func writeMethods(responseWriter http.ResponseWriter, request *http.Request) { + allowedMethods := []string{"OPTIONS", "POST", "PUT"} + method := request.Header.Get("Access-Control-Request-Method") + if isPreflight(request) && slices.Contains(allowedMethods, method) { + responseWriter.Header().Add("Access-Control-Allow-Methods", method) + } +} - origin := request.Header.Get("Origin") - if slices.Contains(allowedOrigins, origin) { - responseWriter.Header().Add("Access-Control-Allow-Origin", origin) - } +func writeOrigins(responseWriter http.ResponseWriter, request *http.Request) { + allowedOrigins := []string{"http://localhost:5173", "http://192.168.1.18:5173"} + origin := request.Header.Get("Origin") + if slices.Contains(allowedOrigins, origin) { + responseWriter.Header().Add("Access-Control-Allow-Origin", origin) + } +} - responseWriter.Header().Add("Access-Control-Allow-Headers", "*") - handler.ServeHTTP(responseWriter, request) - }) +func writeHeaders(responseWriter http.ResponseWriter) { + responseWriter.Header().Add("Access-Control-Allow-Headers", "*") } func isPreflight(request *http.Request) bool { |