-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver_api.go
64 lines (57 loc) · 1.71 KB
/
webserver_api.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
/* JSON Functions */
func getJSON(mid func(http.ResponseWriter, *http.Request) string,
w http.ResponseWriter, r *http.Request) string {
w.Header().Set("Content-Type", "application/json")
return mid(w, r)
}
func getAuthJSON(mid func(http.ResponseWriter, *http.Request) string,
w http.ResponseWriter, r *http.Request) string {
session, _ := sessionStore.Get(r, "mcman_session")
// Vaildate that the session is authenticated
if val, ok := session.Values["is_logged_in"].(string); ok {
switch val {
case "":
w.WriteHeader(403)
w.Header().Set("Content-Type", "application/json")
return "{\"status\":\"error\"}"
default:
w.Header().Set("Content-Type", "application/json")
return mid(w, r)
}
} else {
w.WriteHeader(403)
w.Header().Set("Content-Type", "application/json")
return "{\"status\":\"error\"}"
}
return ""
}
func getOnlineUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var err error
var onlineUsers []MCUser
if onlineUsers, err = c.model.getOnlineMCUsers(); err != nil {
fmt.Fprintf(w, "{\"status\":\"error\",\"message\":\"Error loading online users\"}")
}
if err = json.NewEncoder(w).Encode(onlineUsers); err != nil {
log.Println(err)
}
}
func getOps(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(c.Ops); err != nil {
log.Println(err)
}
}
func getWhitelist(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(c.Whitelist); err != nil {
log.Println(err)
}
}