chore: initial commit
This commit is contained in:
commit
ccc56508a6
20
go.mod
Normal file
20
go.mod
Normal file
|
@ -0,0 +1,20 @@
|
|||
module go.sour.is/turso-maddy
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require github.com/foxcpp/maddy v0.7.1
|
||||
|
||||
require (
|
||||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 // indirect
|
||||
github.com/emersion/go-imap v1.2.2-0.20220928192137-6fac715be9cf // indirect
|
||||
github.com/emersion/go-message v0.18.0 // indirect
|
||||
github.com/emersion/go-msgauth v0.6.8 // indirect
|
||||
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
|
||||
github.com/emersion/go-smtp v0.20.2-0.20240121112028-434ddca4792e // indirect
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 // indirect
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.26.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
78
go.sum
Normal file
78
go.sum
Normal file
|
@ -0,0 +1,78 @@
|
|||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h1:goHVqTbFX3AIo0tzGr14pgfAW2ZfPChKO21Z9MGf/gk=
|
||||
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emersion/go-imap v1.2.2-0.20220928192137-6fac715be9cf h1:EUDVFh7Cpdv9jClkevx7T0++JQqND+TP5UlCmZAm3YA=
|
||||
github.com/emersion/go-imap v1.2.2-0.20220928192137-6fac715be9cf/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
|
||||
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
|
||||
github.com/emersion/go-message v0.18.0 h1:7LxAXHRpSeoO/Wom3ZApVZYG7c3d17yCScYce8WiXA8=
|
||||
github.com/emersion/go-message v0.18.0/go.mod h1:Zi69ACvzaoV/MBnrxfVBPV3xWEuCmC2nEN39oJF4B8A=
|
||||
github.com/emersion/go-msgauth v0.6.8 h1:kW/0E9E8Zx5CdKsERC/WnAvnXvX7q9wTHia1OA4944A=
|
||||
github.com/emersion/go-msgauth v0.6.8/go.mod h1:YDwuyTCUHu9xxmAeVj0eW4INnwB6NNZoPdLerpSxRrc=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
|
||||
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||
github.com/emersion/go-smtp v0.20.2-0.20240121112028-434ddca4792e h1:WAPhaiA+bDO/mFgCDQJKCQI/RbH/73lCcis4Jb8Y2ec=
|
||||
github.com/emersion/go-smtp v0.20.2-0.20240121112028-434ddca4792e/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
|
||||
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||
github.com/foxcpp/maddy v0.7.1 h1:ShauKW0YGs6IZGXopw4ERdv3QFPXWRl2cRaAKmeqxes=
|
||||
github.com/foxcpp/maddy v0.7.1/go.mod h1:79Si5j6OYg+UGEQF47n8C3zfmw/Zng04jqcLuwXFiOU=
|
||||
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/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 h1:6PfEMwfInASh9hkN83aR0j4W/eKaAZt/AURtXAXlas0=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475/go.mod h1:20nXSmcf0nAscrzqsXeC2/tA3KkV2eCiJqYuyAgl+ss=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1 h1:lQwP++jvwcQiPqqIXIvabCIvfh8bzibpjYvT4s0jWmA=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20240322134723-08771dcdd2f1/go.mod h1:sb520Yr+GHBsfL43FQgQ+rLFfuJkItgRWlTgbIQHVxA=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
|
||||
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
|
||||
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
238
table/turso.go
Normal file
238
table/turso.go
Normal file
|
@ -0,0 +1,238 @@
|
|||
package table
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/foxcpp/maddy/framework/config"
|
||||
"github.com/foxcpp/maddy/framework/module"
|
||||
|
||||
"github.com/tursodatabase/go-libsql"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
modName string
|
||||
instName string
|
||||
|
||||
lookup string
|
||||
add string
|
||||
list string
|
||||
set string
|
||||
del string
|
||||
|
||||
dir string
|
||||
sql *sql.DB
|
||||
connector *libsql.Connector
|
||||
}
|
||||
|
||||
func NewTable(modName, instName string, _, _ []string) (module.Module, error) {
|
||||
return &Table{
|
||||
modName: modName,
|
||||
instName: instName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Table) Name() string {
|
||||
return s.modName
|
||||
}
|
||||
|
||||
func (s *Table) InstanceName() string {
|
||||
return s.instName
|
||||
}
|
||||
|
||||
func (s *Table) Init(cfg *config.Map) error {
|
||||
var (
|
||||
initQueries []string
|
||||
primaryUrl string
|
||||
authToken string
|
||||
dbName = "local.db"
|
||||
)
|
||||
cfg.StringList("init", false, false, nil, &initQueries)
|
||||
|
||||
cfg.String("url", false, true, "", &primaryUrl)
|
||||
cfg.String("token", false, true, "", &authToken)
|
||||
|
||||
cfg.String("lookup", false, true, "", &s.lookup)
|
||||
cfg.String("add", false, false, "", &s.add)
|
||||
cfg.String("list", false, false, "", &s.list)
|
||||
cfg.String("del", false, false, "", &s.del)
|
||||
cfg.String("set", false, false, "", &s.set)
|
||||
|
||||
if _, err := cfg.Process(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
s.dir, err = os.MkdirTemp("", "libsql-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating temporary directory: %w", err)
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(s.dir, dbName)
|
||||
|
||||
syncInterval := time.Minute
|
||||
|
||||
s.connector, err = libsql.NewEmbeddedReplicaConnector(dbPath, primaryUrl,
|
||||
libsql.WithAuthToken(authToken),
|
||||
libsql.WithSyncInterval(syncInterval),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating connector: %w", err)
|
||||
}
|
||||
|
||||
s.sql = sql.OpenDB(s.connector)
|
||||
|
||||
if len(initQueries) > 0 {
|
||||
// bldr := ws4.NewRequestBuilder()
|
||||
// for _, init := range initQueries {
|
||||
// bldr.AddStatement(init)
|
||||
// }
|
||||
// req, err := bldr.Build()
|
||||
// if err != nil {
|
||||
// return config.NodeErr(cfg.Block, "failed to init db: %v", err)
|
||||
// }
|
||||
// _, _, err = db.Send(req)
|
||||
// if err != nil {
|
||||
// return config.NodeErr(cfg.Block, "failed to init db: %v", err)
|
||||
// }
|
||||
tx, err := s.sql.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, qry := range initQueries {
|
||||
res, err := tx.Exec(qry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := res.RowsAffected()
|
||||
log.Println("db rows effected: ", rows, err)
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Table) Close() error {
|
||||
defer os.RemoveAll(s.dir)
|
||||
defer s.connector.Close()
|
||||
|
||||
return s.sql.Close()
|
||||
}
|
||||
|
||||
func (s *Table) Lookup(ctx context.Context, val string) (value string, ok bool, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: lookup: %w", s.modName, err)
|
||||
}
|
||||
}()
|
||||
|
||||
row := s.sql.QueryRowContext(ctx, s.lookup, val)
|
||||
err = row.Scan(&value)
|
||||
|
||||
return value, err == nil, err
|
||||
}
|
||||
|
||||
func (s *Table) LookupMulti(ctx context.Context, val string) (lis []string, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: lookupMulti: %w", s.modName, err)
|
||||
}
|
||||
}()
|
||||
|
||||
var rows *sql.Rows
|
||||
rows, err = s.sql.QueryContext(ctx, s.lookup, val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var value string
|
||||
err = rows.Scan(&value)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
lis = append(lis, value)
|
||||
}
|
||||
err = rows.Err()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Table) Keys() (lis []string, err error) {
|
||||
if s.list == "" {
|
||||
return nil, fmt.Errorf("%s: table is not mutable (no 'list' query)", s.modName)
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: list: %w", s.modName, err)
|
||||
}
|
||||
}()
|
||||
|
||||
var rows *sql.Rows
|
||||
rows, err = s.sql.Query(s.list)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var value string
|
||||
err = rows.Scan(&value)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
lis = append(lis, value)
|
||||
}
|
||||
err = rows.Err()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Table) RemoveKey(k string) (err error) {
|
||||
if s.del == "" {
|
||||
return fmt.Errorf("%s: table is not mutable (no 'del' query)", s.modName)
|
||||
}
|
||||
|
||||
_, err = s.sql.ExecContext(context.TODO(), s.del, k)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: del %s: %w", s.modName, k, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Table) SetKey(k, v string) (err error) {
|
||||
if s.set == "" {
|
||||
return fmt.Errorf("%s: table is not mutable (no 'set' query)", s.modName)
|
||||
}
|
||||
if s.add == "" {
|
||||
return fmt.Errorf("%s: table is not mutable (no 'add' query)", s.modName)
|
||||
}
|
||||
|
||||
res, err := s.sql.ExecContext(context.TODO(), s.set, k, v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: set %s: %w", s.modName, k, err)
|
||||
}
|
||||
var n int64
|
||||
if n, err = res.RowsAffected(); err != nil && n == 0 {
|
||||
_, err = s.sql.ExecContext(context.TODO(), s.add, k, v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: add %s: %w", s.modName, k, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
module.Register("table.turso_query", NewTable)
|
||||
}
|
Loading…
Reference in New Issue
Block a user