~yerinalexey/gobin

gobin/config/config.go -rw-r--r-- 833 bytes
0b93da80Alexey Yerin gobin-migrate: use tr to cut off quotes and space 7 months ago
                                                                                
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
// Application configuration

package config

import (
	"os"
	"errors"

	"github.com/naoina/toml"
)

type Config struct {
	DatabaseUri string
	Port int

	TemplateRoot string
	StaticRoot string

	Insecure bool
}

func New(configFile string) (*Config, error) {
	f, err := os.Open(configFile)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	cfg := Config{}

	if err = toml.NewDecoder(f).Decode(&cfg); err != nil {
		return nil, err
	}

	// Checks to make sure config is not messed up
	if cfg.DatabaseUri == "" {
		return nil, errors.New("DatabaseUri is not set")
	}

	if cfg.Port == 0 {
		return nil, errors.New("Port can't be 0")
	}

	if cfg.TemplateRoot == "" {
		return nil, errors.New("TemplateRoot should be set")
	}

	if cfg.StaticRoot == "" {
		return nil, errors.New("StaticRoot should be set")
	}

	return &cfg, nil
}