Initial commit

This commit is contained in:
lash 2024-10-23 21:01:10 +01:00
commit 7c10a881fe
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 262 additions and 0 deletions

31
cmd/main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"git.grassecon.net/term/event/nats"
)
func main() {
ctx := context.Background()
n := nats.NewNatsSubscription()
err := n.Connect(ctx, "localhost:4222")
if err != nil {
fmt.Fprintf(os.Stderr, "Connect err: %v", err)
os.Exit(1)
}
defer n.Close()
cint := make(chan os.Signal)
cterm := make(chan os.Signal)
signal.Notify(cint, os.Interrupt, syscall.SIGINT)
signal.Notify(cterm, os.Interrupt, syscall.SIGTERM)
select {
case _ = <-cint:
case _ = <-cterm:
}
}

18
common/hex.go Normal file
View File

@ -0,0 +1,18 @@
package common
import (
"encoding/hex"
)
func NormalizeHex(s string) (string, error) {
if len(s) >= 2 {
if s[:2] == "0x" {
s = s[2:]
}
}
r, err := hex.DecodeString(s)
if err != nil {
return "", err
}
return hex.EncodeToString(r), nil
}

105
event/nats/nats.go Normal file
View File

@ -0,0 +1,105 @@
package nats
import (
"context"
"encoding/json"
nats "github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"git.defalsify.org/vise.git/logging"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.grassecon.net/term/event"
)
const (
evGive = "TRACKER.FAUCET_GIVE"
evReg = "TRACKER.CUSTODIAL_REGISTRATION"
)
var (
logg = logging.NewVanilla().WithDomain("nats")
)
type NatsSubscription struct {
*event.Router
ctx context.Context
conn *nats.Conn
js jetstream.JetStream
cs jetstream.Consumer
cctx jetstream.ConsumeContext
}
func NewNatsSubscription() *NatsSubscription {
return &NatsSubscription{}
}
func(n *NatsSubscription) Connect(ctx context.Context, connStr string) error {
var err error
n.conn, err = nats.Connect(connStr)
if err != nil {
return err
}
n.js, err = jetstream.New(n.conn)
if err != nil {
return err
}
n.cs, err = n.js.OrderedConsumer(ctx, "TRACKER", jetstream.OrderedConsumerConfig{
//FilterSubjects: []string{"TRACKER.*"},
})
if err != nil {
return err
}
n.ctx = ctx
n.cctx, err = n.cs.Consume(n.handleEvent)
if err != nil {
return err
}
return nil
}
func(n *NatsSubscription) Close() error {
n.cctx.Stop()
select {
case <-n.cctx.Closed():
n.conn.Close()
}
return nil
}
func fail(m jetstream.Msg) {
err := m.Nak()
if err != nil {
logg.Errorf("nats nak fail", "err", err)
}
}
func(n *NatsSubscription) handleEvent(m jetstream.Msg) {
var ev geEvent.Event
logg.Tracef("have msg", "err", m)
b := m.Data()
err := json.Unmarshal(b, &ev)
if err != nil {
logg.Errorf("nats msg deserialize fail", "err", err)
fail(m)
return
}
err = n.Route(&ev)
if err != nil {
logg.Errorf("handler route fail", "err", err)
fail(m)
return
}
err = m.Ack()
// err = m.DoubleAck(n.ctx)
if err != nil {
logg.Errorf("ack fail", "err", err)
panic("ack fail")
}
}

12
event/route.go Normal file
View File

@ -0,0 +1,12 @@
package event
import (
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
)
type Router struct {
}
func(r *Router) Route(ev *geEvent.Event) error {
return nil
}

12
event/sub.go Normal file
View File

@ -0,0 +1,12 @@
package event
import (
"context"
"io"
)
type Subscription interface {
io.Closer
Connect(ctx context.Context, connStr string) error
Next() error
}

19
go.mod Normal file
View File

@ -0,0 +1,19 @@
module git.grassecon.net/term
go 1.23.2
require (
git.defalsify.org/vise.git v0.2.0
github.com/grassrootseconomics/eth-tracker v1.3.0-rc
github.com/nats-io/nats.go v1.37.0
)
require (
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
)

20
go.sum Normal file
View File

@ -0,0 +1,20 @@
git.defalsify.org/vise.git v0.2.0 h1:X2ZgiGRq4C+9qOlDMP0b/oE5QHjVQNT4aEFZB88ST0Q=
git.defalsify.org/vise.git v0.2.0/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE=
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U=
github.com/grassrootseconomics/eth-tracker v1.3.0-rc h1:iYe2rwCBrU5O8x0+HSJRjcPT1h68k/uGd3i/cJJQuTQ=
github.com/grassrootseconomics/eth-tracker v1.3.0-rc/go.mod h1:rLXM5u8FDHnMEdah8ACgo/wfawu4o2sljHGkky2rQKE=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=

9
handler/error.go Normal file
View File

@ -0,0 +1,9 @@
package handler
import (
"fmt"
)
var (
ErrInvalidPayload = fmt.Errorf("Invalid event payload")
)

26
handler/reg.go Normal file
View File

@ -0,0 +1,26 @@
package handler
import (
"git.defalsify.org/vise.git/db"
"git.grassecon.net/term/common"
"git.grassecon.net/term/lookup"
)
func HandleEvReg(store db.Db, payload map[string]any) error {
var err error
address, ok := payload["address"].(string)
if !ok {
return ErrInvalidPayload
}
address, err = common.NormalizeHex(address)
if err != nil {
return err
}
sessionId, err := lookup.GetSessionIdByAddress(store, address)
if err != nil {
return err
}
_ = sessionId
return nil
}

10
lookup/db.go Normal file
View File

@ -0,0 +1,10 @@
package lookup
import (
"git.defalsify.org/vise.git/db"
)
func GetSessionIdByAddress(store db.Db, address string) (string, error) {
var r string
return r, nil
}