Changing server and client so that the server hosts the client files

This commit is contained in:
collin 2025-06-30 12:37:33 +02:00
parent ce3f944274
commit 8f1fb324a0
No known key found for this signature in database
5 changed files with 62 additions and 37 deletions

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="/style.css">
</head> </head>
<body> <body>
<h1 class="title fontXL textCenter backgroundLightGreen colorWhite">Collin and Lucy's Wedding</h1> <h1 class="title fontXL textCenter backgroundLightGreen colorWhite">Collin and Lucy's Wedding</h1>
@ -35,7 +35,7 @@
<a href="https://www.openstreetmap.org/node/2869212621"><h5 class="fontS">Open Map</h5></a> <a href="https://www.openstreetmap.org/node/2869212621"><h5 class="fontS">Open Map</h5></a>
</div> </div>
</div> </div>
<a href="/rsvp.html"> <a href="/rsvp">
<div class="rsvpButton textCenter backgroundLightGreen hidden"> <div class="rsvpButton textCenter backgroundLightGreen hidden">
<p class="colorBlack"> <p class="colorBlack">
R <br/> R <br/>
@ -45,6 +45,6 @@
</p> </p>
</div> </div>
</a> </a>
<script src="rsvp.js"></script> <script src="/index.js"></script>
</body> </body>
</html> </html>

28
client/index.js Normal file
View file

@ -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;
}
};

View file

@ -1,12 +1,12 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="/style.css">
</head> </head>
<body> <body>
<h1 class="title fontXL textCenter backgroundLightGreen colorWhite">Collin and Lucy's Wedding</h1> <h1 class="title fontXL textCenter backgroundLightGreen colorWhite">Collin and Lucy's Wedding</h1>
<div class="contents fontM"> <div class="contents fontM">
<form action="/rsvp" method="post"> <form action="/api/rsvp" method="post">
<p> <p>
<label for="name-0">Name:</label> <label for="name-0">Name:</label>
<input type="text" id="name-0" name="name"/> <input type="text" id="name-0" name="name"/>
@ -24,6 +24,5 @@
</p> </p>
</form> </form>
</div> </div>
<script src="rsvp.js"></script>
</body> </body>
</html> </html>

View file

@ -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
}
}

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os"
"time" "time"
) )
@ -13,11 +14,37 @@ type Handler struct {
ntfy *Ntfy 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) { 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) fmt.Printf("%s - [%s] (%s) %s\n", time.Now().Format(time.RFC3339), r.RemoteAddr, r.Method, r.URL)
switch true { switch true {
case r.Method == "GET" && r.URL.Path == "/": 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) rsvps, err := GetRsvps(h.db)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -27,7 +54,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%#v", rsvps) fmt.Fprintf(w, "%#v", rsvps)
case r.Method == "POST" && r.URL.Path == "/":
case r.Method == "POST" && r.URL.Path == "/api/rsvps":
var rsvp Rsvp var rsvp Rsvp
err := json.NewDecoder(r.Body).Decode(&rsvp) err := json.NewDecoder(r.Body).Decode(&rsvp)