Skip to content

Commit

Permalink
Add server status route
Browse files Browse the repository at this point in the history
  • Loading branch information
man90es committed Dec 28, 2023
1 parent 5342d36 commit 4243342
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
73 changes: 73 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,79 @@
}
},
"paths": {
"/v1": {
"get": {
"summary": "Retrieve instance's status",
"operationId": "getMeta",
"responses": {
"200": {
"description": "OK.",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"cache": {
"type": "object",
"properties": {
"lastDetectedMaintenance": {
"type": "object",
"example": {
"EUNA": "0001-01-01T00:00:00Z",
"SA": "0001-01-01T00:00:00Z"
}
},
"responses": {
"type": "object",
"properties": {
"/adventurer": {
"type": "number"
},
"/adventurer/search": {
"type": "number"
},
"/guild": {
"type": "number"
},
"/guild/search": {
"type": "number"
}
}
},
"ttl": {
"type": "object",
"properties": {
"general": {
"type": "string",
"example": "3h0m0s"
},
"maintenanceStatus": {
"type": "string",
"example": "5m0s"
}
}
}
}
},
"proxies": {
"type": "number"
},
"uptime": {
"type": "string",
"example": "2m49s"
},
"version": {
"type": "string",
"example": "1.5.1"
}
}
}
}
}
}
}
}
},
"/v1/adventurer": {
"get": {
"summary": "Retrieve player's profile.",
Expand Down
33 changes: 33 additions & 0 deletions handlers/GetStatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package handlers

import (
"encoding/json"
"net/http"
"time"

"bdo-rest-api/config"
"bdo-rest-api/scrapers"
)

var initTime = time.Now()

func GetStatus(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]interface{}{
"cache": map[string]interface{}{
"lastDetectedMaintenance": scrapers.GetLastCloseTimes(),
"responses": map[string]int{
"/adventurer": profilesCache.GetItemCount(),
"/adventurer/search": profileSearchCache.GetItemCount(),
"/guild": guildProfilesCache.GetItemCount(),
"/guild/search": guildSearchCache.GetItemCount(),
},
"ttl": map[string]string{
"general": config.GetCacheTTL().Round(time.Minute).String(),
"maintenanceStatus": config.GetMaintenanceStatusTTL().Round(time.Minute).String(),
},
},
"proxies": len(config.GetProxyList()),
"uptime": time.Since(initTime).Round(time.Second).String(),
"version": "1.5.1",
})
}
5 changes: 3 additions & 2 deletions httpServer/BuildServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (

func BuildServer() *http.Server {
router, err := registerHandlers(map[string]func(http.ResponseWriter, *http.Request){
"/v1/adventurer/search": handlers.GetAdventurerSearch,
"/v1/guild/search": handlers.GetGuildSearch,
"/v1": handlers.GetStatus,
"/v1/adventurer": handlers.GetAdventurer,
"/v1/adventurer/search": handlers.GetAdventurerSearch,
"/v1/guild": handlers.GetGuild,
"/v1/guild/search": handlers.GetGuildSearch,
})

if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions scrapers/GetCloseTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

var lastCloseTimes = map[string]time.Time{
"EU": {},
"SA": {},
"EUNA": {},
"SA": {},
}

func GetCloseTime(region string) (isCloseTime bool, expires time.Time) {
// NA and EU use one website
if region == "NA" {
region = "EU"
// EU and NA use one website
if region == "EU" || region == "NA" {
region = "EUNA"
}

expires = lastCloseTimes[region].Add(config.GetMaintenanceStatusTTL())
Expand All @@ -24,3 +24,7 @@ func GetCloseTime(region string) (isCloseTime bool, expires time.Time) {
func setCloseTime(region string) {
lastCloseTimes[region] = time.Now()
}

func GetLastCloseTimes() map[string]time.Time {
return lastCloseTimes
}

0 comments on commit 4243342

Please sign in to comment.