Skip to content

Commit

Permalink
Improve sashabaranov#356 to allow registration of wildcard URL handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
coggsflod committed Jun 13, 2023
1 parent e72f365 commit d0f40df
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"net/http/httptest"
"regexp"
)

const testAPI = "this-is-my-secure-token-do-not-steal!!"
Expand Down Expand Up @@ -36,11 +37,14 @@ func (ts *ServerTest) OpenAITestServer() *httptest.Server {
return
}

handlerCall, ok := ts.handlers[r.URL.Path]
if !ok {
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
return
// Handle /path/* routes.
for route, handler := range ts.handlers {
pattern, _ := regexp.Compile(route)
if pattern.MatchString(r.URL.Path) {
handler(w, r)
return
}
}
handlerCall(w, r)
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
}))
}

0 comments on commit d0f40df

Please sign in to comment.