-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
115 lines (96 loc) · 2.75 KB
/
http.go
File metadata and controls
115 lines (96 loc) · 2.75 KB
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
109
110
111
112
113
114
115
package logger
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func levelToString(level Level) string {
if level == LevelDebug {
return "debug"
} else if level == LevelInfo {
return "info"
} else if level == LevelWarn {
return "warn"
} else if level == LevelError {
return "error"
} else {
return ""
}
}
// HTTPHandler it's a handler to HTTPFunc function
func HTTPHandler() http.Handler {
return http.HandlerFunc(HTTPFunc)
}
// HTTPFunc permit you control level of all your namespace, and change it in execution time
func HTTPFunc(w http.ResponseWriter, r *http.Request) {
lastpart := strings.LastIndex(r.RequestURI, "/")
namespace := r.RequestURI[lastpart+1:]
// Get list of namespaces and levels
if r.Method == "GET" {
// Get all namespaces
if lastpart == 0 {
namespaces := make(map[string]string, 0)
for namespace, logger := range loggers {
namespace = logger.Namespace
if namespace == "" {
namespace = "_default_"
}
namespaces[namespace] = levelToString(logger.Level)
}
json, _ := json.Marshal(&namespaces)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
io.WriteString(w, string(json))
return
}
if logger, ok := loggers[namespace]; ok {
loggerObj := make(map[string]string, 0)
loggerObj["namespace"] = logger.Namespace
loggerObj["level"] = levelToString(logger.Level)
json, _ := json.Marshal(&loggerObj)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
io.WriteString(w, string(json))
} else {
http.Error(w, fmt.Sprintf("namespace '%s' not found", namespace), http.StatusNotFound)
}
return
}
if r.Method == "PUT" {
var userLevel map[string]interface{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&userLevel); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if userLevel["level"] == nil {
http.Error(w, "missing 'level' field", http.StatusBadRequest)
return
}
if lastpart == 0 {
if userLevel["namespace"] == nil {
http.Error(w, "missing 'namespace' field", http.StatusBadRequest)
return
}
namespace = userLevel["namespace"].(string)
}
level := GetLevelByString(userLevel["level"].(string))
if namespace == "all" {
for _, logger := range loggers {
logger.SetLevel(level)
}
} else if logger, ok := loggers[namespace]; ok {
logger.SetLevel(level)
} else if namespace == "" {
DefaultLogger.SetLevel(level)
} else {
http.Error(w, fmt.Sprintf("namespace '%s' not found", namespace), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
return
}
w.WriteHeader(http.StatusNotImplemented)
io.WriteString(w, http.StatusText(http.StatusNotImplemented))
}