diff options
| author | Paul Buetow <paul@buetow.org> | 2024-05-05 15:50:06 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-05-05 15:50:06 +0300 |
| commit | bc3850054cd182fd84e180482e88935aa3559a1c (patch) | |
| tree | 4073c0579c4115870109801371b2b5e1a40c68b1 | |
| parent | f173d052d3f8679716862d233d8751f67d3b197c (diff) | |
use sha checksum as the id
| -rw-r--r-- | entry.go | 7 | ||||
| -rw-r--r-- | submit.go | 7 |
2 files changed, 7 insertions, 7 deletions
@@ -1,6 +1,7 @@ package main import ( + "crypto/sha256" "encoding/json" "fmt" ) @@ -14,13 +15,15 @@ type entry struct { Body string `json:"body"` Shared []shared `json:"shared,omitempty"` Epoch int `json:"epoch,omitempty"` + id string } -func newEntry(data []byte) (entry, error) { +func newEntry(bytes []byte) (entry, error) { var entry entry - if err := json.Unmarshal(data, &entry); err != nil { + if err := json.Unmarshal(bytes, &entry); err != nil { return entry, fmt.Errorf("unable to deserialise payload: %w", err) } + entry.id = fmt.Sprintf("%x", sha256.Sum256(bytes)) return entry, nil } @@ -1,7 +1,6 @@ package main import ( - "crypto/sha256" "fmt" "io" "net/http" @@ -10,7 +9,7 @@ import ( func handleSubmit(w http.ResponseWriter, r *http.Request, dataDir string) error { if r.Method != "POST" { - return fmt.Errorf("Expexted POST request") + return fmt.Errorf("expexted POST request") } bytes, err := io.ReadAll(r.Body) @@ -22,9 +21,7 @@ func handleSubmit(w http.ResponseWriter, r *http.Request, dataDir string) error if err != nil { return err } - - filePath := fmt.Sprintf("%s/%s/%x.json", dataDir, - time.Now().Format("2006"), sha256.Sum256(bytes)) + filePath := fmt.Sprintf("%s/%s/%s.json", dataDir, time.Now().Format("2006"), entry.id) jsonStr, err := entry.serialize() if err != nil { |
