Saving of nodes
This commit is contained in:
parent
c5bec0afa6
commit
08fd2cf4e9
16 changed files with 852 additions and 42 deletions
111
webserver.go
111
webserver.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
// Standard
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
|
@ -31,11 +32,27 @@ func initWebserver() (err error) {
|
|||
http.HandleFunc("/", pageIndex)
|
||||
http.HandleFunc("/app", pageApp)
|
||||
http.HandleFunc("/nodes/tree/{startNode}", actionNodesTree)
|
||||
http.HandleFunc("/nodes/{nodeID}", actionNode)
|
||||
http.HandleFunc("/nodes/update/{nodeID}", actionNodeUpdate)
|
||||
http.HandleFunc("/types/{typeID}", actionType)
|
||||
http.HandleFunc("/types/", actionTypesAll)
|
||||
|
||||
err = http.ListenAndServe(address, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func httpError(w http.ResponseWriter, err error) { // {{{
|
||||
out := struct {
|
||||
OK bool
|
||||
Error string
|
||||
}{
|
||||
false,
|
||||
err.Error(),
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
if r.URL.Path == "/" {
|
||||
http.Redirect(w, r, "/app", http.StatusSeeOther)
|
||||
|
|
@ -43,7 +60,6 @@ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
engine.StaticResource(w, r)
|
||||
}
|
||||
} // }}}
|
||||
|
||||
func pageApp(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
page := NewPage("app")
|
||||
err := engine.Render(page, w, r)
|
||||
|
|
@ -64,11 +80,96 @@ func actionNodesTree(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
maxDepth = 3
|
||||
}
|
||||
|
||||
topNode, err := GetNode(startNode, maxDepth)
|
||||
topNode, err := GetNodeTree(startNode, maxDepth)
|
||||
if err != nil {
|
||||
logger.Error("test", "error", err)
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(topNode)
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
Nodes *Node
|
||||
}{
|
||||
true,
|
||||
topNode,
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionNode(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
nodeID := 0
|
||||
nodeIDStr := r.PathValue("nodeID")
|
||||
nodeID, _ = strconv.Atoi(nodeIDStr)
|
||||
|
||||
node, err := GetNode(nodeID)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
Node Node
|
||||
}{
|
||||
true,
|
||||
node,
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionNodeUpdate(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
nodeID := 0
|
||||
nodeIDStr := r.PathValue("nodeID")
|
||||
nodeID, _ = strconv.Atoi(nodeIDStr)
|
||||
|
||||
data, _ := io.ReadAll(r.Body)
|
||||
|
||||
err := UpdateNode(nodeID, data)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
}{
|
||||
true,
|
||||
}
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionType(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
typeID := 0
|
||||
typeIDStr := r.PathValue("typeID")
|
||||
typeID, _ = strconv.Atoi(typeIDStr)
|
||||
|
||||
typ, err := GetType(typeID)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(typ)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func actionTypesAll(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
types, err := GetTypes()
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
out := struct {
|
||||
OK bool
|
||||
Types []NodeType
|
||||
}{
|
||||
true,
|
||||
types,
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(out)
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
|
||||
// vim: foldmethod=marker
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue