Skip to content

Commit

Permalink
handle CORS preflight requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bparees committed Aug 25, 2023
1 parent 5bbf046 commit f4631cc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/wisdom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func newStartServerCommand() *cobra.Command {
}

r.HandleFunc("/infer", h.InferHandler).Methods("POST")
r.HandleFunc("/infer", h.CORSHandler).Methods("OPTIONS")
//r.HandleFunc("/feedback", h.FeedbackHandler).Methods("POST")
r.HandleFunc("/login", h.HandleLogin)
r.HandleFunc("/githubcallback", h.HandleGithubCallback)
Expand Down
3 changes: 3 additions & 0 deletions pkg/filters/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func MarkdownStripper(response *api.ModelResponse) (*api.ModelResponse, error) {

//response.Output = markdownRegex.ReplaceAllString(response.Output, "")
matches := markdownRegex.FindStringSubmatch(response.Output)
if len(matches) < 2 {
return response, fmt.Errorf("no markdown found in response")
}
response.Output = matches[1]
/*
node := gomarkdown.Parse([]byte(response.Output), nil)
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/invoker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import (
log "github.com/sirupsen/logrus"

"github.com/openshift/wisdom/pkg/api"
"github.com/openshift/wisdom/pkg/filters"
)
Expand All @@ -11,6 +13,7 @@ func InvokeModel(input api.ModelInput, model api.Model, filter filters.Filter) (
if response == nil {
response = &api.ModelResponse{}
}
log.Debugf("model response: %#v", response)
if err != nil {
response.ErrorMessage = err.Error()
return response, err
Expand Down
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
12 changes: 12 additions & 0 deletions pkg/server/inference_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import (
"fmt"
"net/http"

"github.com/labstack/gommon/log"
"github.com/openshift/wisdom/pkg/api"
"github.com/openshift/wisdom/pkg/model"
)

func (h *Handler) CORSHandler(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "Access-Control-Allow-Origin", "*")
http.Header.Add(w.Header(), "Access-Control-Allow-Methods", "GET, POST, OPTIONS")
http.Header.Add(w.Header(), "Access-Control-Allow-Headers", "Content-Type, Authorization")

}

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 @@ -49,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 f4631cc

Please sign in to comment.