~yerinalexey/gobin

ref: 64db6c99c5a6cc515dc4f6c92b2651fe9109f726 gobin/config/config.go -rw-r--r-- 895 bytes
64db6c99 — Alexey Yerin chore: update license year 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
// Application configuration

package config

import (
	"os"
	"errors"
	"strconv"
)

type Config struct {
	DbUri string
	Port int

	TemplateRoot string
	StaticRoot string

	BaseUrl string
}

func New() (*Config, error) {
	db_uri := os.Getenv("DATABASE_URI")
	if db_uri == "" {
		return nil, errors.New("DATABASE_URI is not set")
	}

	var port int = 4000

	if v := os.Getenv("PORT"); v != "" {
		var err error
		port, err = strconv.Atoi(v)

		if err != nil {
			return nil, errors.New("PORT is not an integer")
		}
	}

	base_url := os.Getenv("BASE_URL")
	if base_url == "" {
		base_url = "http://localhost:4000"
	}

	template_root := os.Getenv("TEMPLATE_ROOT")
	if template_root == "" {
		template_root = "./templates"
	}

	static_root := os.Getenv("STATIC_ROOT")
	if template_root == "" {
		static_root = "./static"
	}

	return &Config{db_uri, port, template_root, static_root, base_url}, nil
}