-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.go
109 lines (92 loc) · 2.24 KB
/
endpoints.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
109
package main
import (
"github.com/drawpile/pubsrvproxy/queries"
"log"
"net/http"
)
/**
* Server info
*/
type ServerInfoResponse struct {
ApiName string `json:"api_name"`
Version string `json:"version"`
Name string `json:"name"`
Description string `json:"description"`
Favicon string `json:"favicon"`
ReadOnly bool `json:"read_only"`
Public bool `json:"public"`
Private bool `json:"private"`
}
func (r ServerInfoResponse) WriteResponse(w http.ResponseWriter) {
writeJsonResponse(w, r, http.StatusOK)
}
func ServerInfoEndpoint(ctx *apiContext) apiResponse {
if len(ctx.path) == 0 {
return ServerInfoResponse{
ApiName: "drawpile-session-list",
Version: "1.6",
Name: ctx.cfg.Name,
Description: ctx.cfg.Description,
Favicon: ctx.cfg.Favicon,
ReadOnly: true,
Public: true,
Private: false,
}
} else {
return notFoundResponse()
}
}
/**
* List sessions
*
* The returned response is compatible with the one that the listserver returns.
*/
type SessionListResponse struct {
sessions []queries.SessionInfo
}
func (r SessionListResponse) WriteResponse(w http.ResponseWriter) {
writeJsonResponse(w, r.sessions, http.StatusOK)
}
func SessionListEndpoint(ctx *apiContext) apiResponse {
if len(ctx.path) == 0 {
list, err := queries.QuerySessionInfo(ctx.GetQueryOpts())
if err != nil {
log.Println("Session listing error:", err)
return internalServerError()
}
for i, _ := range list {
list[i].Host = ctx.cfg.ServerHost
list[i].Port = ctx.cfg.ServerPort
}
return SessionListResponse{list}
} else {
return notFoundResponse()
}
}
/**
* List users
*/
type UserListResponse struct {
users []queries.UserInfo
}
func (r UserListResponse) WriteResponse(w http.ResponseWriter) {
writeJsonResponse(w, r.users, http.StatusOK)
}
func UserListEndpoint(ctx *apiContext) apiResponse {
if len(ctx.path) == 0 {
list, err := queries.QueryUserList(ctx.GetQueryOpts())
if err != nil {
log.Println("User listing error:", err)
return internalServerError()
}
if ctx.cfg.ShowUserIps == false {
// Redact IP addresses
for i, _ := range list {
list[i].Ip = ""
}
}
return UserListResponse{list}
} else {
return notFoundResponse()
}
}