More sync

This commit is contained in:
Magnus Åhall 2025-01-12 16:06:28 +01:00
parent 25179ffd15
commit 1c3116d9dc
7 changed files with 208 additions and 6 deletions

27
main.go
View file

@ -124,8 +124,8 @@ func main() { // {{{
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncNode))
http.HandleFunc("/sync/to_server/{client}", authenticated(actionSyncNode))
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
http.HandleFunc("/sync/to_server", authenticated(actionSyncToServer))
http.HandleFunc("/node/retrieve/{uuid}", authenticated(actionNodeRetrieve))
@ -242,7 +242,7 @@ func pageSync(w http.ResponseWriter, r *http.Request) { // {{{
}
} // }}}
func actionSyncNode(w http.ResponseWriter, r *http.Request) { // {{{
func actionSyncFromServer(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.
@ -296,6 +296,27 @@ func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
"Node": node,
})
} // }}}
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
body, _ := r.GetBody()
data, _ := io.ReadAll(body)
var request struct {
ClientUUID string
NodeData string
}
err := json.Unmarshal(data, &request)
if err != nil {
httpError(w, err)
return
}
db.Exec(`CALL add_nodes(%d, %s::jsonb)`, user.ID, request.ClientUUID, request.NodeData)
responseData(w, map[string]interface{}{
"OK": true,
})
} // }}}
func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin)