Skip to content

Commit 2556999

Browse files
committed
feat: support base path with unified request interface
1 parent 3b1431e commit 2556999

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

cmd/lambda/main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ func APIGatewayHandler(ctx context.Context, req awsevents.APIGatewayV2HTTPReques
5050
logger.Debug("received api gateway request", slog.String("request", string(j)))
5151
}
5252

53-
path := req.RawPath
54-
if appInst.Config.BasePath != "" {
55-
path = strings.TrimPrefix(path, appInst.Config.BasePath)
56-
if path == "" {
57-
path = "/"
58-
}
59-
}
60-
6153
headers := make(map[string]string)
6254
for key, value := range req.Headers {
6355
headers[strings.ToLower(key)] = value
@@ -66,7 +58,7 @@ func APIGatewayHandler(ctx context.Context, req awsevents.APIGatewayV2HTTPReques
6658
appReq := app.Request{
6759
Type: app.RequestTypeHTTP,
6860
Method: req.RequestContext.HTTP.Method,
69-
Path: path,
61+
Path: req.RawPath,
7062
Headers: headers,
7163
Body: []byte(req.Body),
7264
}

internal/app/request.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"log/slog"
8+
"strings"
89

910
"github.com/cruxstack/github-ops-app/internal/github"
1011
)
@@ -82,17 +83,27 @@ func (a *App) handleScheduledRequest(ctx context.Context, req Request) Response
8283
}
8384

8485
// handleHTTPRequest routes HTTP requests based on path.
86+
// strips BasePath prefix if configured (e.g., "/api/v1" -> "/").
8587
func (a *App) handleHTTPRequest(ctx context.Context, req Request) Response {
86-
switch req.Path {
88+
path := req.Path
89+
if a.Config.BasePath != "" {
90+
path = strings.TrimPrefix(path, a.Config.BasePath)
91+
if path == "" {
92+
path = "/"
93+
}
94+
}
95+
96+
switch path {
8797
case "/server/status":
8898
return a.handleStatusRequest(req)
8999
case "/server/config":
90100
return a.handleConfigRequest(req)
91101
case "/webhooks", "/":
92102
return a.handleWebhookRequest(ctx, req)
93-
case "/scheduled/okta-sync":
94-
return a.handleScheduledHTTPRequest(ctx, req)
95103
default:
104+
if strings.HasPrefix(path, "/scheduled/") {
105+
return a.handleScheduledHTTPRequest(ctx, req, path)
106+
}
96107
return errorResponse(404, "not found")
97108
}
98109
}
@@ -147,15 +158,16 @@ func (a *App) handleWebhookRequest(ctx context.Context, req Request) Response {
147158
}
148159

149160
// handleScheduledHTTPRequest processes scheduled events via HTTP POST.
150-
func (a *App) handleScheduledHTTPRequest(ctx context.Context, req Request) Response {
161+
// path is the normalized path with BasePath already stripped.
162+
func (a *App) handleScheduledHTTPRequest(ctx context.Context, req Request, path string) Response {
151163
if req.Method != "POST" {
152164
return errorResponse(405, "method not allowed")
153165
}
154166

155167
// extract action from path (e.g., "/scheduled/okta-sync" -> "okta-sync")
156-
action := "okta-sync"
157-
if len(req.Path) > len("/scheduled/") {
158-
action = req.Path[len("/scheduled/"):]
168+
action := strings.TrimPrefix(path, "/scheduled/")
169+
if action == "" {
170+
return errorResponse(400, "missing scheduled action")
159171
}
160172

161173
scheduledReq := Request{

0 commit comments

Comments
 (0)