-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_config.go
88 lines (72 loc) · 2.05 KB
/
server_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"strings"
"sync"
)
// ServerConfig structure of server.config.json file
// PersonalToken APIToken personal access token to increase limit rate
// will be use in request's header to github API
type ServerConfig struct {
GithubAPIURL string `json:"github_api_url"`
PersonalToken string `json:"personal_token"`
WorkerNumber int `json:"worker_number"`
}
// will load config file from path
// If no file found no error will be returned, project can work without
func loadConfigFile(path string) (*ServerConfig, error) {
file, err := ioutil.ReadFile(path)
data := ServerConfig{}
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(file), &data)
if err != nil {
return nil, err
}
return &data, nil
}
// Check will check conformity of config file
func (env *ServerConfig) Check() error {
var errorMessages []string
errChan := make(chan error)
wg := &sync.WaitGroup{}
wg.Add(3)
go checkURL(wg, errChan, env.GithubAPIURL, "github_api_url")
go checkWorkerNumber(wg, errChan, env.WorkerNumber, "worker_number")
go checkAPICredentials(wg, errChan, env.PersonalToken, "personal_token")
go func() {
wg.Wait()
close(errChan)
}()
for err := range errChan {
errorMessages = append(errorMessages, err.Error())
}
if len(errorMessages) > 0 {
return errors.New(" → " + strings.Join(errorMessages, "\n → "))
}
return nil
}
func checkURL(wg *sync.WaitGroup, c chan error, raw string, name string) {
defer wg.Done()
_, err := url.Parse(raw)
if err != nil || raw == "" {
c <- fmt.Errorf("%v is not a valid URL: %v", name, err)
}
}
func checkWorkerNumber(wg *sync.WaitGroup, c chan error, raw int, name string) {
defer wg.Done()
if raw <= 0 {
c <- fmt.Errorf("%v is not valid, need to be greater then 0", name)
}
}
func checkAPICredentials(wg *sync.WaitGroup, c chan error, raw string, name string) {
defer wg.Done()
if raw == "" {
c <- fmt.Errorf("%v is empty, you can increase the rate limit when this field is filled in. See README.md", name)
}
}