Update `config.go` Add `feed.go` Add `messageTypes.go` Add `process.go` Update `main.go` Update `structures.go` Update `go.mod` Update `go.sum`
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package feed
|
|
|
|
import (
|
|
"context"
|
|
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/config"
|
|
"github.com/go-stomp/stomp/v3"
|
|
"github.com/pkg/errors"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/rs/zerolog/log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
func Run() error {
|
|
log.Info().Msg("starting feed processor")
|
|
|
|
if config.NationalRailUsername == "" || config.NationalRailPassword == "" {
|
|
log.Fatal().Msg("missing National Rail login information")
|
|
}
|
|
|
|
// Connect to Redis
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: config.RedisHostname,
|
|
Username: config.RedisUsername,
|
|
Password: config.RedisPassword,
|
|
DB: 0,
|
|
})
|
|
|
|
_, err := client.Ping(context.Background()).Result()
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to PING Redis")
|
|
}
|
|
|
|
// Setup cancellation thing
|
|
stopSig := make(chan os.Signal, 1)
|
|
signal.Notify(stopSig, syscall.SIGINT)
|
|
|
|
log.Debug().Msg("attempting connection")
|
|
|
|
log.Debug().Str("user", config.NationalRailUsername).Str("pass", config.NationalRailPassword).Send()
|
|
|
|
stompConn, err := stomp.Dial("tcp", "datafeeds.networkrail.co.uk:61618", stomp.ConnOpt.Login(config.NationalRailUsername, config.NationalRailPassword))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Debug().Msg("connected")
|
|
|
|
// Setup watcher
|
|
|
|
subscription, err := stompConn.Subscribe("/topic/TRAIN_MVT_ALL_TOC", stomp.AckAuto)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
select {
|
|
case msg := <-subscription.C:
|
|
log.Debug().Bytes("dat", msg.Body).Send()
|
|
case <-stopSig:
|
|
if err := subscription.Unsubscribe(); err != nil {
|
|
_ = stompConn.MustDisconnect()
|
|
return err
|
|
}
|
|
break
|
|
}
|
|
|
|
return stompConn.Disconnect()
|
|
}
|