Skip to content

Commit 18252c0

Browse files
committed
Addressing comments
1 parent eae4452 commit 18252c0

14 files changed

Lines changed: 349 additions & 306 deletions

File tree

api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/go-chi/chi/v5"
77
)
88

9-
func Router(steamWebAPIKey string) chi.Router {
9+
func Router() chi.Router {
1010
r := chi.NewRouter()
11-
r.Mount("/v1", v1.Router(steamWebAPIKey))
11+
r.Mount("/v1", v1.Router())
1212
return r
1313
}

api/v1/steam/resolve.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package steam
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"reverse-watch/domain/models"
8+
"reverse-watch/errors"
9+
"reverse-watch/middleware"
10+
"reverse-watch/render"
11+
)
12+
13+
type resolveSteamIDResponse struct {
14+
SteamID models.SteamID `json:"steam_id"`
15+
}
16+
17+
func resolveSteamID() http.HandlerFunc {
18+
return func(w http.ResponseWriter, r *http.Request) {
19+
svc, ok := middleware.SteamServiceFromContext(r.Context())
20+
if !ok || svc == nil {
21+
render.Errorf(w, r, errors.InternalServerError, "steam service not configured")
22+
return
23+
}
24+
25+
raw := strings.TrimSpace(r.URL.Query().Get("input"))
26+
if raw == "" {
27+
// Backwards-compatible alias in case the caller uses a different name.
28+
raw = strings.TrimSpace(r.URL.Query().Get("steam"))
29+
}
30+
if raw == "" {
31+
render.Errorf(w, r, errors.BadRequest, "missing input")
32+
return
33+
}
34+
35+
id, err := svc.ResolveSteamID(r.Context(), raw)
36+
if err != nil {
37+
render.Errorf(w, r, errors.BadRequest, "could not resolve steam id")
38+
return
39+
}
40+
41+
render.JSON(w, r, &resolveSteamIDResponse{SteamID: *id})
42+
}
43+
}
44+

api/v1/steam/router.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package steam
2+
3+
import (
4+
"time"
5+
6+
"reverse-watch/ratelimit"
7+
8+
"github.com/go-chi/chi/v5"
9+
)
10+
11+
func Router() chi.Router {
12+
r := chi.NewRouter()
13+
r.With(ratelimit.ThrottleByIP(time.Minute, 100)).Get("/resolve", resolveSteamID())
14+
return r
15+
}
16+

api/v1/users/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/go-chi/chi/v5"
99
)
1010

11-
func Router(steamWebAPIKey string) chi.Router {
11+
func Router() chi.Router {
1212
r := chi.NewRouter()
13-
r.With(ratelimit.ThrottleByIP(time.Minute, 100)).Get("/{steamId}", fetchUserStatus(steamWebAPIKey))
13+
r.With(ratelimit.ThrottleByIP(time.Minute, 100)).Get("/{steamId}", fetchUserStatus())
1414
return r
1515
}

api/v1/users/users.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package users
22

33
import (
4-
"context"
54
"net/http"
6-
"strings"
7-
"time"
85

96
"reverse-watch/domain/dto"
107
"reverse-watch/domain/models"
@@ -22,25 +19,12 @@ type fetchUserStatusResponse struct {
2219
LastReversalTimestamp *uint64 `json:"last_reversal_timestamp,omitempty"`
2320
}
2421

25-
func fetchUserStatus(steamWebAPIKey string) http.HandlerFunc {
22+
func fetchUserStatus() http.HandlerFunc {
2623
return func(w http.ResponseWriter, r *http.Request) {
2724
factory := r.Context().Value(middleware.FactoryContextKey).(repository.Factory)
2825

2926
steamIdStr := chi.URLParam(r, "steamId")
30-
client := &http.Client{Timeout: 15 * time.Second}
31-
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
32-
defer cancel()
33-
34-
var steamId *models.SteamID
35-
var err error
36-
if strings.TrimSpace(steamWebAPIKey) != "" {
37-
steamId, err = models.ParseSteamUserInputWithOpts(ctx, client, steamIdStr, &models.SteamUserInputOpts{
38-
UseWebAPIForVanity: true,
39-
SteamWebAPIKey: steamWebAPIKey,
40-
})
41-
} else {
42-
steamId, err = models.ParseSteamUserInput(ctx, client, steamIdStr)
43-
}
27+
steamId, err := models.ToSteamID(steamIdStr)
4428
if err != nil {
4529
render.Errorf(w, r, errors.BadRequest, "invalid steam id")
4630
return

api/v1/v1.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import (
55
"reverse-watch/api/v1/health"
66
"reverse-watch/api/v1/marketplace"
77
"reverse-watch/api/v1/reversals"
8+
"reverse-watch/api/v1/steam"
89
"reverse-watch/api/v1/users"
910

1011
"github.com/go-chi/chi/v5"
1112
)
1213

13-
func Router(steamWebAPIKey string) chi.Router {
14+
func Router() chi.Router {
1415
r := chi.NewRouter()
1516
r.Mount("/health", health.Router())
1617
r.Mount("/marketplace", marketplace.Router())
1718
r.Mount("/reversals", reversals.Router())
18-
r.Mount("/users", users.Router(steamWebAPIKey))
19+
r.Mount("/users", users.Router())
20+
r.Mount("/steam", steam.Router())
1921
r.Mount("/admin", admin.Router())
2022
return r
2123
}

config.example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
},
1414
"Environment": "development",
1515
"Steam": {
16-
"WebAPIKey": ""
16+
"WebAPIKey": [""]
1717
}
1818
}

config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type Config struct {
4141
}
4242
}
4343

44-
// Steam.WebAPIKey is optional. If set, /api/v1/users resolves /id/{vanity} via the Web API
45-
// instead of the default ?xml=1 request to the community site.
44+
// Steam.WebAPIKey is optional. If set, /api/v1/steam/resolve can use the Steam Web API
45+
// (keys are rotated) instead of the default ?xml=1 request to the community site.
4646
Steam struct {
47-
WebAPIKey string
47+
WebAPIKey []string
4848
}
4949
}
5050

domain/models/steamid_input_test.go

Lines changed: 0 additions & 217 deletions
This file was deleted.

0 commit comments

Comments
 (0)