diff options
Diffstat (limited to 'server/storage.go')
-rw-r--r-- | server/storage.go | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/server/storage.go b/server/storage.go index 21950fa..e845dd5 100644 --- a/server/storage.go +++ b/server/storage.go @@ -4,6 +4,7 @@ import ( "crypto/ed25519" "fmt" "io" + "log" "net/http" "os" "strconv" @@ -13,6 +14,7 @@ import ( // Replicate a user's log to another server func replicate(id, s string) { + log.Printf("Starting replication for %s %s", id, s) for true { mu.Lock() // Make sure that this server is still the primary for this user @@ -42,7 +44,7 @@ func replicate(id, s string) { op := user.log[idx] mu.Unlock() file, _ := os.Open(op) - resp, err := http.Post(s + "/storage/" + id + "/" + op + "?idx=" + fmt.Sprint(idx), "application/octet-stream", file) + resp, err := http.Post(s+"/storage/"+id+"/"+op+"?idx="+fmt.Sprint(idx), "application/octet-stream", file) if err != nil { time.Sleep(50 * time.Millisecond) continue @@ -59,18 +61,18 @@ func replicate(id, s string) { } } - // Handle storage requests func storageHandler(w http.ResponseWriter, r *http.Request) { pathSplit := strings.Split(r.URL.Path, "/") - id := pathSplit[1] - filename := pathSplit[2] + id := pathSplit[2] + filename := pathSplit[3] r.ParseForm() if r.Method == "GET" { if r.Form.Has("direct") { // Directly read and respond with file file, err := os.ReadFile(dataDir + "/" + id + "/" + filename) if err != nil { + log.Print(err) w.WriteHeader(http.StatusNotFound) return } @@ -78,7 +80,9 @@ func storageHandler(w http.ResponseWriter, r *http.Request) { return } val := dhtGet(id, false) - if verify(id, val) != nil { + err := verify(id, val) + if err != nil { + verify(id, val) w.WriteHeader(http.StatusNotFound) return } @@ -89,6 +93,7 @@ func storageHandler(w http.ResponseWriter, r *http.Request) { if servers[0] == me { file, err := os.ReadFile(dataDir + "/" + id + "/" + filename) if err != nil { + log.Print(err) w.WriteHeader(http.StatusNotFound) return } @@ -119,10 +124,13 @@ func storageHandler(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(r.Body) if err != nil { + log.Print(err) w.WriteHeader(http.StatusInternalServerError) return } - if verify(id, b) != nil { + err = verify(id, b) + if err != nil { + log.Print(err) w.WriteHeader(http.StatusUnauthorized) return } @@ -149,16 +157,19 @@ func storageHandler(w http.ResponseWriter, r *http.Request) { // Fetch older version of file resp, err := http.Get(user.servers[0] + "/storage/" + id + "/" + op) if err != nil { + log.Print(err) w.WriteHeader(http.StatusInternalServerError) return } b, err := io.ReadAll(resp.Body) if err != nil { + log.Print(err) w.WriteHeader(http.StatusInternalServerError) return } - err = os.WriteFile(dataDir + "/" + id + "/" + op, b, 0644) + err = os.WriteFile(dataDir+"/"+id+"/"+op, b, 0644) if err != nil { + log.Print(err) w.WriteHeader(http.StatusInternalServerError) return } @@ -166,7 +177,7 @@ func storageHandler(w http.ResponseWriter, r *http.Request) { } } - err = os.WriteFile(dataDir + "/" + id + "/" + filename, b, 0644) + err = os.WriteFile(dataDir+"/"+id+"/"+filename, b, 0644) if err != nil { w.WriteHeader(http.StatusInternalServerError) return |