single module

This commit is contained in:
Erik Winter 2024-09-18 07:55:14 +02:00 committed by Erik Winter
parent 225c0db847
commit 671deb355a
19 changed files with 136 additions and 93 deletions

View File

@ -1,6 +1,4 @@
sync-run:
cd sync && PLANNER_DB_PATH=test.db PLANNER_PORT=8092 PLANNER_API_KEY=testKey go run .
cd sync/service && 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

View File

@ -1,5 +0,0 @@
module go-mod.ewintr.nl/planner/cal
go 1.21.5
require github.com/google/uuid v1.6.0

View File

@ -6,14 +6,14 @@ import (
"time"
"go-mod.ewintr.nl/planner/sync/client"
"go-mod.ewintr.nl/planner/sync/planner"
"go-mod.ewintr.nl/planner/sync/item"
)
func main() {
fmt.Println("cal")
c := client.NewClient("http://localhost:8092", "testKey")
items, err := c.Updated([]planner.Kind{planner.KindEvent}, time.Time{})
items, err := c.Updated([]item.Kind{item.KindEvent}, time.Time{})
if err != nil {
fmt.Println(err)
os.Exit(1)
@ -21,18 +21,18 @@ func main() {
fmt.Printf("%+v\n", items)
i := planner.Item{
i := item.Item{
ID: "id-1",
Kind: planner.KindEvent,
Kind: item.KindEvent,
Updated: time.Now(),
Body: "body",
}
if err := c.Update([]planner.Item{i}); err != nil {
if err := c.Update([]item.Item{i}); err != nil {
fmt.Println(err)
os.Exit(1)
}
items, err = c.Updated([]planner.Kind{planner.KindEvent}, time.Time{})
items, err = c.Updated([]item.Kind{item.KindEvent}, time.Time{})
if err != nil {
fmt.Println(err)
os.Exit(1)

View File

@ -1,4 +1,4 @@
package service
package main
import (
"time"

20
go.mod Normal file
View File

@ -0,0 +1,20 @@
module go-mod.ewintr.nl/planner
go 1.21.5
require (
github.com/dustin/go-humanize v1.0.1 // 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
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/libc v1.55.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.1 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)

29
go.sum Normal file
View File

@ -0,0 +1,29 @@
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/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/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/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=
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/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U=
modernc.org/libc v1.55.3/go.mod h1:qFXepLhz+JjFThQ4kzwzOjA/y/artDeg+pcYnY+Q83w=
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.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM=
modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
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,5 +0,0 @@
module go-mod.ewintr.nl/planner/sync-client
go 1.21.5
require github.com/google/uuid v1.6.0

View File

@ -1,2 +0,0 @@
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=

View File

@ -1,3 +0,0 @@
module go-mod.ewintr.nl/planner/sync-service
go 1.21.5

View File

@ -1,15 +0,0 @@
package main
import (
"errors"
"time"
)
var (
ErrNotFound = errors.New("not found")
)
type Syncer interface {
Update(item Item) error
Updated(kind []Kind, t time.Time) ([]Item, error)
}

View File

@ -9,6 +9,8 @@ import (
"net/url"
"strings"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
type Client struct {
@ -27,7 +29,7 @@ func NewClient(url, apiKey string) *Client {
}
}
func (c *Client) Update(items []Item) error {
func (c *Client) Update(items []item.Item) error {
body, err := json.Marshal(items)
if err != nil {
return fmt.Errorf("could not marhal body: %v", err)
@ -49,7 +51,7 @@ func (c *Client) Update(items []Item) error {
return nil
}
func (c *Client) Updated(ks []Kind, ts time.Time) ([]Item, error) {
func (c *Client) Updated(ks []item.Kind, ts time.Time) ([]item.Item, error) {
ksStr := make([]string, 0, len(ks))
for _, k := range ks {
ksStr = append(ksStr, string(k))
@ -78,7 +80,7 @@ func (c *Client) Updated(ks []Kind, ts time.Time) ([]Item, error) {
return nil, fmt.Errorf("could not read response body: %v", err)
}
var items []Item
var items []item.Item
if err := json.Unmarshal(body, &items); err != nil {
return nil, fmt.Errorf("could not unmarshal response body: %v", err)
}

View File

@ -1,4 +1,4 @@
package client
package item
import (
"time"

View File

@ -10,6 +10,8 @@ import (
"slices"
"strings"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
type Server struct {
@ -67,17 +69,17 @@ func (s *Server) SyncGet(w http.ResponseWriter, r *http.Request) {
return
}
}
ks := make([]Kind, 0)
ks := make([]item.Kind, 0)
ksStr := r.URL.Query().Get("ks")
if ksStr != "" {
for _, k := range strings.Split(ksStr, ",") {
if !slices.Contains(KnownKinds, Kind(k)) {
if !slices.Contains(item.KnownKinds, item.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))
ks = append(ks, item.Kind(k))
}
}
@ -111,7 +113,7 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
var items []Item
var items []item.Item
if err := json.Unmarshal(body, &items); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
@ -119,33 +121,33 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
return
}
for _, item := range items {
if item.ID == "" {
for _, it := range items {
if it.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)
if it.Kind == "" {
msg := fmt.Sprintf("item %s does not have a kind", it.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)
if !slices.Contains(item.KnownKinds, it.Kind) {
msg := fmt.Sprintf("items %s does not have a know kind", it.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)
if it.Body == "" {
msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, it.ID)
http.Error(w, msg, http.StatusBadRequest)
s.logger.Info(msg)
return
}
item.Updated = time.Now()
if err := s.syncer.Update(item); err != nil {
it.Updated = time.Now()
if err := s.syncer.Update(it); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)

View File

@ -14,6 +14,8 @@ import (
"strings"
"testing"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
func TestServerServeHTTP(t *testing.T) {
@ -56,10 +58,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 := []item.Item{
{ID: "id-0", Kind: item.KindEvent, Updated: now.Add(-10 * time.Minute)},
{ID: "id-1", Kind: item.KindEvent, Updated: now.Add(-5 * time.Minute)},
{ID: "id-2", Kind: item.KindTask, Updated: now.Add(time.Minute)},
}
for _, item := range items {
@ -76,7 +78,7 @@ func TestSyncGet(t *testing.T) {
ts time.Time
ks []string
expStatus int
expItems []Item
expItems []item.Item
}{
{
name: "full",
@ -87,13 +89,13 @@ func TestSyncGet(t *testing.T) {
name: "new",
ts: now.Add(-6 * time.Minute),
expStatus: http.StatusOK,
expItems: []Item{items[1], items[2]},
expItems: []item.Item{items[1], items[2]},
},
{
name: "kind",
ks: []string{string(KindTask)},
ks: []string{string(item.KindTask)},
expStatus: http.StatusOK,
expItems: []Item{items[2]},
expItems: []item.Item{items[2]},
},
{
name: "unknown kind",
@ -121,7 +123,7 @@ func TestSyncGet(t *testing.T) {
return
}
var actItems []Item
var actItems []item.Item
actBody, err := io.ReadAll(res.Result().Body)
if err != nil {
t.Errorf("exp nil, got %v", err)
@ -155,7 +157,7 @@ func TestSyncPost(t *testing.T) {
name string
reqBody []byte
expStatus int
expItems []Item
expItems []item.Item
}{
{
name: "empty",
@ -180,9 +182,9 @@ func TestSyncPost(t *testing.T) {
{"id":"id-2","kind":"event","updated":"2024-09-06T08:12:00Z","deleted":false,"body":"item2"}
]`),
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: []item.Item{
{ID: "id-1", Kind: item.KindEvent, Updated: time.Date(2024, 9, 6, 8, 0, 0, 0, time.UTC)},
{ID: "id-2", Kind: item.KindEvent, Updated: time.Date(2024, 9, 6, 12, 0, 0, 0, time.UTC)},
},
},
} {
@ -201,7 +203,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([]item.Kind{}, time.Time{})
if err != nil {
t.Errorf("exp nil, git %v", err)
}

View File

@ -8,8 +8,6 @@ import (
"os/signal"
"strconv"
"syscall"
"go-mod.ewintr.nl/planner/sync/planner"
)
func main() {
@ -29,7 +27,7 @@ func main() {
os.Exit(1)
}
repo, err := planner.NewSqlite(dbPath)
repo, err := NewSqlite(dbPath)
if err != nil {
fmt.Printf("could not open sqlite db: %s", err.Error())
os.Exit(1)
@ -43,7 +41,7 @@ func main() {
})
address := fmt.Sprintf(":%d", port)
srv := planner.NewServer(repo, apiKey, logger)
srv := NewServer(repo, apiKey, logger)
go http.ListenAndServe(address, srv)
logger.Info("service started")

View File

@ -3,26 +3,28 @@ package main
import (
"slices"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
type Memory struct {
items map[string]Item
items map[string]item.Item
}
func NewMemory() *Memory {
return &Memory{
items: make(map[string]Item),
items: make(map[string]item.Item),
}
}
func (m *Memory) Update(item Item) error {
func (m *Memory) Update(item 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)
func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, error) {
result := make([]item.Item, 0)
for _, i := range m.items {
timeOK := timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp)

View File

@ -3,6 +3,8 @@ package main
import (
"testing"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
func TestMemoryItem(t *testing.T) {
@ -11,7 +13,7 @@ func TestMemoryItem(t *testing.T) {
mem := NewMemory()
t.Log("start empty")
actItems, actErr := mem.Updated([]Kind{}, time.Time{})
actItems, actErr := mem.Updated([]item.Kind{}, time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -20,11 +22,11 @@ func TestMemoryItem(t *testing.T) {
}
t.Log("add one")
t1 := NewItem(Kind("kinda"), "test")
t1 := item.NewItem(item.Kind("kinda"), "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([]item.Kind{}, time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -38,11 +40,11 @@ func TestMemoryItem(t *testing.T) {
before := time.Now()
t.Log("add second")
t2 := NewItem(Kind("kindb"), "test 2")
t2 := item.NewItem(item.Kind("kindb"), "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([]item.Kind{}, time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -56,7 +58,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([]item.Kind{}, before)
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -72,7 +74,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([]item.Kind{}, before)
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
@ -87,7 +89,7 @@ func TestMemoryItem(t *testing.T) {
}
t.Log("select kind")
actItems, actErr = mem.Updated([]Kind{"kinda"}, time.Time{})
actItems, actErr = mem.Updated([]item.Kind{"kinda"}, time.Time{})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}

View File

@ -7,6 +7,7 @@ import (
"strings"
"time"
"go-mod.ewintr.nl/planner/sync/item"
_ "modernc.org/sqlite"
)
@ -49,7 +50,7 @@ func NewSqlite(dbPath string) (*Sqlite, error) {
return s, nil
}
func (s *Sqlite) Update(item Item) error {
func (s *Sqlite) Update(item item.Item) error {
if _, err := s.db.Exec(`
INSERT INTO items
(id, kind, updated, deleted, body)
@ -67,7 +68,7 @@ body=excluded.body`,
return nil
}
func (s *Sqlite) Updated(ks []Kind, t time.Time) ([]Item, error) {
func (s *Sqlite) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
query := `
SELECT id, kind, updated, deleted, body
FROM items
@ -93,10 +94,10 @@ WHERE updated > ?`
}
}
result := make([]Item, 0)
result := make([]item.Item, 0)
defer rows.Close()
for rows.Next() {
var item Item
var item item.Item
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}

17
sync/service/storage.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"errors"
"time"
"go-mod.ewintr.nl/planner/sync/item"
)
var (
ErrNotFound = errors.New("not found")
)
type Syncer interface {
Update(item item.Item) error
Updated(kind []item.Kind, t time.Time) ([]item.Item, error)
}