1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-19 04:50:02 +02:00

config/config.go: put Ship stuff into its own struct/section and also provide a shorthand "string" value for "ship" field (only url)

This commit is contained in:
Henrik Hautakoski 2023-04-17 15:17:46 +02:00
parent e860c81601
commit 5fa1dbb504
4 changed files with 115 additions and 44 deletions

View file

@ -20,27 +20,32 @@ type TelegramConfig struct {
Channel int64 `json:"channel"`
}
type Config struct {
Name string `json:"name"`
ShipApi string `json:"ship_api"`
Api string `json:"api"`
Redis RedisConfig `json:"redis"`
Telegram TelegramConfig `json:"telegram"`
type ShipConfig struct {
Url string `json:"url"`
IrreversibleOnly bool `json:"irreversible_only"`
MaxMessagesInFlight uint32 `json:"max_messages_in_flight"`
StartBlockNum uint32 `json:"start_block_num"`
EndBlockNum uint32 `json:"end_block_num"`
}
type Config struct {
Name string `json:"name"`
Ship ShipConfig `json:"ship"`
Api string `json:"api"`
Redis RedisConfig `json:"redis"`
Telegram TelegramConfig `json:"telegram"`
}
func Parse(data []byte) (*Config, error) {
cfg := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
@ -53,6 +58,20 @@ func Parse(data []byte) (*Config, error) {
return &cfg, err
}
func (ship *ShipConfig) UnmarshalJSON(data []byte) error {
var err error
if err = json.Unmarshal(data, &ship.Url); err != nil {
//
type ShipConfigRaw ShipConfig
raw := ShipConfigRaw(*ship)
if err = json.Unmarshal(data, &raw); err == nil {
*ship = ShipConfig(raw)
}
}
return err
}
func Load(filename string) (*Config, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {

View file

@ -10,10 +10,13 @@ import (
func TestParse_Default(t *testing.T) {
expected := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
@ -29,13 +32,15 @@ func TestParse_Default(t *testing.T) {
func TestParse(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
ShipApi: "127.0.0.1:8089",
StartBlockNum: 23671836,
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
Ship: ShipConfig{
Url: "127.0.0.1:8089",
StartBlockNum: 23671836,
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789,
@ -51,11 +56,56 @@ func TestParse(t *testing.T) {
cfg, err := Parse([]byte(`{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"ship_api": "127.0.0.1:8089",
"irreversible_only": true,
"max_messages_in_flight": 1337,
"start_block_num": 23671836,
"end_block_num": 23872222,
"ship": {
"url": "127.0.0.1:8089",
"irreversible_only": true,
"max_messages_in_flight": 1337,
"start_block_num": 23671836,
"end_block_num": 23872222
},
"telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789
},
"redis": {
"addr": "localhost:6379",
"password": "passwd",
"db": 4,
"prefix": "some::ship"
}
}`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
}
func TestParseShorthandShipUrl(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
Ship: ShipConfig{
Url: "127.0.0.1:8089",
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "passwd",
DB: 4,
Prefix: "some::ship",
},
}
cfg, err := Parse([]byte(`{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"ship": "127.0.0.1:8089",
"telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789