mirror of
https://github.com/aljazceru/khatru.git
synced 2026-02-10 15:34:21 +01:00
feat(blossom): use rs/cors to handle BUD-01 CORS
This commit is contained in:
@@ -2,6 +2,7 @@ package blossom
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/rs/cors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -31,43 +32,37 @@ func New(rl *khatru.Relay, serviceURL string) *BlossomServer {
|
||||
|
||||
base := rl.Router()
|
||||
mux := http.NewServeMux()
|
||||
blossomApi := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
blossomApi.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "OPTIONS" {
|
||||
setCors(w)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/upload" {
|
||||
if r.Method == "PUT" {
|
||||
setCors(w)
|
||||
bs.handleUpload(w, r)
|
||||
return
|
||||
} else if r.Method == "HEAD" {
|
||||
setCors(w)
|
||||
bs.handleUploadCheck(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/list/") && r.Method == "GET" {
|
||||
setCors(w)
|
||||
bs.handleList(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if len(strings.SplitN(r.URL.Path, ".", 2)[0]) == 65 {
|
||||
if r.Method == "HEAD" {
|
||||
setCors(w)
|
||||
bs.handleHasBlob(w, r)
|
||||
return
|
||||
} else if r.Method == "GET" {
|
||||
setCors(w)
|
||||
bs.handleGetBlob(w, r)
|
||||
return
|
||||
} else if r.Method == "DELETE" {
|
||||
setCors(w)
|
||||
bs.handleDelete(w, r)
|
||||
return
|
||||
}
|
||||
@@ -76,6 +71,13 @@ func New(rl *khatru.Relay, serviceURL string) *BlossomServer {
|
||||
base.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
bud01corsHeaders := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "PUT", "DELETE"},
|
||||
AllowedHeaders: []string{"Authorization", "*"},
|
||||
})
|
||||
|
||||
mux.Handle("/", bud01corsHeaders.Handler(blossomApi))
|
||||
rl.SetRouter(mux)
|
||||
|
||||
return bs
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func setCors(w http.ResponseWriter) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, DELETE, OPTIONS")
|
||||
}
|
||||
//func setCors(w http.ResponseWriter) {
|
||||
// w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
// w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
// w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, DELETE, OPTIONS")
|
||||
//}
|
||||
|
||||
func blossomError(w http.ResponseWriter, msg string, code int) {
|
||||
w.Header().Add("X-Reason", msg)
|
||||
|
||||
Reference in New Issue
Block a user