1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +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/
config.json
config.yml

View file

@ -1,42 +1,43 @@
package config
import (
"encoding/json"
"io/ioutil"
"gopkg.in/yaml.v3"
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"`
Addr string `yaml:"addr"`
Password string `yaml:"password"`
DB int `yaml:"db"`
CacheID string `yaml:"cache_id"`
Prefix string `yaml:"prefix"`
}
type TelegramConfig struct {
Id string `json:"id"`
Channel int64 `json:"channel"`
Id string `yaml:"id"`
Channel int64 `yaml:"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"`
Url string `yaml:"url"`
IrreversibleOnly bool `yaml:"irreversible_only"`
MaxMessagesInFlight uint32 `yaml:"max_messages_in_flight"`
StartBlockNum uint32 `yaml:"start_block_num"`
EndBlockNum uint32 `yaml:"end_block_num"`
}
type Config struct {
Name string `json:"name"`
Ship ShipConfig `json:"ship"`
Api string `json:"api"`
Name string `yaml:"name"`
Ship ShipConfig `yaml:"ship"`
Api string `yaml:"api"`
Redis RedisConfig `json:"redis"`
MessageCodec string `json:"message_codec"`
Redis RedisConfig `yaml:"redis"`
MessageCodec string `yaml:"message_codec"`
Telegram TelegramConfig `json:"telegram"`
Telegram TelegramConfig `yaml:"telegram"`
}
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
}
func (ship *ShipConfig) UnmarshalJSON(data []byte) error {
func (ship *ShipConfig) UnmarshalYAML(value *yaml.Node) 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
raw := ShipConfigRaw(*ship)
if err = json.Unmarshal(data, &raw); err == nil {
if err = value.Decode(&raw); err == nil {
*ship = ShipConfig(raw)
}
}
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.Equal(t, cfg, &expected)
}
@ -56,28 +56,25 @@ func TestParse(t *testing.T) {
},
}
cfg, err := Parse([]byte(`{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"message_codec": "mojibake",
"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"
}
}`))
cfg, err := Parse([]byte(`
name: "ship-reader-1"
api: "http://127.0.0.1:8080"
message_codec: "mojibake"
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)
@ -107,21 +104,19 @@ func TestParseShorthandShipUrl(t *testing.T) {
},
}
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"
}
}`))
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)

View file

@ -110,7 +110,7 @@ func main() {
showHelp := getopt.BoolLong("help", 'h', "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")
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/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v3 v3.0.1
)
require (
@ -47,7 +48,6 @@ require (
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // 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