Skip to content

Commit

Permalink
Streamline handling of CORS preflight requests.
Browse files Browse the repository at this point in the history
Also shift HTTP method checks into the HandleFunc declaration.
  • Loading branch information
LTLA committed Jan 22, 2025
1 parent 6126f72 commit a29f8d8
Showing 1 changed file with 12 additions and 45 deletions.
57 changes: 12 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ func dumpHttpErrorResponse(w http.ResponseWriter, err error, path string) {
dumpJsonResponse(w, status_code, map[string]interface{}{ "status": "ERROR", "reason": message }, path)
}

func configureCors(w http.ResponseWriter, r *http.Request) bool {
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.WriteHeader(http.StatusNoContent)
return true
} else {
return false
}
}

/***************************************************/

func main() {
Expand Down Expand Up @@ -181,26 +169,12 @@ func main() {
fs := http.FileServer(http.Dir(globals.Registry))
fetch_endpt := endpt_prefix + "/fetch/"
fs_stripped := http.StripPrefix(fetch_endpt, fs)
http.HandleFunc(fetch_endpt, func(w http.ResponseWriter, r *http.Request) {
if configureCors(w, r) {
return
}
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
http.HandleFunc("GET " + fetch_endpt, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fs_stripped.ServeHTTP(w, r)
})

http.HandleFunc(endpt_prefix + "/list", func(w http.ResponseWriter, r *http.Request) {
if configureCors(w, r) {
return
}
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

http.HandleFunc("GET " + endpt_prefix + "/list", func(w http.ResponseWriter, r *http.Request) {
listing, err := listFilesHandler(r, globals.Registry)
if err != nil {
dumpHttpErrorResponse(w, err, "list request")
Expand All @@ -210,28 +184,21 @@ func main() {
})

// Creating some useful endpoints.
http.HandleFunc(endpt_prefix + "/info", func(w http.ResponseWriter, r *http.Request) {
if configureCors(w, r) {
return
}
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
http.HandleFunc("GET " + endpt_prefix + "/info", func(w http.ResponseWriter, r *http.Request) {
dumpJsonResponse(w, http.StatusOK, map[string]string{ "staging": staging, "registry": globals.Registry }, "info request")
})

http.HandleFunc(endpt_prefix + "/", func(w http.ResponseWriter, r *http.Request) {
if configureCors(w, r) {
return
}
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
http.HandleFunc("GET " + endpt_prefix + "/", func(w http.ResponseWriter, r *http.Request) {
dumpJsonResponse(w, http.StatusOK, map[string]string{ "name": "gobbler API", "url": "https://github.com/ArtifactDB/gobbler" }, "default request")
})

http.HandleFunc("OPTIONS /", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.WriteHeader(http.StatusNoContent)
})

// Adding a per-day job that purges various old files.
ticker := time.NewTicker(time.Hour * 24)
defer ticker.Stop()
Expand Down

0 comments on commit a29f8d8

Please sign in to comment.