Skip to content

Commit

Permalink
Merge pull request #8 from lukaszbudnik/cors-support
Browse files Browse the repository at this point in the history
added CORS support
  • Loading branch information
lukaszbudnik committed Feb 16, 2021
2 parents b3fa7f0 + bb07fc7 commit f8fab8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ type response struct {
var counter = 0
var hostname = os.Getenv("HOSTNAME")

func preflight(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Expose-Headers", "*")
w.Header().Set("Access-Control-Max-Age", "600")
w.WriteHeader(http.StatusOK)
}

func handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")

showEnvs := os.Getenv("YOSOY_SHOW_ENVS")
showFiles := os.Getenv("YOSOY_SHOW_FILES")

Expand Down Expand Up @@ -80,7 +92,8 @@ func main() {
r := mux.NewRouter()

r.Handle("/favicon.ico", r.NotFoundHandler)
r.PathPrefix("/").HandlerFunc(handler)
r.PathPrefix("/").HandlerFunc(preflight).Methods(http.MethodOptions)
r.PathPrefix("/").HandlerFunc(handler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete, http.MethodConnect, http.MethodHead, http.MethodTrace)

loggingRouter := handlers.CombinedLoggingHandler(os.Stdout, r)
proxyRouter := handlers.ProxyHeaders(loggingRouter)
Expand Down
4 changes: 4 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ func TestHandler(t *testing.T) {
var response response
json.Unmarshal(rr.Body.Bytes(), &response)

// test response
assert.Equal(t, 1, response.Counter)
assert.Equal(t, "example.org", response.Host)
assert.NotEmpty(t, response.EnvVariables)
assert.NotEmpty(t, response.Files[".gitignore"])

// test cors
assert.Contains(t, rr.HeaderMap["Access-Control-Allow-Origin"], "*")
}

0 comments on commit f8fab8d

Please sign in to comment.