~yerinalexey/gobin

ref: 7626ff7cc8c182be92ce4a2d1ce9e14fe8e43f40 gobin/config/config.go -rw-r--r-- 912 bytes
7626ff7c — Alexey Yerin config: rename dbUri to databaseUri 6 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
54
55
56
57
// Application configuration

package config

import (
	"os"
	"errors"

	"github.com/naoina/toml"
)

type Config struct {
	DatabaseUri string
	Port int

	TemplateRoot string
	StaticRoot string

	BaseUrl string
}

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.BaseUrl == "" {
		return nil, errors.New("BaseUri should be set")
	}

	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
}