diff options
author | Michael Hunteman <michael@huntm.net> | 2024-07-06 15:24:05 -0700 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-07-06 15:24:43 -0700 |
commit | bacefa8db406aa188d27670649bb57f12a3b4c47 (patch) | |
tree | a6fb8996b940b6016d314d29fdd265ce6d74275e | |
parent | b9b8099da1a098300d11e0f479d56e24e363e076 (diff) |
Rename to guest
-rw-r--r-- | client/src/apiSlice.ts | 12 | ||||
-rw-r--r-- | client/src/components/Rsvp.tsx | 2 | ||||
-rw-r--r-- | client/src/main.tsx | 2 | ||||
-rw-r--r-- | client/src/mocks/handlers.ts | 4 | ||||
-rw-r--r-- | client/src/pages.ts | 2 | ||||
-rw-r--r-- | server/cmd/main.go | 51 | ||||
-rw-r--r-- | server/guest/models.go (renamed from server/guests/models.go) | 2 | ||||
-rw-r--r-- | server/guest/store.go (renamed from server/guests/store.go) | 2 | ||||
-rw-r--r-- | server/post.json | 11 | ||||
-rw-r--r-- | server/schema.sql | 2 |
10 files changed, 47 insertions, 43 deletions
diff --git a/client/src/apiSlice.ts b/client/src/apiSlice.ts index 514f8b1..038615f 100644 --- a/client/src/apiSlice.ts +++ b/client/src/apiSlice.ts @@ -39,23 +39,23 @@ export const apiSlice = createApi({ return headers; }, }), - tagTypes: ['Guests'], + tagTypes: ['Guest'], endpoints: (builder) => ({ getGuests: builder.query<void, void>({ - query: () => 'guests', - providesTags: ['Guests'], + query: () => 'guest', + providesTags: ['Guest'], }), updateGuest: builder.mutation<Guest, Guest>({ query: (guest) => ({ - url: `guests/${guest?.id}`, + url: `guest/${guest?.id}`, method: 'PUT', body: guest, - providesTags: ['Guests'], + providesTags: ['Guest'], }), }), login: builder.mutation<LoginResponse, LoginRequest>({ query: (credentials) => ({ - url: 'guests/login', + url: 'guest/login', method: 'POST', body: credentials, }), diff --git a/client/src/components/Rsvp.tsx b/client/src/components/Rsvp.tsx index fbcaf4a..d908f75 100644 --- a/client/src/components/Rsvp.tsx +++ b/client/src/components/Rsvp.tsx @@ -22,7 +22,7 @@ function Rsvp() { <Outlet context={auth?.guest} /> </> ) : ( - <Navigate to="/guests/login" state={{ from: location }} replace /> + <Navigate to="/guest/login" state={{ from: location }} replace /> ); } diff --git a/client/src/main.tsx b/client/src/main.tsx index 88999f7..45ff68b 100644 --- a/client/src/main.tsx +++ b/client/src/main.tsx @@ -31,7 +31,7 @@ const router = createBrowserRouter([ element: <Registry />, }, { - path: 'guests/login', + path: 'guest/login', element: <GuestLogin />, }, { diff --git a/client/src/mocks/handlers.ts b/client/src/mocks/handlers.ts index 0e882ee..8dc92b7 100644 --- a/client/src/mocks/handlers.ts +++ b/client/src/mocks/handlers.ts @@ -4,7 +4,7 @@ import { nanoid } from '@reduxjs/toolkit'; const token = nanoid(); export const handlers = [ - http.post('/guests/login', () => { + http.post('/guest/login', () => { return HttpResponse.json({ guest: { id: 1, @@ -17,7 +17,7 @@ export const handlers = [ token, }); }), - http.patch('/guests/1', () => { + http.patch('/guest/1', () => { return HttpResponse.json({ id: 1, firstName: 'Michael', diff --git a/client/src/pages.ts b/client/src/pages.ts index a1dbdfd..8bf5d23 100644 --- a/client/src/pages.ts +++ b/client/src/pages.ts @@ -1,7 +1,7 @@ const pages = [ { name: 'Home', to: '/' }, { name: 'Schedule', to: '/schedule' }, - { name: 'RSVP', to: '/guests/login' }, + { name: 'RSVP', to: '/guest/login' }, { name: 'Registry', to: '/registry' }, ]; diff --git a/server/cmd/main.go b/server/cmd/main.go index b4b1c6d..83a64af 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -8,12 +8,13 @@ import ( "net/http" "os" "regexp" + "slices" "time" "github.com/golang-jwt/jwt/v5" "github.com/jackc/pgx/v5/pgxpool" - "git.huntm.net/wedding/server/guests" + "git.huntm.net/wedding/server/guest" ) type guestHandler struct { @@ -21,10 +22,10 @@ type guestHandler struct { } type guestStore interface { - FindGuest(creds guests.Credentials) (guests.Guest, error) - Get() ([]guests.Guest, error) - Add(guest guests.Guest) error - Update(guest guests.Guest) error + FindGuest(creds guest.Credentials) (guest.Guest, error) + Get() ([]guest.Guest, error) + Add(guest guest.Guest) error + Update(guest guest.Guest) error } var ( @@ -34,8 +35,8 @@ var ( port = "5432" database = "postgres" - guestRe = regexp.MustCompile(`^/guests/*$`) - guestIdRe = regexp.MustCompile(`^/guests/([0-9]+)$`) + guestRe = regexp.MustCompile(`^/guest/*$`) + guestIdRe = regexp.MustCompile(`^/guest/([0-9]+)$`) ) func newGuestHandler(s guestStore) *guestHandler { @@ -45,7 +46,7 @@ func newGuestHandler(s guestStore) *guestHandler { } func (h *guestHandler) login(w http.ResponseWriter, r *http.Request) { - var creds guests.Credentials + var creds guest.Credentials err := json.NewDecoder(r.Body).Decode(&creds) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -53,14 +54,14 @@ func (h *guestHandler) login(w http.ResponseWriter, r *http.Request) { } defer r.Body.Close() - guest, err := h.store.FindGuest(creds) + weddingGuest, err := h.store.FindGuest(creds) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return } expirationTime := time.Now().Add(15 * time.Minute) - claims := &guests.Claims{ + claims := &guest.Claims{ Credentials: creds, RegisteredClaims: jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(expirationTime), @@ -80,8 +81,8 @@ func (h *guestHandler) login(w http.ResponseWriter, r *http.Request) { return } - loginResponse := &guests.LoginResponse{ - Guest: guest, + loginResponse := &guest.LoginResponse{ + Guest: weddingGuest, Token: tokenString, } @@ -113,7 +114,7 @@ func (h *guestHandler) getGuests(w http.ResponseWriter, _ *http.Request) { } func (h *guestHandler) createGuest(w http.ResponseWriter, r *http.Request) { - var guest guests.Guest + var guest guest.Guest err := json.NewDecoder(r.Body).Decode(&guest) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -145,7 +146,7 @@ func (h *guestHandler) createGuest(w http.ResponseWriter, r *http.Request) { func (h *guestHandler) updateGuest(w http.ResponseWriter, r *http.Request) { tokenString := r.Header.Get("Authorization") - claims := &guests.Claims{} + claims := &guest.Claims{} key, err := os.ReadFile("C:\\Users\\mhunt\\skey.pem") if err != nil { @@ -175,7 +176,7 @@ func (h *guestHandler) updateGuest(w http.ResponseWriter, r *http.Request) { return } - var guest guests.Guest + var guest guest.Guest err = json.NewDecoder(r.Body).Decode(&guest) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -194,7 +195,7 @@ func (h *guestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch { case r.Method == http.MethodOptions: w.WriteHeader(http.StatusOK) - case r.Method == http.MethodPost && r.URL.Path == "/guests/login": + case r.Method == http.MethodPost && r.URL.Path == "/guest/login": h.login(w, r) case r.Method == http.MethodGet && guestRe.MatchString(r.URL.Path): h.getGuests(w, r) @@ -208,11 +209,19 @@ func (h *guestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func enableCors(next http.Handler) http.Handler { + allowedOrigins := []string{"http://localhost:5173", "http://192.168.1.41:5173"} + allowedMethods := []string{"OPTIONS", "POST", "PUT"} return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if isPreflight(r) { - w.Header().Add("Access-Control-Allow-Methods", "*") + method := r.Header.Get("Method") + if isPreflight(r) && slices.Contains(allowedMethods, method) { + w.Header().Add("Access-Control-Allow-Methods", method) } - w.Header().Add("Access-Control-Allow-Origin", "*") + + origin := r.Header.Get("Origin") + if slices.Contains(allowedOrigins, origin) { + w.Header().Add("Access-Control-Allow-Origin", origin) + } + w.Header().Add("Access-Control-Allow-Headers", "*") next.ServeHTTP(w, r) }) @@ -231,10 +240,10 @@ func main() { } defer db.Close() - store := guests.NewMemStore(db) + store := guest.NewMemStore(db) guestHandler := newGuestHandler(store) mux := http.NewServeMux() - mux.Handle("/guests/", guestHandler) + mux.Handle("/guest/", guestHandler) log.Fatal(http.ListenAndServe(":8080", enableCors(mux))) } diff --git a/server/guests/models.go b/server/guest/models.go index bcc6a52..aa2c56f 100644 --- a/server/guests/models.go +++ b/server/guest/models.go @@ -1,4 +1,4 @@ -package guests +package guest import "github.com/golang-jwt/jwt/v5" diff --git a/server/guests/store.go b/server/guest/store.go index bedc646..525bb71 100644 --- a/server/guests/store.go +++ b/server/guest/store.go @@ -1,4 +1,4 @@ -package guests +package guest import ( "context" diff --git a/server/post.json b/server/post.json index 39e1d55..f12bd5a 100644 --- a/server/post.json +++ b/server/post.json @@ -4,12 +4,7 @@ "lastName": "Hunteman", "attendance": "yes", "email": "mhunteman@cox.net", - "message": "Hi", - "partySize": 2, - "partyList": [ - { - "firstName": "Madison", - "lastName": "Rossitto" - } - ] + "message": "", + "partySize": 1, + "partyList": [] } diff --git a/server/schema.sql b/server/schema.sql index b2dd533..fdc589a 100644 --- a/server/schema.sql +++ b/server/schema.sql @@ -9,7 +9,7 @@ create table guest ( ); create table party ( - guest_id integer NOT NULL references guests(id) ON DELETE CASCADE, + guest_id integer NOT NULL references guest(id) ON DELETE CASCADE, first_name varchar(64), last_name varchar(64) );
\ No newline at end of file |