More sync operations

This commit is contained in:
Magnus Åhall 2024-12-18 19:12:10 +01:00
parent 9df85d9580
commit d0150145ed
10 changed files with 362 additions and 131 deletions

46
main.go
View file

@ -13,6 +13,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
"path"
@ -24,6 +25,7 @@ import (
const VERSION = "v1"
const CONTEXT_USER = 1
const SYNC_PAGINATION = 250
var (
FlagGenerate bool
@ -122,7 +124,8 @@ func main() { // {{{
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/node/tree/{timestamp}/{offset}", authenticated(actionNodeTree))
http.HandleFunc("/sync/node/{sequence}/{offset}", authenticated(actionSyncNode))
http.HandleFunc("/node/retrieve/{uuid}", authenticated(actionNodeRetrieve))
http.HandleFunc("/service_worker.js", pageServiceWorker)
@ -201,7 +204,10 @@ func pageServiceWorker(w http.ResponseWriter, r *http.Request) { // {{{
return
}
err = tmpl.Execute(w, struct{ VERSION string; DevMode bool }{VERSION, FlagDev})
err = tmpl.Execute(w, struct {
VERSION string
DevMode bool
}{VERSION, FlagDev})
if err != nil {
w.Write([]byte(err.Error()))
return
@ -235,22 +241,40 @@ func pageSync(w http.ResponseWriter, r *http.Request) { // {{{
}
} // }}}
func actionNodeTree(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
changedFrom, _ := strconv.Atoi(r.PathValue("timestamp"))
offset, _ := strconv.Atoi(r.PathValue("offset"))
nodes, maxSeq, moreRowsExist, err := NodeTree(user.ID, offset, uint64(changedFrom))
func actionSyncNode(w http.ResponseWriter, r *http.Request) { // {{{
// The purpose of the Client UUID is to avoid
// sending nodes back once again to a client that
// just created or modified it.
request := struct {
ClientUUID string
}{}
body, _ := io.ReadAll(r.Body)
err := json.Unmarshal(body, &request)
if err != nil {
Log.Error("/node/tree", "error", err)
httpError(w, err)
return
}
user := getUser(r)
changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))
offset, _ := strconv.Atoi(r.PathValue("offset"))
nodes, maxSeq, moreRowsExist, err := Nodes(user.ID, offset, uint64(changedFrom), request.ClientUUID)
if err != nil {
Log.Error("/node/tree", "error", err)
httpError(w, err)
return
}
Log.Debug("/node/tree", "num_nodes", len(nodes), "maxSeq", maxSeq)
foo, _ := json.Marshal(nodes)
os.WriteFile(fmt.Sprintf("/tmp/nodes-%d.json", offset), foo, 0644)
j, _ := json.Marshal(struct {
OK bool
Nodes []TreeNode
MaxSeq uint64
OK bool
Nodes []Node
MaxSeq uint64
Continue bool
}{true, nodes, maxSeq, moreRowsExist})
w.Write(j)