fix: Add CORS Option

This commit is contained in:
DullJZ
2026-01-31 20:20:48 +08:00
parent 9738091c87
commit d9258d4924
2 changed files with 10 additions and 1 deletions

View File

@@ -1 +1 @@
v0.2.3
v0.2.4

View File

@@ -1,6 +1,7 @@
package api
import (
"net/http"
"sync/atomic"
"github.com/DullJZ/s3-balance/internal/balancer"
@@ -73,6 +74,7 @@ func (h *S3Handler) initSettings(accessKey, secretKey string, proxyMode, authReq
func (h *S3Handler) RegisterS3Routes(router *mux.Router) {
// 公共路由(不需要认证)
router.HandleFunc("/", h.handleListBuckets).Methods("GET")
router.HandleFunc("/", h.handleOptions).Methods("OPTIONS")
// 带认证/虚拟主机的路由
protected := router.NewRoute().PathPrefix("/{bucket}").Subrouter()
@@ -80,7 +82,9 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) {
// Bucket operations
protected.HandleFunc("", h.handleBucketOperations).Methods("GET", "HEAD", "PUT", "DELETE")
protected.HandleFunc("", h.handleOptions).Methods("OPTIONS")
protected.HandleFunc("/", h.handleBucketOperations).Methods("GET", "HEAD", "PUT", "DELETE")
protected.HandleFunc("/", h.handleOptions).Methods("OPTIONS")
// Multipart upload operations - must be registered before generic object operations
protected.HandleFunc("/{key:.*}", h.handleUploadPart).Methods("PUT").Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId}")
@@ -92,6 +96,7 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) {
// Object operations - must be registered after multipart operations to avoid conflicts
protected.HandleFunc("/{key:.*}", h.handleObjectOperations).Methods("GET", "HEAD", "PUT", "DELETE")
protected.HandleFunc("/{key:.*}", h.handleOptions).Methods("OPTIONS")
// 添加中间件
protected.Use(h.accessLogMiddleware)
@@ -110,6 +115,10 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) {
}))
}
func (h *S3Handler) handleOptions(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
func (h *S3Handler) loadSettings() handlerSettings {
if v := h.settings.Load(); v != nil {
return v.(handlerSettings)