OSDN Git Service

358eee526aac34f9ee643d2c0f4f2f70a720caaf
[bytom/vapor.git] / federation / config / config.go
1 package config
2
3 import (
4         "encoding/json"
5         "os"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/vapor/crypto/ed25519/chainkd"
10 )
11
12 func NewConfig() *Config {
13         if len(os.Args) <= 1 {
14                 log.Fatal("Please setup the config file path")
15         }
16
17         return NewConfigWithPath(os.Args[1])
18 }
19
20 func NewConfigWithPath(path string) *Config {
21         configFile, err := os.Open(path)
22         if err != nil {
23                 log.WithFields(log.Fields{"err": err, "file_path": os.Args[1]}).Fatal("fail to open config file")
24         }
25         defer configFile.Close()
26
27         cfg := &Config{}
28         if err := json.NewDecoder(configFile).Decode(cfg); err != nil {
29                 log.WithField("err", err).Fatal("fail to decode config file")
30         }
31
32         return cfg
33 }
34
35 type Config struct {
36         API         API         `json:"api"`
37         MySQLConfig MySQLConfig `json:"mysql"`
38         Warders     []Warder    `json:"warders"`
39         Quorum      int         `json:"quorum"`
40         Mainchain   Chain       `json:"mainchain"`
41         Sidechain   Chain       `json:"sidechain"`
42 }
43
44 type API struct {
45         ListeningPort uint64 `json:"listening_port"`
46         IsReleaseMode bool   `json:"is_release_mode"`
47 }
48
49 type MySQLConfig struct {
50         Connection MySQLConnection `json:"connection"`
51         LogMode    bool            `json:"log_mode"`
52 }
53
54 type MySQLConnection struct {
55         Host     string `json:"host"`
56         Port     uint   `json:"port"`
57         Username string `json:"username"`
58         Password string `json:"password"`
59         DbName   string `json:"database"`
60 }
61
62 type Warder struct {
63         Position uint8        `json:"position"`
64         XPub     chainkd.XPub `json:"xpub"`
65 }
66
67 type Chain struct {
68         Name          string `json:"name"`
69         Upstream      string `json:"upstream"`
70         SyncSeconds   uint64 `json:"sync_seconds"`
71         Confirmations uint64 `json:"confirmations"`
72 }