Skip to content

Commit 2f8b89e

Browse files
committed
fix: don't return 404 for API base endpoints if they exist
1 parent 5332f2d commit 2f8b89e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

pkg/run/gateway.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ func (s *BaseHttpGateway) api(ctx *fasthttp.RequestCtx) {
5454
apiName := ctx.UserValue("name").(string)
5555
// Rewrite the URL of the request to remove the /api/{name} subroute
5656
pathParts := nitric_utils.SplitPath(string(ctx.Path()))
57+
58+
// When a request is made to the base API path, return status info
59+
if len(pathParts) == 2 {
60+
w := s.pool.GetWorkers(&worker.GetWorkerOptions{
61+
Filter: apiWorkerFilter(apiName),
62+
})
63+
ctx.Response.SetBody([]byte(fmt.Sprintf("<html><body><strong>%d</strong> active route(s) detected for API %v.</body></html>", len(w), apiName)))
64+
if len(w) > 0 {
65+
ctx.Response.SetStatusCode(200)
66+
} else {
67+
ctx.Response.SetStatusCode(404)
68+
}
69+
return
70+
}
71+
5772
// remove first two path parts
5873
newPathParts := pathParts[2:]
5974

@@ -124,9 +139,9 @@ func (s *BaseHttpGateway) Start(pool worker.WorkerPool) error {
124139

125140
// Setup routes
126141
r := router.New()
127-
// Make a request for an API gateway
128-
r.ANY("/apis/{name}/{any:*}", s.api)
129-
// trigger a topic
142+
// Make a request to an API gateway
143+
r.ANY("/apis/{name}/{any?:*}", s.api)
144+
// Publish to a topic
130145
r.POST("/topic/{name}", s.topic)
131146

132147
s.server = &fasthttp.Server{

0 commit comments

Comments
 (0)