-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
70 lines (57 loc) · 1.89 KB
/
config.go
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
package main
import (
"encoding/json"
"fmt"
"os"
)
type Config struct {
WebServerPort int `json:"web_server_port"`
BaseUri string `json:"base_uri"` // without a trailing slash
RedisEndpoint string `json:"redis_endpoint"`
RedisPassword string `json:"redis_password"`
KeyCategories string `json:"key_categories"`
KeyProductCounter string `json:"key_product_counter"`
KeyImageCounter string `json:"key_image_counter"`
KeyImage string `json:"key_image"`
KeyImages string `json:"key_images"`
KeyProduct string `json:"key_product"`
KeyProductImages string `json:"key_product_images"`
KeyAllProducts string `json:"key_all_products"`
KeyProductsInCategory string `json:"key_products_in_category"`
ResultsPerPage int `json:"results_per_page"`
BugsnagKey string `json:"bugsnag_key"`
}
func getConfiguration() Config {
// Get configuration values
configFile, _ := os.Open("conf.json")
defer configFile.Close()
decoder := json.NewDecoder(configFile)
config := Config{}
err := decoder.Decode(&config)
// If there's an error in the json config file we resort to default values
if err != nil {
fmt.Println("❌ Config file error: ", err)
fmt.Println("Reading default configuration.")
config = getDefaultConfiguration()
}
return config
}
func getDefaultConfiguration() Config {
return Config{
WebServerPort: 8080,
BaseUri: "http://localhost:8080",
RedisEndpoint: "localhost:6379",
RedisPassword: "",
KeyCategories: "categories",
KeyProductCounter: "product_counter",
KeyImageCounter: "image_counter",
KeyProduct: "product:%v",
KeyImage: "image:%v",
KeyImages: "images",
KeyProductImages: "product:%v:images",
KeyAllProducts: "products",
KeyProductsInCategory: "products:cat:%v",
ResultsPerPage: 20,
BugsnagKey: "",
}
}