-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (90 loc) · 2.79 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package config
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
)
type Config struct {
HTTP HTTPConfig `json:"http"`
Blizzard BlizzardConfig `json:"blizzard"`
EpicGamesStore EpicGamesStoreConfig `json:"epicGamesStore"`
}
type HTTPConfig struct {
Host string `json:"host"`
SocketPermissions string `json:"socketPermissions"`
ListenHost string `json:"listenHost"`
ListenPort int `json:"listenPort"`
}
type BlizzardConfig struct {
AuthTokenUrl string `json:"authTokenUrl"`
TokenPriceUrl string `json:"tokenPriceUrl"`
}
type EpicGamesStoreConfig struct {
ProductBaseUrl string `json:"productBaseUrl"`
FreeGamesApiUrl string `json:"freeGamesApiUrl"`
}
func New(path string) *Config {
config := &Config{
HTTP: HTTPConfig{
Host: "http://localhost",
ListenHost: "127.0.0.1",
ListenPort: 5000,
SocketPermissions: "0666", // User: rw, Group: rw, Other: rw
},
Blizzard: BlizzardConfig{
AuthTokenUrl: "https://us.battle.net/oauth/token?grant_type=client_credentials",
TokenPriceUrl: "https://us.api.blizzard.com/data/wow/token/index?namespace=dynamic-us",
},
EpicGamesStore: EpicGamesStoreConfig{
ProductBaseUrl: "https://www.epicgames.com/store/en-US/product/",
FreeGamesApiUrl: "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US",
},
}
config.Load(path)
config.ValidateConfig()
return config
}
func (config *Config) Load(path string) {
contents, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
log.Fatal(
fmt.Printf("The file at \"%s\" either does not exist, or could not be read", path),
)
} else {
log.Fatal(err)
}
}
if err := json.Unmarshal(contents, &config); err != nil {
log.Fatalf("Error loading config file \"%s\"\n\nError:\n%v", path, err)
}
}
func (config *Config) ValidateConfig() {
if config.HTTP.Host == "" {
log.Fatal("Config: HTTP host not set! Exiting...")
}
if config.HTTP.SocketPermissions == "" {
log.Fatal("Config: Socket Permissions not set! Exiting...")
}
if config.HTTP.ListenHost == "" {
log.Fatal("Config: HTTP listen host not set! Exiting...")
}
if config.HTTP.ListenPort == 0 {
log.Fatal("Config: HTTP listen port not set! Exiting...")
}
if config.Blizzard.AuthTokenUrl == "" {
log.Fatal("Config: Blizzard auth token url not set! Exiting...")
}
if config.Blizzard.TokenPriceUrl == "" {
log.Fatal("Config: Blizzard token price url not set! Exiting...")
}
if config.EpicGamesStore.ProductBaseUrl == "" {
log.Fatal("Config: Epic Games Store product base url not set! Exiting...")
}
if config.EpicGamesStore.FreeGamesApiUrl == "" {
log.Fatal("Config: Epic Games Store free games api url not set! Exiting...")
}
}