1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-04 12:03:41 +02:00

app/config/config.go: Move from json format to yaml.

This commit is contained in:
Henrik Hautakoski 2023-04-24 17:44:31 +02:00
parent bbf63e447d
commit e4c852c99e
7 changed files with 83 additions and 86 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
build/ build/
config.json config.yml

View file

@ -1,42 +1,43 @@
package config package config
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"gopkg.in/yaml.v3"
shipclient "github.com/eosswedenorg-go/antelope-ship-client" shipclient "github.com/eosswedenorg-go/antelope-ship-client"
) )
type RedisConfig struct { type RedisConfig struct {
Addr string `json:"addr"` Addr string `yaml:"addr"`
Password string `json:"password"` Password string `yaml:"password"`
DB int `json:"db"` DB int `yaml:"db"`
CacheID string `json:"cache_id"` CacheID string `yaml:"cache_id"`
Prefix string `json:"prefix"` Prefix string `yaml:"prefix"`
} }
type TelegramConfig struct { type TelegramConfig struct {
Id string `json:"id"` Id string `yaml:"id"`
Channel int64 `json:"channel"` Channel int64 `yaml:"channel"`
} }
type ShipConfig struct { type ShipConfig struct {
Url string `json:"url"` Url string `yaml:"url"`
IrreversibleOnly bool `json:"irreversible_only"` IrreversibleOnly bool `yaml:"irreversible_only"`
MaxMessagesInFlight uint32 `json:"max_messages_in_flight"` MaxMessagesInFlight uint32 `yaml:"max_messages_in_flight"`
StartBlockNum uint32 `json:"start_block_num"` StartBlockNum uint32 `yaml:"start_block_num"`
EndBlockNum uint32 `json:"end_block_num"` EndBlockNum uint32 `yaml:"end_block_num"`
} }
type Config struct { type Config struct {
Name string `json:"name"` Name string `yaml:"name"`
Ship ShipConfig `json:"ship"` Ship ShipConfig `yaml:"ship"`
Api string `json:"api"` Api string `yaml:"api"`
Redis RedisConfig `json:"redis"` Redis RedisConfig `yaml:"redis"`
MessageCodec string `json:"message_codec"` MessageCodec string `yaml:"message_codec"`
Telegram TelegramConfig `json:"telegram"` Telegram TelegramConfig `yaml:"telegram"`
} }
func Parse(data []byte) (*Config, error) { func Parse(data []byte) (*Config, error) {
@ -56,21 +57,23 @@ func Parse(data []byte) (*Config, error) {
}, },
} }
err := json.Unmarshal(data, &cfg) err := yaml.Unmarshal(data, &cfg)
return &cfg, err return &cfg, err
} }
func (ship *ShipConfig) UnmarshalJSON(data []byte) error { func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) error {
var err error var err error
if err = json.Unmarshal(data, &ship.Url); err != nil { if value.Kind == yaml.ScalarNode {
// ship.Url = value.Value
} else {
type ShipConfigRaw ShipConfig type ShipConfigRaw ShipConfig
raw := ShipConfigRaw(*ship) raw := ShipConfigRaw(*ship)
if err = json.Unmarshal(data, &raw); err == nil { if err = value.Decode(&raw); err == nil {
*ship = ShipConfig(raw) *ship = ShipConfig(raw)
} }
} }
return err return err
} }

View file

@ -27,7 +27,7 @@ func TestParse_Default(t *testing.T) {
}, },
} }
cfg, err := Parse([]byte(`{}`)) cfg, err := Parse([]byte(``))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, cfg, &expected) require.Equal(t, cfg, &expected)
} }
@ -56,28 +56,25 @@ func TestParse(t *testing.T) {
}, },
} }
cfg, err := Parse([]byte(`{ cfg, err := Parse([]byte(`
"name": "ship-reader-1", name: "ship-reader-1"
"api": "http://127.0.0.1:8080", api: "http://127.0.0.1:8080"
"message_codec": "mojibake", message_codec: "mojibake"
"ship": { ship:
"url": "127.0.0.1:8089", url: "127.0.0.1:8089"
"irreversible_only": true, irreversible_only: true
"max_messages_in_flight": 1337, max_messages_in_flight: 1337
"start_block_num": 23671836, start_block_num: 23671836
"end_block_num": 23872222 end_block_num: 23872222
}, telegram:
"telegram": { id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", channel: -123456789
"channel": -123456789 redis:
}, addr: "localhost:6379"
"redis": { password: "passwd"
"addr": "localhost:6379", db: 4
"password": "passwd", prefix: "some::ship"
"db": 4, `))
"prefix": "some::ship"
}
}`))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, cfg, &expected) require.Equal(t, cfg, &expected)
@ -107,21 +104,19 @@ func TestParseShorthandShipUrl(t *testing.T) {
}, },
} }
cfg, err := Parse([]byte(`{ cfg, err := Parse([]byte(`
"name": "ship-reader-1", name: "ship-reader-1"
"api": "http://127.0.0.1:8080", api: "http://127.0.0.1:8080"
"ship": "127.0.0.1:8089", ship: "127.0.0.1:8089"
"telegram": { telegram:
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
"channel": -123456789 channel: -123456789
}, redis:
"redis": { addr: "localhost:6379"
"addr": "localhost:6379", password: "passwd"
"password": "passwd", db: 4
"db": 4, prefix: "some::ship"
"prefix": "some::ship" `))
}
}`))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, cfg, &expected) require.Equal(t, cfg, &expected)

View file

@ -110,7 +110,7 @@ func main() {
showHelp := getopt.BoolLong("help", 'h', "display this help text") showHelp := getopt.BoolLong("help", 'h', "display this help text")
showVersion := getopt.BoolLong("version", 'v', "display this help text") showVersion := getopt.BoolLong("version", 'v', "display this help text")
configFile := getopt.StringLong("config", 'c', "./config.json", "Config file to read", "file") configFile := getopt.StringLong("config", 'c', "./config.yml", "Config file to read", "file")
pidFile := getopt.StringLong("pid", 'p', "", "Where to write process id", "file") pidFile := getopt.StringLong("pid", 'p', "", "Where to write process id", "file")
getopt.Parse() getopt.Parse()

View file

@ -1,21 +0,0 @@
{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"ship": {
"url": "127.0.0.1:8089",
"irreversible_only": false,
"max_messages_in_flight": 10,
"start_block_num": 4294967295,
"end_block_num": 4294967295
},
"telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789
},
"redis": {
"addr": "localhost:6379",
"password": "",
"db": 0,
"prefix": "ship"
}
}

20
config.example.yml Normal file
View file

@ -0,0 +1,20 @@
name: "ship-reader-1"
api: "http://127.0.0.1:8080"
message_codec: "json"
ship:
url: "127.0.0.1:8089"
irreversible_only: false
max_messages_in_flight: 10
start_block_num: 4294967295
end_block_num: 4294967295
telegram:
id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
channel: -123456789
redis:
addr: "localhost:6379"
pasword: ""
db: 0
prefix: "ship"

2
go.mod
View file

@ -14,6 +14,7 @@ require (
github.com/pborman/getopt/v2 v2.1.0 github.com/pborman/getopt/v2 v2.1.0
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2 github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v3 v3.0.1
) )
require ( require (
@ -47,7 +48,6 @@ require (
golang.org/x/sys v0.7.0 // indirect golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect golang.org/x/term v0.7.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )
replace github.com/eosswedenorg/thalos/api => ./api replace github.com/eosswedenorg/thalos/api => ./api