-
Notifications
You must be signed in to change notification settings - Fork 98
/
utils.go
61 lines (55 loc) · 1.31 KB
/
utils.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
package common
import (
"encoding/json"
"log"
"net/http"
"os"
)
type (
appError struct {
Error string `json:"error"`
Message string `json:"message"`
HttpStatus int `json:"status"`
}
errorResource struct {
Data appError `json:"data"`
}
configuration struct {
Server, MongoDBHost, DBUser, DBPwd, Database string
LogLevel int
}
)
func DisplayAppError(w http.ResponseWriter, handlerError error, message string, code int) {
errObj := appError{
Error: handlerError.Error(),
Message: message,
HttpStatus: code,
}
//log.Printf("AppError]: %s\n", handlerError)
Error.Printf("AppError]: %s\n", handlerError)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
if j, err := json.Marshal(errorResource{Data: errObj}); err == nil {
w.Write(j)
}
}
// AppConfig holds the configuration values from config.json file
var AppConfig configuration
// Initialize AppConfig
func initConfig() {
loadAppConfig()
}
// Reads config.json and decode into AppConfig
func loadAppConfig() {
file, err := os.Open("common/config.json")
defer file.Close()
if err != nil {
log.Fatalf("[loadConfig]: %s\n", err)
}
decoder := json.NewDecoder(file)
AppConfig = configuration{}
err = decoder.Decode(&AppConfig)
if err != nil {
log.Fatalf("[loadAppConfig]: %s\n", err)
}
}