1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-24 10:23:41 +02:00

move config directory to app/config

This commit is contained in:
Henrik Hautakoski 2023-04-20 16:45:47 +02:00
parent a8183a6766
commit c774cbf3ca
3 changed files with 1 additions and 1 deletions

82
app/config/config.go Normal file
View file

@ -0,0 +1,82 @@
package config
import (
"encoding/json"
"io/ioutil"
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
)
type RedisConfig struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int `json:"db"`
CacheID string `json:"cache_id"`
Prefix string `json:"prefix"`
}
type TelegramConfig struct {
Id string `json:"id"`
Channel int64 `json:"channel"`
}
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{
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
DB: 0,
Prefix: "ship",
},
}
err := json.Unmarshal(data, &cfg)
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 {
return nil, err
}
return Parse(bytes)
}

123
app/config/config_test.go Normal file
View file

@ -0,0 +1,123 @@
package config
import (
"testing"
"github.com/stretchr/testify/require"
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
)
func TestParse_Default(t *testing.T) {
expected := Config{
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
DB: 0,
Prefix: "ship",
},
}
cfg, err := Parse([]byte(`{}`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
}
func TestParse(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: 23671836,
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
},
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": {
"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
},
"redis": {
"addr": "localhost:6379",
"password": "passwd",
"db": 4,
"prefix": "some::ship"
}
}`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
}