summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-12-31 04:41:50 +0100
committerAki <please@ignore.pl>2023-12-31 04:44:47 +0100
commit08e9dc998ad1096c415c43c214731220c5ab34ff (patch)
treee8e9d692c118b6e6e43c681fe688e2d04e8748a6
parentef05c6943814d5851cd8a890ecb05cf715cd0bec (diff)
downloadrudone-08e9dc998ad1096c415c43c214731220c5ab34ff.zip
rudone-08e9dc998ad1096c415c43c214731220c5ab34ff.tar.gz
rudone-08e9dc998ad1096c415c43c214731220c5ab34ff.tar.bz2
Embed script and any pages into the tool
-rw-r--r--index.html5
-rw-r--r--main.go14
2 files changed, 11 insertions, 8 deletions
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..78dcc05
--- /dev/null
+++ b/index.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<html lang="en">
+<meta charset="utf-8">
+<title>rudone</title>
+<h1>rudone &mdash; <span style="color: green">OK</span></h1>
diff --git a/main.go b/main.go
index f2cb460..6d92d32 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "embed"
"encoding/json"
"log"
"net/http"
@@ -8,9 +9,9 @@ import (
"github.com/gorilla/mux"
)
-func handleHome(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte("stats OK"))
-}
+//go:embed *.js
+//go:embed *.html
+var content embed.FS
func handleEntryOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -23,28 +24,25 @@ func handleEntryPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
-
var entry Entry
-
err := json.NewDecoder(r.Body).Decode(&entry)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
-
err = InsertEntry(&entry)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
+ log.Println("New entry for", entry.Location)
}
func handleRequests() {
router := mux.NewRouter()
- router.HandleFunc("/", handleHome)
router.HandleFunc("/entry", handleEntryPost).Methods("POST")
router.HandleFunc("/entry", handleEntryOptions).Methods("OPTIONS")
-
+ router.PathPrefix("/").Handler(http.FileServer(http.FS(content)))
log.Fatal(http.ListenAndServe(":8080", router))
}