This commit is contained in:
Magnus Åhall 2024-12-01 10:21:29 +01:00
parent 5c2842c995
commit 1d6ba99a36
8 changed files with 945 additions and 83 deletions

41
main.go
View file

@ -17,6 +17,7 @@ import (
"net/http"
"path"
"regexp"
"strconv"
"strings"
"text/template"
)
@ -109,6 +110,7 @@ func main() { // {{{
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/node/tree", authenticated(actionNodeTree))
http.HandleFunc("/node/retrieve/{id}", authenticated(actionNodeRetrieve))
http.HandleFunc("/service_worker.js", pageServiceWorker)
@ -163,10 +165,13 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { // {{{
Webengine.StaticResource(w, r)
} // }}}
func httpError(w http.ResponseWriter, err error) {// {{{
j, _ := json.Marshal(struct { OK bool; Error string }{false, err.Error()})
func httpError(w http.ResponseWriter, err error) { // {{{
j, _ := json.Marshal(struct {
OK bool
Error string
}{false, err.Error()})
w.Write(j)
}// }}}
} // }}}
func pageServiceWorker(w http.ResponseWriter, r *http.Request) { // {{{
w.Header().Add("Content-Type", "text/javascript; charset=utf-8")
@ -183,18 +188,14 @@ func pageServiceWorker(w http.ResponseWriter, r *http.Request) { // {{{
return
}
err = tmpl.Execute(w, struct{ VERSION string }{VERSION})
err = tmpl.Execute(w, struct{ VERSION string; DevMode bool }{VERSION, FlagDev})
if err != nil {
w.Write([]byte(err.Error()))
return
}
} // }}}
func pageLogin(w http.ResponseWriter, r *http.Request) { // {{{
page := HTMLTemplate.SimplePage{
Layout: "main",
Page: "login",
Version: VERSION,
}
page := NewPage("login")
err := Webengine.Render(page, w, r)
if err != nil {
@ -222,12 +223,30 @@ func actionNodeTree(w http.ResponseWriter, r *http.Request) { // {{{
}
j, _ := json.Marshal(struct {
OK bool
Nodes []Node
OK bool
Nodes []Node
}{true, nodes})
Log.Debug("tree", "nodes", nodes)
w.Write(j)
} // }}}
func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
var err error
idStr := r.PathValue("id")
id, _ := strconv.Atoi(idStr)
node, err := RetrieveNode(user.ID, id)
if err != nil {
responseError(w, err)
return
}
responseData(w, map[string]interface{}{
"OK": true,
"Node": node,
})
} // }}}
func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin)