Make frontend partially functional

This commit is contained in:
Andrew-71 2024-03-17 16:44:05 +03:00
parent 1923ad0bc8
commit 7ddaacc415
5 changed files with 73 additions and 7 deletions

3
.gitignore vendored
View file

@ -21,3 +21,6 @@ go.work
hibiscus hibiscus
.idea/ .idea/
# Because currently it's used in "prod"
data/

4
config/config.txt Normal file
View file

@ -0,0 +1,4 @@
port=7101
username=test
password=pass
export_code=hibiscus

View file

@ -12,7 +12,7 @@ import (
) )
func GetFile(filename string, w http.ResponseWriter, r *http.Request) { func GetFile(filename string, w http.ResponseWriter, r *http.Request) {
path := "data/" + filepath.Base(filename) + ".txt" // This should *theoretically* sanitize the string path := "data/" + filename + ".txt" // Can we and should we sanitize this?
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
NotFound(w, r) NotFound(w, r)

View file

@ -16,11 +16,11 @@
<main class="container"> <main class="container">
<h2 style="margin-bottom:0;"><label for="day">Your day so far:</label></h2> <h2 style="margin-bottom:0;"><label for="day">Your day so far:</label></h2>
<textarea id="day" name="Day entry" cols="40" rows="10" style="max-width: 640px; width: 100%"></textarea> <textarea id="day" name="Day entry" cols="40" rows="10" style="max-width: 640px; width: 100%"></textarea>
<div class="grid"><button onclick="console.log('Test save')">Save</button></div> <div class="grid"><button onclick="saveToday()">Save</button></div>
<h2><label for="log">Log</label></h2> <h2><label for="log">Log</label></h2>
<input type="text" id="log" name="log" style="max-width: 640px; width: 100%"/> <input type="text" id="log" name="log" style="max-width: 640px; width: 100%"/>
<div class="grid"><button onclick="console.log('Test log save')">Save</button></div> <div class="grid"><button onclick="saveLog()">Save</button></div>
</main> </main>
<footer class="container"> <footer class="container">
<p><a href="/page/sitemap" class="no-accent"></a> v0.0.1 | PoC using <a href="https://picocss.com">PicoCSS</a></p> <p><a href="/page/sitemap" class="no-accent"></a> v0.0.1 | PoC using <a href="https://picocss.com">PicoCSS</a></p>
@ -34,7 +34,67 @@
timeField.innerText = todayDate.toISOString().split('T')[0] timeField.innerText = todayDate.toISOString().split('T')[0]
} }
updateTime() updateTime()
async function postData(url = "", data = "") {
const response = await fetch(url, {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "text/plain",
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: data,
});
if (response.ok) {
return response.text();
} else {
return response.status
}
}
async function getData(url = "", data = "") {
const response = await fetch(url, {
method: "GET",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer"
});
if (response.ok) {
return response.text();
} else {
console.log(response.text())
return response.status
// return "Error"
}
}
function saveLog() {
let logField = document.getElementById("log")
postData("/api/log", logField.value).then((data) => {
if (data !== 500) {
logField.value = ""
}
});
}
function saveToday() {
let logField = document.getElementById("day")
postData("/api/today", logField.value).then((data) => {
console.log(data);
});
}
function loadToday() {
let dayField = document.getElementById("day")
getData("/api/today", dayField.value).then((data) => {
if (data !== 404) {
dayField.value = data
} else {
dayField.value = ""
}
});
}
loadToday()
</script> </script>
</html> </html>

View file

@ -14,6 +14,7 @@ func Serve() {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes) r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes)
r.Use(basicAuth) // TODO: ..duh! r.Use(basicAuth) // TODO: ..duh!
r.NotFound(NotFound)
// Home page // Home page
r.Get("/", func(w http.ResponseWriter, r *http.Request) { r.Get("/", func(w http.ResponseWriter, r *http.Request) {
@ -39,8 +40,6 @@ func Serve() {
r.Mount("/api", apiRouter) r.Mount("/api", apiRouter)
r.NotFound(NotFound)
// Static files // Static files
fs := http.FileServer(http.Dir("public")) fs := http.FileServer(http.Dir("public"))
r.Handle("/public/*", http.StripPrefix("/public/", fs)) r.Handle("/public/*", http.StripPrefix("/public/", fs))