Compare commits

..

No commits in common. "5ddeb5bb5ab66182ac1baad1bd78c1f8fb4a410a" and "42fb906dad9076d3ae6e55bce601c8b6bd29a0b2" have entirely different histories.

19 changed files with 147 additions and 502 deletions

View File

@ -1,6 +1,3 @@
sync-run:
cd sync && PLANNER_DB_PATH=test.db PLANNER_PORT=8092 PLANNER_API_KEY=testKey go run .
sync-build-and-push:
cd sync && docker build . -t codeberg.org/ewintr/syncservice && docker push codeberg.org/ewintr/syncservice
run:
PLANNER_PORT=8092 PLANNER_API_KEY=testKey go run ./sync-service/

View File

@ -1,87 +0,0 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
baseURL string
apiKey string
c *http.Client
}
func NewClient(url, apiKey string) *Client {
return &Client{
baseURL: url,
apiKey: apiKey,
c: &http.Client{
Timeout: 10 * time.Second,
},
}
}
func (c *Client) Update(items []Item) error {
body, err := json.Marshal(items)
if err != nil {
return fmt.Errorf("could not marhal body: %v", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/sync", c.baseURL), bytes.NewReader(body))
if err != nil {
return fmt.Errorf("could not create request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
res, err := c.c.Do(req)
if err != nil {
return fmt.Errorf("could not make request: %v", err)
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("server returned status %d", res.StatusCode)
}
return nil
}
func (c *Client) Updated(ks []Kind, ts time.Time) ([]Item, error) {
ksStr := make([]string, 0, len(ks))
for _, k := range ks {
ksStr = append(ksStr, string(k))
}
u := fmt.Sprintf("%s/sync?ks=%s", c.baseURL, strings.Join(ksStr, ","))
if !ts.IsZero() {
u = fmt.Sprintf("%s&ts=", url.QueryEscape(ts.Format(time.RFC3339)))
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("could not get response: %v", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status %d", res.StatusCode)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %v", err)
}
var items []Item
if err := json.Unmarshal(body, &items); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %v", err)
}
return items, nil
}

View File

@ -1,25 +0,0 @@
module code.ewintr.nl/planner/cal
go 1.21.5
require (
code.ewintr.nl/planner/sync v0.0.0-20240912051930-423effa1b447
github.com/google/uuid v1.6.0
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.22.0 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.0 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
replace code.ewintr.nl/planner/sync => ../sync

View File

@ -1,37 +0,0 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU=
modernc.org/sqlite v1.33.0 h1:WWkA/T2G17okiLGgKAj4/RMIvgyMT19yQ038160IeYk=
modernc.org/sqlite v1.33.0/go.mod h1:9uQ9hF/pCZoYZK73D/ud5Z7cIRIILSZI8NdIemVMTX8=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

View File

@ -1,39 +0,0 @@
package main
import (
"fmt"
"os"
"time"
)
func main() {
fmt.Println("cal")
c := NewClient("http://localhost:8092", "testKey")
items, err := c.Updated([]Kind{KindEvent}, time.Time{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%+v\n", items)
i := Item{
ID: "id-1",
Kind: KindEvent,
Updated: time.Now(),
Body: "body",
}
if err := c.Update([]Item{i}); err != nil {
fmt.Println(err)
os.Exit(1)
}
items, err = c.Updated([]Kind{KindEvent}, time.Time{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%+v\n", items)
}

View File

@ -1,35 +0,0 @@
package main
import (
"time"
"github.com/google/uuid"
)
type Kind string
const (
KindTask Kind = "task"
KindEvent Kind = "event"
)
var (
KnownKinds = []Kind{KindTask, KindEvent}
)
type Item struct {
ID string `json:"id"`
Kind Kind `json:"kind"`
Updated time.Time `json:"updated"`
Deleted bool `json:"deleted"`
Body string `json:"body"`
}
func NewItem(k Kind, body string) Item {
return Item{
ID: uuid.New().String(),
Kind: k,
Updated: time.Now(),
Body: body,
}
}

View File

@ -1,14 +1,11 @@
module code.ewintr.nl/planner/sync
module code.ewintr.nl/planner
go 1.21.5
require (
github.com/google/uuid v1.6.0
modernc.org/sqlite v1.33.0
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
@ -17,6 +14,7 @@ require (
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.0 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)

View File

@ -1,7 +1,7 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
@ -10,19 +10,11 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=

View File

@ -7,7 +7,6 @@ import (
"log/slog"
"net/http"
"path"
"slices"
"strings"
"time"
)
@ -34,9 +33,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if r.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", s.apiKey) {
msg := "not authorized"
http.Error(w, fmtError(msg), http.StatusUnauthorized)
s.logger.Info(msg)
http.Error(w, `{"error":"not authorized"}`, http.StatusUnauthorized)
return
}
@ -49,9 +46,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case head == "sync" && r.Method == http.MethodPost:
s.SyncPost(w, r)
default:
msg := "not found"
http.Error(w, fmtError(msg), http.StatusNotFound)
s.logger.Info(msg)
http.Error(w, `{"error":"not found"}`, http.StatusNotFound)
}
}
@ -61,39 +56,20 @@ func (s *Server) SyncGet(w http.ResponseWriter, r *http.Request) {
if tsStr != "" {
var err error
if timestamp, err = time.Parse(time.RFC3339, tsStr); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
ks := make([]Kind, 0)
ksStr := r.URL.Query().Get("ks")
if ksStr != "" {
for _, k := range strings.Split(ksStr, ",") {
if !slices.Contains(KnownKinds, Kind(k)) {
msg := fmt.Sprintf("unknown kind: %s", k)
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
return
}
ks = append(ks, Kind(k))
}
}
items, err := s.syncer.Updated(ks, timestamp)
items, err := s.syncer.Updated(timestamp)
if err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
http.Error(w, fmtError(err), http.StatusInternalServerError)
return
}
body, err := json.Marshal(items)
if err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
http.Error(w, fmtError(err), http.StatusInternalServerError)
return
}
@ -104,51 +80,21 @@ func (s *Server) SyncGet(w http.ResponseWriter, r *http.Request) {
func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
http.Error(w, fmtError(err), http.StatusBadRequest)
return
}
defer r.Body.Close()
var items []Item
var items []Syncable
if err := json.Unmarshal(body, &items); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
http.Error(w, fmtError(err), http.StatusBadRequest)
return
}
for _, item := range items {
if item.ID == "" {
msg := "item without an id"
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
return
}
if item.Kind == "" {
msg := fmt.Sprintf("item %s does not have a kind", item.ID)
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
return
}
if !slices.Contains(KnownKinds, item.Kind) {
msg := fmt.Sprintf("items %s does not have a know kind", item.ID)
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
return
}
if item.Body == "" {
msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, item.ID)
http.Error(w, msg, http.StatusBadRequest)
s.logger.Info(msg)
return
}
item.Updated = time.Now()
if err := s.syncer.Update(item); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
http.Error(w, fmtError(err), http.StatusInternalServerError)
return
}
}
@ -174,6 +120,6 @@ func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status":"ok"}`)
}
func fmtError(msg string) string {
return fmt.Sprintf(`{"error":%q}`, msg)
func fmtError(err error) string {
return fmt.Sprintf(`{"error":%q}`, err.Error())
}

View File

@ -11,7 +11,6 @@ import (
"net/url"
"os"
"sort"
"strings"
"testing"
"time"
)
@ -56,10 +55,10 @@ func TestSyncGet(t *testing.T) {
now := time.Now()
mem := NewMemory()
items := []Item{
{ID: "id-0", Kind: KindEvent, Updated: now.Add(-10 * time.Minute)},
{ID: "id-1", Kind: KindEvent, Updated: now.Add(-5 * time.Minute)},
{ID: "id-2", Kind: KindTask, Updated: now.Add(time.Minute)},
items := []Syncable{
{ID: "id-0", Updated: now.Add(-10 * time.Minute)},
{ID: "id-1", Updated: now.Add(-5 * time.Minute)},
{ID: "id-2", Updated: now.Add(time.Minute)},
}
for _, item := range items {
@ -74,9 +73,8 @@ func TestSyncGet(t *testing.T) {
for _, tc := range []struct {
name string
ts time.Time
ks []string
expStatus int
expItems []Item
expItems []Syncable
}{
{
name: "full",
@ -84,28 +82,14 @@ func TestSyncGet(t *testing.T) {
expItems: items,
},
{
name: "new",
name: "normal",
ts: now.Add(-6 * time.Minute),
expStatus: http.StatusOK,
expItems: []Item{items[1], items[2]},
},
{
name: "kind",
ks: []string{string(KindTask)},
expStatus: http.StatusOK,
expItems: []Item{items[2]},
},
{
name: "unknown kind",
ks: []string{"test"},
expStatus: http.StatusBadRequest,
expItems: []Syncable{items[1], items[2]},
},
} {
t.Run(tc.name, func(t *testing.T) {
url := fmt.Sprintf("/sync?ts=%s", url.QueryEscape(tc.ts.Format(time.RFC3339)))
if len(tc.ks) > 0 {
url = fmt.Sprintf("%s&ks=%s", url, strings.Join(tc.ks, ","))
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("exp nil, got %v", err)
@ -117,11 +101,7 @@ func TestSyncGet(t *testing.T) {
if res.Result().StatusCode != tc.expStatus {
t.Errorf("exp %v, got %v", tc.expStatus, res.Result().StatusCode)
}
if tc.expStatus != http.StatusOK {
return
}
var actItems []Item
var actItems []Syncable
actBody, err := io.ReadAll(res.Result().Body)
if err != nil {
t.Errorf("exp nil, got %v", err)
@ -155,34 +135,27 @@ func TestSyncPost(t *testing.T) {
name string
reqBody []byte
expStatus int
expItems []Item
expItems []Syncable
}{
{
name: "empty",
expStatus: http.StatusBadRequest,
},
{
name: "invalid json",
name: "invalid",
reqBody: []byte(`{"fail}`),
expStatus: http.StatusBadRequest,
},
{
name: "invalid item",
reqBody: []byte(`[
{"id":"id-1","kind":"event","updated":"2024-09-06T08:00:00Z"},
]`),
expStatus: http.StatusBadRequest,
},
{
name: "normal",
reqBody: []byte(`[
{"id":"id-1","kind":"event","updated":"2024-09-06T08:00:00Z","deleted":false,"body":"item"},
{"id":"id-2","kind":"event","updated":"2024-09-06T08:12:00Z","deleted":false,"body":"item2"}
{"ID":"id-1","Updated":"2024-09-06T08:00:00Z","Deleted":false,"Item":""},
{"ID":"id-2","Updated":"2024-09-06T08:12:00Z","Deleted":false,"Item":""}
]`),
expStatus: http.StatusNoContent,
expItems: []Item{
{ID: "id-1", Kind: KindEvent, Updated: time.Date(2024, 9, 6, 8, 0, 0, 0, time.UTC)},
{ID: "id-2", Kind: KindEvent, Updated: time.Date(2024, 9, 6, 12, 0, 0, 0, time.UTC)},
expItems: []Syncable{
{ID: "id-1", Updated: time.Date(2024, 9, 6, 8, 0, 0, 0, time.UTC)},
{ID: "id-2", Updated: time.Date(2024, 9, 6, 12, 0, 0, 0, time.UTC)},
},
},
} {
@ -201,7 +174,7 @@ func TestSyncPost(t *testing.T) {
t.Errorf("exp %v, got %v", tc.expStatus, res.Result().StatusCode)
}
actItems, err := mem.Updated([]Kind{}, time.Time{})
actItems, err := mem.Updated(time.Time{})
if err != nil {
t.Errorf("exp nil, git %v", err)
}

View File

@ -11,11 +11,6 @@ import (
)
func main() {
dbPath := os.Getenv("PLANNER_DB_PATH")
if dbPath == "" {
fmt.Println("PLANNER_DB_PATH is empty")
os.Exit(1)
}
port, err := strconv.Atoi(os.Getenv("PLANNER_PORT"))
if err != nil {
fmt.Println("PLANNER_PORT env is not an integer")
@ -27,22 +22,16 @@ func main() {
os.Exit(1)
}
repo, err := NewSqlite(dbPath)
//mem := NewMemory()
repo, err := NewSqlite("test.db")
if err != nil {
fmt.Printf("could not open sqlite db: %s", err.Error())
os.Exit(1)
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger.Info("configuration", "configuration", map[string]string{
"dbPath": dbPath,
"port": fmt.Sprintf("%d", port),
"apiKey": "***",
})
address := fmt.Sprintf(":%d", port)
srv := NewServer(repo, apiKey, logger)
go http.ListenAndServe(address, srv)
go http.ListenAndServe(fmt.Sprintf(":%d", port), NewServer(repo, apiKey, logger))
logger.Info("service started")

33
sync-service/memory.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"time"
)
type Memory struct {
items map[string]Syncable
}
func NewMemory() *Memory {
return &Memory{
items: make(map[string]Syncable),
}
}
func (m *Memory) Update(item Syncable) error {
m.items[item.ID] = item
return nil
}
func (m *Memory) Updated(timestamp time.Time) ([]Syncable, error) {
result := make([]Syncable, 0)
for _, i := range m.items {
if timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp) {
result = append(result, i)
}
}
return result, nil
}

View File

@ -11,7 +11,7 @@ func TestMemoryItem(t *testing.T) {
mem := NewMemory()
t.Log("start empty")
actItems, actErr := mem.Updated([]Kind{}, time.Time{})
actItems, actErr := mem.Updated(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -20,11 +20,11 @@ func TestMemoryItem(t *testing.T) {
}
t.Log("add one")
t1 := NewItem(Kind("kinda"), "test")
t1 := NewSyncable("test")
if actErr := mem.Update(t1); actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
actItems, actErr = mem.Updated([]Kind{}, time.Time{})
actItems, actErr = mem.Updated(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -38,11 +38,11 @@ func TestMemoryItem(t *testing.T) {
before := time.Now()
t.Log("add second")
t2 := NewItem(Kind("kindb"), "test 2")
t2 := NewSyncable("test 2")
if actErr := mem.Update(t2); actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
actItems, actErr = mem.Updated([]Kind{}, time.Time{})
actItems, actErr = mem.Updated(time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -56,7 +56,7 @@ func TestMemoryItem(t *testing.T) {
t.Errorf("exp %v, got %v", actItems[1].ID, t2.ID)
}
actItems, actErr = mem.Updated([]Kind{}, before)
actItems, actErr = mem.Updated(before)
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -72,7 +72,7 @@ func TestMemoryItem(t *testing.T) {
if actErr := mem.Update(t1); actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
actItems, actErr = mem.Updated([]Kind{}, before)
actItems, actErr = mem.Updated(before)
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -85,16 +85,4 @@ func TestMemoryItem(t *testing.T) {
if actItems[1].ID != t2.ID {
t.Errorf("exp %v, got %v", actItems[1].ID, t2.ID)
}
t.Log("select kind")
actItems, actErr = mem.Updated([]Kind{"kinda"}, time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actItems) != 1 {
t.Errorf("exp 1, got %d", len(actItems))
}
if actItems[0].ID != t1.ID {
t.Errorf("exp %v, got %v", t1.ID, actItems[0].ID)
}
}

51
sync-service/planner.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"time"
"github.com/google/uuid"
)
type Kind string
const (
KindTask Kind = "task"
)
type Syncable struct {
ID string `json:"id"`
Kind Kind `json:"kind"`
Updated time.Time `json:"updated"`
Deleted bool `json:"deleted"`
Item string `json:"item"`
}
func NewSyncable(item string) Syncable {
return Syncable{
ID: uuid.New().String(),
Updated: time.Now(),
Item: item,
}
}
type Task struct {
id string
description string
updated time.Time
}
func NewTask(description string) Task {
return Task{
id: uuid.New().String(),
description: description,
updated: time.Now(),
}
}
func (t Task) ID() string {
return t.id
}
func (t Task) Updated() time.Time {
return t.updated
}

View File

@ -4,7 +4,6 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"time"
_ "modernc.org/sqlite"
@ -49,7 +48,7 @@ func NewSqlite(dbPath string) (*Sqlite, error) {
return s, nil
}
func (s *Sqlite) Update(item Item) error {
func (s *Sqlite) Update(item Syncable) error {
if _, err := s.db.Exec(`
INSERT INTO items
(id, kind, updated, deleted, body)
@ -57,47 +56,31 @@ VALUES
(?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE
SET
kind=excluded.kind,
updated=excluded.updated,
deleted=excluded.deleted,
body=excluded.body`,
item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Body); err != nil {
kind=?,
updated=?,
deleted=?,
body=?`,
item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item,
item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
return nil
}
func (s *Sqlite) Updated(ks []Kind, t time.Time) ([]Item, error) {
query := `
func (s *Sqlite) Updated(t time.Time) ([]Syncable, error) {
rows, err := s.db.Query(`
SELECT id, kind, updated, deleted, body
FROM items
WHERE updated > ?`
var rows *sql.Rows
var err error
if len(ks) == 0 {
rows, err = s.db.Query(query, t.Format(timestampFormat))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
} else {
args := []any{t.Format(timestampFormat)}
ph := make([]string, 0, len(ks))
for _, k := range ks {
args = append(args, string(k))
ph = append(ph, "?")
}
query = fmt.Sprintf("%s AND kind in (%s)", query, strings.Join(ph, ","))
rows, err = s.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
WHERE updated > ?`, t.Format(timestampFormat))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
result := make([]Item, 0)
result := make([]Syncable, 0)
defer rows.Close()
for rows.Next() {
var item Item
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil {
var item Syncable
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Item); err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
result = append(result, item)

View File

@ -10,6 +10,6 @@ var (
)
type Syncer interface {
Update(item Item) error
Updated(kind []Kind, t time.Time) ([]Item, error)
Update(item Syncable) error
Updated(t time.Time) ([]Syncable, error)
}

View File

@ -1,11 +0,0 @@
FROM golang:1.23-alpine
RUN apk add --no-cache git
WORKDIR /src
COPY . ./
RUN go mod download
RUN go build -o syncservice
CMD ["./syncservice"]

View File

@ -1,36 +0,0 @@
package main
import (
"slices"
"time"
)
type Memory struct {
items map[string]Item
}
func NewMemory() *Memory {
return &Memory{
items: make(map[string]Item),
}
}
func (m *Memory) Update(item Item) error {
m.items[item.ID] = item
return nil
}
func (m *Memory) Updated(kinds []Kind, timestamp time.Time) ([]Item, error) {
result := make([]Item, 0)
for _, i := range m.items {
timeOK := timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp)
kindOK := len(kinds) == 0 || slices.Contains(kinds, i.Kind)
if timeOK && kindOK {
result = append(result, i)
}
}
return result, nil
}

View File

@ -1,35 +0,0 @@
package main
import (
"time"
"github.com/google/uuid"
)
type Kind string
const (
KindTask Kind = "task"
KindEvent Kind = "event"
)
var (
KnownKinds = []Kind{KindTask, KindEvent}
)
type Item struct {
ID string `json:"id"`
Kind Kind `json:"kind"`
Updated time.Time `json:"updated"`
Deleted bool `json:"deleted"`
Body string `json:"body"`
}
func NewItem(k Kind, body string) Item {
return Item{
ID: uuid.New().String(),
Kind: k,
Updated: time.Now(),
Body: body,
}
}