Skip to content

Commit 4243342

Browse files
committed
Add server status route
1 parent 5342d36 commit 4243342

File tree

4 files changed

+118
-7
lines changed

4 files changed

+118
-7
lines changed

docs/openapi.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,79 @@
4242
}
4343
},
4444
"paths": {
45+
"/v1": {
46+
"get": {
47+
"summary": "Retrieve instance's status",
48+
"operationId": "getMeta",
49+
"responses": {
50+
"200": {
51+
"description": "OK.",
52+
"content": {
53+
"application/json": {
54+
"schema": {
55+
"type": "object",
56+
"properties": {
57+
"cache": {
58+
"type": "object",
59+
"properties": {
60+
"lastDetectedMaintenance": {
61+
"type": "object",
62+
"example": {
63+
"EUNA": "0001-01-01T00:00:00Z",
64+
"SA": "0001-01-01T00:00:00Z"
65+
}
66+
},
67+
"responses": {
68+
"type": "object",
69+
"properties": {
70+
"/adventurer": {
71+
"type": "number"
72+
},
73+
"/adventurer/search": {
74+
"type": "number"
75+
},
76+
"/guild": {
77+
"type": "number"
78+
},
79+
"/guild/search": {
80+
"type": "number"
81+
}
82+
}
83+
},
84+
"ttl": {
85+
"type": "object",
86+
"properties": {
87+
"general": {
88+
"type": "string",
89+
"example": "3h0m0s"
90+
},
91+
"maintenanceStatus": {
92+
"type": "string",
93+
"example": "5m0s"
94+
}
95+
}
96+
}
97+
}
98+
},
99+
"proxies": {
100+
"type": "number"
101+
},
102+
"uptime": {
103+
"type": "string",
104+
"example": "2m49s"
105+
},
106+
"version": {
107+
"type": "string",
108+
"example": "1.5.1"
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
},
45118
"/v1/adventurer": {
46119
"get": {
47120
"summary": "Retrieve player's profile.",

handlers/GetStatus.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"bdo-rest-api/config"
9+
"bdo-rest-api/scrapers"
10+
)
11+
12+
var initTime = time.Now()
13+
14+
func GetStatus(w http.ResponseWriter, r *http.Request) {
15+
json.NewEncoder(w).Encode(map[string]interface{}{
16+
"cache": map[string]interface{}{
17+
"lastDetectedMaintenance": scrapers.GetLastCloseTimes(),
18+
"responses": map[string]int{
19+
"/adventurer": profilesCache.GetItemCount(),
20+
"/adventurer/search": profileSearchCache.GetItemCount(),
21+
"/guild": guildProfilesCache.GetItemCount(),
22+
"/guild/search": guildSearchCache.GetItemCount(),
23+
},
24+
"ttl": map[string]string{
25+
"general": config.GetCacheTTL().Round(time.Minute).String(),
26+
"maintenanceStatus": config.GetMaintenanceStatusTTL().Round(time.Minute).String(),
27+
},
28+
},
29+
"proxies": len(config.GetProxyList()),
30+
"uptime": time.Since(initTime).Round(time.Second).String(),
31+
"version": "1.5.1",
32+
})
33+
}

httpServer/BuildServer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313

1414
func BuildServer() *http.Server {
1515
router, err := registerHandlers(map[string]func(http.ResponseWriter, *http.Request){
16-
"/v1/adventurer/search": handlers.GetAdventurerSearch,
17-
"/v1/guild/search": handlers.GetGuildSearch,
16+
"/v1": handlers.GetStatus,
1817
"/v1/adventurer": handlers.GetAdventurer,
18+
"/v1/adventurer/search": handlers.GetAdventurerSearch,
1919
"/v1/guild": handlers.GetGuild,
20+
"/v1/guild/search": handlers.GetGuildSearch,
2021
})
2122

2223
if err != nil {

scrapers/GetCloseTime.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
)
88

99
var lastCloseTimes = map[string]time.Time{
10-
"EU": {},
11-
"SA": {},
10+
"EUNA": {},
11+
"SA": {},
1212
}
1313

1414
func GetCloseTime(region string) (isCloseTime bool, expires time.Time) {
15-
// NA and EU use one website
16-
if region == "NA" {
17-
region = "EU"
15+
// EU and NA use one website
16+
if region == "EU" || region == "NA" {
17+
region = "EUNA"
1818
}
1919

2020
expires = lastCloseTimes[region].Add(config.GetMaintenanceStatusTTL())
@@ -24,3 +24,7 @@ func GetCloseTime(region string) (isCloseTime bool, expires time.Time) {
2424
func setCloseTime(region string) {
2525
lastCloseTimes[region] = time.Now()
2626
}
27+
28+
func GetLastCloseTimes() map[string]time.Time {
29+
return lastCloseTimes
30+
}

0 commit comments

Comments
 (0)