single module

This commit is contained in:
Erik Winter 2024-09-18 07:55:14 +02:00
parent 225c0db847
commit 3160cf8402
16 changed files with 93 additions and 52 deletions

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

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)
@ -132,7 +134,7 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
s.logger.Info(msg)
return
}
if !slices.Contains(KnownKinds, item.Kind) {
if !slices.Contains(item.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)

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

@ -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)
}