From 78e24f275725f621a8a9baaa36d4fdc9380f03b8 Mon Sep 17 00:00:00 2001 From: Collin Duncan Date: Thu, 26 Jun 2025 13:05:59 +0200 Subject: [PATCH] Changing server and client so that the server hosts the client files --- client/index.html | 6 +++--- client/index.js | 28 ++++++++++++++++++++++++++++ client/rsvp.html | 5 ++--- client/rsvp.js | 30 ------------------------------ server/controller.go | 30 +++++++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 client/index.js delete mode 100644 client/rsvp.js diff --git a/client/index.html b/client/index.html index 4b5cfa0..c31b660 100644 --- a/client/index.html +++ b/client/index.html @@ -1,7 +1,7 @@ - +

Collin and Lucy's Wedding

@@ -35,7 +35,7 @@
Open Map
- + - + \ No newline at end of file diff --git a/client/index.js b/client/index.js new file mode 100644 index 0000000..93a92ab --- /dev/null +++ b/client/index.js @@ -0,0 +1,28 @@ +const COOKIE_NAME = "hasRsvped"; + +function hasRsvped() { + const cookies = Object.fromEntries( + document.cookie.split(";").map((x) => x.trim().split("=")) + ); + + return cookies[COOKIE_NAME] === "true"; +} + +function persistRsvp(hasRsvped = true) { + document.cookie = `${COOKIE_NAME}=${String(hasRsvped)}; path=/`; + console.log(document.cookie); +} + +function rsvpButtonClicked() { + console.log("here"); + persistRsvp(true); +} + +window.onload = function () { + if (!hasRsvped()) { + const rsvpButton = document.querySelector(".rsvpButton"); + rsvpButton.classList.remove("hidden"); + + rsvpButton.onclick = rsvpButtonClicked; + } +}; diff --git a/client/rsvp.html b/client/rsvp.html index 0128ca8..44ee632 100644 --- a/client/rsvp.html +++ b/client/rsvp.html @@ -1,12 +1,12 @@ - +

Collin and Lucy's Wedding

-
+

@@ -24,6 +24,5 @@

- \ No newline at end of file diff --git a/client/rsvp.js b/client/rsvp.js deleted file mode 100644 index af206d9..0000000 --- a/client/rsvp.js +++ /dev/null @@ -1,30 +0,0 @@ -const COOKIE_NAME = 'hasRsvped' - -function hasRsvped() { - const cookies = Object.fromEntries( - document.cookie - .split(';') - .map(x => x.trim().split('=')) - ) - - return cookies[COOKIE_NAME] === 'true' -} - -function persistRsvp(hasRsvped = true) { - document.cookie = `${COOKIE_NAME}=${String(hasRsvped)}; path=/` - console.log(document.cookie); -} - -function rsvpButtonClicked() { - console.log('here') - persistRsvp(true) -} - -window.onload = function () { - if (!hasRsvped()) { - const rsvpButton = document.querySelector('.rsvpButton') - rsvpButton.classList.remove('hidden') - - rsvpButton.onclick = rsvpButtonClicked - } -} \ No newline at end of file diff --git a/server/controller.go b/server/controller.go index af277da..baf807f 100644 --- a/server/controller.go +++ b/server/controller.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "os" "time" ) @@ -13,11 +14,37 @@ type Handler struct { ntfy *Ntfy } +func getStaticFile(relPath string, contentType string, w http.ResponseWriter) { + file, err := os.ReadFile(relPath) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "%s", err.Error()) + return + } + + w.Header().Add("Content-Type", contentType) + w.WriteHeader(http.StatusOK) + fmt.Printf("%s\n", w.Header().Get("Content-Type")) + w.Write(file) +} + func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s - [%s] (%s) %s\n", time.Now().Format(time.RFC3339), r.RemoteAddr, r.Method, r.URL) switch true { case r.Method == "GET" && r.URL.Path == "/": + getStaticFile("../client/index.html", "text/html", w) + + case r.Method == "GET" && r.URL.Path == "/rsvp": + getStaticFile("../client/rsvp.html", "text/html", w) + + case r.Method == "GET" && r.URL.Path == "/index.js": + getStaticFile("../client/index.js", "text/javascript", w) + + case r.Method == "GET" && r.URL.Path == "/style.css": + getStaticFile("../client/style.css", "text/css", w) + + case r.Method == "GET" && r.URL.Path == "/api/rsvps": rsvps, err := GetRsvps(h.db) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -27,7 +54,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "%#v", rsvps) - case r.Method == "POST" && r.URL.Path == "/": + + case r.Method == "POST" && r.URL.Path == "/api/rsvps": var rsvp Rsvp err := json.NewDecoder(r.Body).Decode(&rsvp)