Skip to content

Commit

Permalink
Merge pull request #6 from bparees/cors
Browse files Browse the repository at this point in the history
handle CORS preflight requests
  • Loading branch information
bparees authored Aug 25, 2023
2 parents 0872288 + f4631cc commit b7ceef2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/server/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ func (h *Handler) HandleApiToken(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, url.String(), http.StatusFound)
return
}

w.Header().Set("Content-Type", "text/json")

apiToken := api.APIToken{
Token: tokenString,
}
buf := bytes.Buffer{}
err = json.NewEncoder(&buf).Encode(apiToken)
if err != nil {
log.Errorf("failed to encode token: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/json")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/inference_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"

"github.com/labstack/gommon/log"
"github.com/openshift/wisdom/pkg/api"
"github.com/openshift/wisdom/pkg/model"
)
Expand All @@ -18,6 +19,9 @@ func (h *Handler) CORSHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) InferHandler(w http.ResponseWriter, r *http.Request) {

http.Header.Add(w.Header(), "Access-Control-Allow-Origin", "*")

if !h.hasValidBearerToken(r) {
http.Error(w, "No valid bearer token found", http.StatusUnauthorized)
return
Expand Down Expand Up @@ -56,6 +60,7 @@ func (h *Handler) InferHandler(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "text/json")
if err != nil || (response != nil && response.ErrorMessage != "") {
log.Debugf("model invocation returning error: %v", err)
w.WriteHeader(http.StatusExpectationFailed)
} else {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit b7ceef2

Please sign in to comment.