Skip to content

Commit 37b4917

Browse files
committed
feat: dont check for issues during API downtime
1 parent 03570b2 commit 37b4917

File tree

1 file changed

+84
-85
lines changed

1 file changed

+84
-85
lines changed

web/notifications.go

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -74,113 +74,112 @@ func NotificationsEndpoint(httpClient *http.Client) echo.HandlerFunc {
7474
}
7575

7676
return func(c echo.Context) error {
77-
ctx := c.Request().Context()
78-
g, gCtx := errgroup.WithContext(ctx)
77+
notifications := make([]notification, 0)
7978

80-
var disabledEndpoints []string
81-
var endpointsWithIssues []string
79+
now := time.Now()
80+
if now.After(apiDowntimeStart) && now.Before(apiDowntimeEnd) {
81+
notifications = append(notifications, notification{
82+
Type: notificationTypeError,
83+
Header: "VoE Release - Guild Wars 2 API Disabled",
84+
Content: fmt.Sprintf(
85+
"The Guild Wars 2 API will be disabled until %s to prevent spoilers of Visions of Eternity. While the API is disabled, you will not be able to add or update API Tokens or verify accounts.\nSee: [Guild Wars 2 API Disabled from October 24–30](https://en-forum.guildwars2.com/topic/163562-guild-wars-2-api-disabled-from-october-24%%E2%%80%%9330/)",
86+
apiDowntimeEnd.Format(time.RFC3339),
87+
),
88+
})
89+
} else {
90+
g, gCtx := errgroup.WithContext(c.Request().Context())
8291

83-
g.Go(func() error {
84-
req, err := http.NewRequestWithContext(gCtx, http.MethodGet, "https://api.guildwars2.com/v2.json", nil)
85-
if err != nil {
86-
return echo.NewHTTPError(http.StatusInternalServerError)
87-
}
92+
var disabledEndpoints []string
93+
var endpointsWithIssues []string
8894

89-
req.Header.Set("User-Agent", "GW2Auth")
95+
g.Go(func() error {
96+
req, err := http.NewRequestWithContext(gCtx, http.MethodGet, "https://api.guildwars2.com/v2.json", nil)
97+
if err != nil {
98+
return echo.NewHTTPError(http.StatusInternalServerError)
99+
}
90100

91-
res, err := httpClient.Do(req)
92-
if err != nil {
93-
return echo.NewHTTPError(http.StatusBadGateway, err)
94-
}
101+
req.Header.Set("User-Agent", "GW2Auth")
95102

96-
defer res.Body.Close()
103+
res, err := httpClient.Do(req)
104+
if err != nil {
105+
return echo.NewHTTPError(http.StatusBadGateway, err)
106+
}
97107

98-
if res.StatusCode != http.StatusOK {
99-
return echo.NewHTTPError(http.StatusBadGateway, fmt.Errorf("unexpected status code: %d", res.StatusCode))
100-
}
108+
defer res.Body.Close()
101109

102-
var gw2Status gw2StatusResponse
103-
if err = json.NewDecoder(res.Body).Decode(&gw2Status); err != nil {
104-
return echo.NewHTTPError(http.StatusBadGateway, err)
105-
}
110+
if res.StatusCode != http.StatusOK {
111+
return echo.NewHTTPError(http.StatusBadGateway, fmt.Errorf("unexpected status code: %d", res.StatusCode))
112+
}
106113

107-
for _, element := range gw2Status.Routes {
108-
if slices.Contains(relevantEndpoints, element.Path) && !element.Active {
109-
disabledEndpoints = append(disabledEndpoints, element.Path)
114+
var gw2Status gw2StatusResponse
115+
if err = json.NewDecoder(res.Body).Decode(&gw2Status); err != nil {
116+
return echo.NewHTTPError(http.StatusBadGateway, err)
110117
}
111-
}
112118

113-
return nil
114-
})
119+
for _, element := range gw2Status.Routes {
120+
if slices.Contains(relevantEndpoints, element.Path) && !element.Active {
121+
disabledEndpoints = append(disabledEndpoints, element.Path)
122+
}
123+
}
115124

116-
g.Go(func() error {
117-
req, err := http.NewRequestWithContext(gCtx, http.MethodGet, "https://status.gw2efficiency.com/api", nil)
118-
if err != nil {
119-
return echo.NewHTTPError(http.StatusInternalServerError)
120-
}
125+
return nil
126+
})
121127

122-
req.Header.Set("User-Agent", "GW2Auth")
128+
g.Go(func() error {
129+
req, err := http.NewRequestWithContext(gCtx, http.MethodGet, "https://status.gw2efficiency.com/api", nil)
130+
if err != nil {
131+
return echo.NewHTTPError(http.StatusInternalServerError)
132+
}
123133

124-
res, err := httpClient.Do(req)
125-
if err != nil {
126-
return echo.NewHTTPError(http.StatusBadGateway, err)
127-
}
134+
req.Header.Set("User-Agent", "GW2Auth")
128135

129-
defer res.Body.Close()
136+
res, err := httpClient.Do(req)
137+
if err != nil {
138+
return echo.NewHTTPError(http.StatusBadGateway, err)
139+
}
130140

131-
if res.StatusCode != http.StatusOK {
132-
return echo.NewHTTPError(http.StatusBadGateway, fmt.Errorf("unexpected status code: %d", res.StatusCode))
133-
}
141+
defer res.Body.Close()
134142

135-
var gw2EffStatus gw2EffApiStatusResponse
136-
if err = json.NewDecoder(res.Body).Decode(&gw2EffStatus); err != nil {
137-
return echo.NewHTTPError(http.StatusBadGateway, err)
138-
}
139-
140-
for _, element := range gw2EffStatus.Data {
141-
if slices.Contains(relevantEndpoints, element.Name) && (element.Status != http.StatusOK || element.CheckError() || element.Duration >= 15_000) {
142-
endpointsWithIssues = append(endpointsWithIssues, element.Name)
143+
if res.StatusCode != http.StatusOK {
144+
return echo.NewHTTPError(http.StatusBadGateway, fmt.Errorf("unexpected status code: %d", res.StatusCode))
143145
}
144-
}
145146

146-
return nil
147-
})
148-
149-
if err := g.Wait(); err != nil {
150-
var httpErr *echo.HTTPError
151-
if errors.As(err, &httpErr) {
152-
return httpErr
153-
} else {
154-
return echo.NewHTTPError(http.StatusInternalServerError)
155-
}
156-
}
147+
var gw2EffStatus gw2EffApiStatusResponse
148+
if err = json.NewDecoder(res.Body).Decode(&gw2EffStatus); err != nil {
149+
return echo.NewHTTPError(http.StatusBadGateway, err)
150+
}
157151

158-
notifications := make([]notification, 0)
152+
for _, element := range gw2EffStatus.Data {
153+
if slices.Contains(relevantEndpoints, element.Name) && (element.Status != http.StatusOK || element.CheckError() || element.Duration >= 15_000) {
154+
endpointsWithIssues = append(endpointsWithIssues, element.Name)
155+
}
156+
}
159157

160-
if len(disabledEndpoints) > 0 {
161-
notifications = append(notifications, notification{
162-
Type: notificationTypeError,
163-
Header: "The Guild Wars 2 API is unavailable",
164-
Content: "Some of the endpoints used by GW2Auth are currently disabled. This might impact your experience with GW2Auth and Applications using GW2Auth.",
165-
})
166-
} else if len(endpointsWithIssues) > 0 {
167-
notifications = append(notifications, notification{
168-
Type: notificationTypeWarning,
169-
Header: "The Guild Wars 2 API experiences issues right now",
170-
Content: "Some of the endpoints used by GW2Auth appear to be in a degraded state right now. This might impact your experience with GW2Auth and Applications using GW2Auth.",
158+
return nil
171159
})
172-
}
173160

174-
now := time.Now()
175-
if now.After(apiDowntimeStart) && now.Before(apiDowntimeEnd) {
176-
notifications = append(notifications, notification{
177-
Type: notificationTypeError,
178-
Header: "VoE Release - Guild Wars 2 API Disabled",
179-
Content: fmt.Sprintf(
180-
"The Guild Wars 2 API will be disabled until %s to prevent spoilers of Visions of Eternity. While the API is disabled, you will not be able to add or update API Tokens or verify accounts.\nSee: [Guild Wars 2 API Disabled from October 24–30](https://en-forum.guildwars2.com/topic/163562-guild-wars-2-api-disabled-from-october-24%%E2%%80%%9330/)",
181-
apiDowntimeEnd.Format(time.RFC3339),
182-
),
183-
})
161+
if err := g.Wait(); err != nil {
162+
var httpErr *echo.HTTPError
163+
if errors.As(err, &httpErr) {
164+
return httpErr
165+
} else {
166+
return echo.NewHTTPError(http.StatusInternalServerError)
167+
}
168+
}
169+
170+
if len(disabledEndpoints) > 0 {
171+
notifications = append(notifications, notification{
172+
Type: notificationTypeError,
173+
Header: "The Guild Wars 2 API is unavailable",
174+
Content: "Some of the endpoints used by GW2Auth are currently disabled. This might impact your experience with GW2Auth and Applications using GW2Auth.",
175+
})
176+
} else if len(endpointsWithIssues) > 0 {
177+
notifications = append(notifications, notification{
178+
Type: notificationTypeWarning,
179+
Header: "The Guild Wars 2 API experiences issues right now",
180+
Content: "Some of the endpoints used by GW2Auth appear to be in a degraded state right now. This might impact your experience with GW2Auth and Applications using GW2Auth.",
181+
})
182+
}
184183
}
185184

186185
return c.JSON(http.StatusOK, notifications)

0 commit comments

Comments
 (0)