From da3f35196055a5a405131523fbfb98c05fe511bc Mon Sep 17 00:00:00 2001 From: Xe Date: Mon, 17 Jan 2022 15:46:51 +0000 Subject: [PATCH] start frontend Signed-off-by: Xe --- server.go | 12 ++++++++++++ static/index.html | 21 ++++++++++++++++++++ static/js/main.js | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 static/index.html diff --git a/server.go b/server.go index c16bdcb..9c5ad93 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "database/sql" "embed" "encoding/json" + "io" "net/http" "time" @@ -47,6 +48,17 @@ func NewServer(db *sql.DB, srv *tsnet.Server) *Server { srv: srv, } + mux.HandleFunc("/schema.sql", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fin, err := schemaFS.Open("schema/schema.sql") + if err != nil { + http.Error(w, "not found??", http.StatusNotFound) + return + } + defer fin.Close() + io.Copy(w, fin) + }) + mux.HandleFunc("/api/whois", s.whois) mux.HandleFunc("/api/importcsv", s.importCSV) mux.HandleFunc("/api/queries/get", s.getQueries) diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..7a98609 --- /dev/null +++ b/static/index.html @@ -0,0 +1,21 @@ + + + + + Twitchalitics + + +
+

Twitchalitics

+ +

+ +
+ Loading... +
+ +
+ Import CSV tool +
+ + diff --git a/static/js/main.js b/static/js/main.js index 38951a6..86cc5ef 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,4 +1,50 @@ -import { h, g, x, r } from "./xeact.js"; -import { div, span, h1 } from "./xeact-html.js"; +import { h, g, x, r, t, u } from "./xeact.js"; +import { div, span, h2, ahref, br } from "./xeact-html.js"; +let li = (b) => h("li", {}, b); +let ul = (b) => h("ul", {}, b); +let code = (b) => h("code", {}, b); +let getWhois = async() => { + let resp = await fetch(u("/api/whois")); + resp = await resp.json(); + return resp; +}; + +let getQueries = async() => { + let resp = await fetch(u("/api/queries/get")); + resp = await resp.json(); + resp = resp.map((q) => li(span({}, [t(q.name), t(" "), code(q.query), t(" "), t(`added by ${q.who}`)]))); + return resp; +}; + +let queryForm = () => { + return div({}, [ + h2("SQL Query"), + ahref("/schema.sql", "Schema"), + h("form", {action: "", autocorrect: "off", autocapitalize: "none", style: "display:flex"}, [ + h("input", {type: "search", style: "width:100%"}), + h("button", {type: "submit"}, t("👀")) + ]), + ]); +}; + +let mainUI = async () => { + let root = g("appRoot"); + x(root); + + let whois = await getWhois(); + let queries = await getQueries(); + + let qf = queryForm(); + + root.appendChild(div({}, [ + t(`Hello, ${whois.UserProfile.DisplayName}.`), + qf, + h2("Results"), + h2("Fun Queries to Try"), + div({}, queries), + ])); +}; + +mainUI();