Add support for state for Service{} (#142)

Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: #142
pull/149/head
James Mills 6 months ago
parent 35aa48c273
commit f3a847674c
  1. 4
      client.go
  2. 3
      internal/server.go
  3. BIN
      internal/web/app.wasm
  4. 22
      service.go

@ -380,7 +380,9 @@ func (cli *Client) SendToAddr(addr *Addr, msg string) error {
return fmt.Errorf("error encrypting message to %s: %w", addr, err)
}
if err := cli.send.Send(cli.key, addr.Endpoint().String(), string(b), addr.Cap()); err != nil {
endpoint := addr.Endpoint().String()
log.Debugf("sending message to %s", endpoint)
if err := cli.send.Send(cli.key, endpoint, string(b), addr.Cap()); err != nil {
return fmt.Errorf("error sending message to %s: %w", addr, err)
}

@ -230,6 +230,7 @@ func (s *Server) setupCronJobs() error {
func (s *Server) setupServiceUser() error {
svcUser := fmt.Sprintf("%s@%s", serviceUser, s.config.PrimaryDomain)
svcUserState := filepath.Join(s.config.Data, fmt.Sprintf("%s.json", serviceUser))
log.Infof("starting service user %s", svcUser)
@ -253,7 +254,7 @@ func (s *Server) setupServiceUser() error {
return err
}
svc, err := saltyim.NewService(me, id)
svc, err := saltyim.NewService(me, id, svcUserState)
if err != nil {
return err
}

BIN
internal/web/app.wasm (Stored with Git LFS)

Binary file not shown.

@ -17,10 +17,10 @@ import (
type Service struct {
mu sync.RWMutex
me *Addr
id *Identity
cli *Client
me *Addr
id *Identity
cli *Client
state string
textFns map[string]MessageTextHandlerFunc
eventFns map[string]MessageEventHandlerFunc
@ -29,10 +29,11 @@ type Service struct {
type MessageTextHandlerFunc func(context.Context, *Service, *keys.EdX25519PublicKey, *lextwt.SaltyText) error
type MessageEventHandlerFunc func(context.Context, *Service, *keys.EdX25519PublicKey, *lextwt.SaltyEvent) error
func NewService(me *Addr, id *Identity) (*Service, error) {
func NewService(me *Addr, id *Identity, state string) (*Service, error) {
svc := &Service{
me: me,
id: id,
state: state,
textFns: make(map[string]MessageTextHandlerFunc),
eventFns: make(map[string]MessageEventHandlerFunc),
}
@ -76,7 +77,11 @@ func (svc *Service) Run(ctx context.Context) error {
// create the service user's client in a loop until successful
// TODO: Should this timeout? Use a context?
if err := retry.Do(func() error {
cli, err := NewClient(svc.me, WithClientIdentity(WithIdentity(svc.id)))
cli, err := NewClient(
svc.me,
WithStateFromFile(svc.state),
WithClientIdentity(WithIdentity(svc.id)),
)
if err != nil {
return err
}
@ -93,6 +98,11 @@ func (svc *Service) Run(ctx context.Context) error {
); err != nil {
return fmt.Errorf("error setting up service user %s: %w", svc.me, err)
}
defer func() {
if err := svc.cli.State().Save(svc.state); err != nil {
log.WithError(err).Warnf("error saving state: %s", svc.state)
}
}()
log.Debugf("listening for service requests as %s", svc.me)

Loading…
Cancel
Save