Changing server and client so that the server hosts the client files
This commit is contained in:
parent
459d168716
commit
78e24f2757
5 changed files with 62 additions and 37 deletions
|
|
@ -1,7 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
<link rel="stylesheet" type="text/css" href="/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
<a href="/rsvp.html">
|
||||
<a href="/rsvp">
|
||||
<div class="rsvpButton textCenter backgroundLightGreen hidden">
|
||||
<p class="colorBlack">
|
||||
R <br/>
|
||||
|
|
@ -45,6 +45,6 @@
|
|||
</p>
|
||||
</div>
|
||||
</a>
|
||||
<script src="rsvp.js"></script>
|
||||
<script src="/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
28
client/index.js
Normal file
28
client/index.js
Normal 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;
|
||||
}
|
||||
};
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
<link rel="stylesheet" type="text/css" href="/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1 class="title fontXL textCenter backgroundLightGreen colorWhite">Collin and Lucy's Wedding</h1>
|
||||
<div class="contents fontM">
|
||||
<form action="/rsvp" method="post">
|
||||
<form action="/api/rsvp" method="post">
|
||||
<p>
|
||||
<label for="name-0">Name:</label>
|
||||
<input type="text" id="name-0" name="name"/>
|
||||
|
|
@ -24,6 +24,5 @@
|
|||
</p>
|
||||
</form>
|
||||
</div>
|
||||
<script src="rsvp.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue