summaryrefslogtreecommitdiff
path: root/config.go
blob: 69edf853aaf7febabcd84de2acb90302b1f74cb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
This file is part of Dscuss.
Copyright (C) 2017  Vitaly Minko

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package dscuss

// Config provides access to the parameters from the configuration file and
// saves them to the file.

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"vminko.org/dscuss/errors"
	"vminko.org/dscuss/log"
)

type NetworkConfig struct {
	Address         string
	Port            int
	AddressProvider string
	DHTPort         int
	DHTBootstrap    string
	MaxInConnCount  uint32
	MaxOutConnCount uint32
}

type config struct {
	Network NetworkConfig
}

var defaultConfig = config{
	Network: NetworkConfig{
		Port:            8004,
		AddressProvider: "dht",
		DHTPort:         0,
		DHTBootstrap:    "dscuss.org:6881",
		MaxInConnCount:  10,
		MaxOutConnCount: 10,
	},
}

func (c *config) save(path string) error {
	cfgStr, err := json.MarshalIndent(c, "", "	")
	if err != nil {
		log.Errorf("Can't marshal config: %v", err)
		return err
	}
	err = ioutil.WriteFile(path, []byte(cfgStr), 0644)
	if err != nil {
		log.Errorf("Can't save config file to %s: %v", path, err)
		return err
	}
	return nil
}

func newConfig(path string) (*config, error) {
	var c = defaultConfig

	file, err := os.Open(path)
	if err != nil {
		log.Warningf("Can't open config file %s: %v", path, err)
		err = c.save(path)
		if err != nil {
			log.Fatalf("Can't save default config file to %s: %v", path, err)
		}
		return &c, nil
	}

	decoder := json.NewDecoder(file)
	err = decoder.Decode(&c)
	if err != nil {
		log.Errorf("Error decoding json file %s: %v", err)
		return nil, errors.Config
	}

	/* TBD: validate parameters */

	return &c, nil
}