mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-12 05:54:25 +00:00
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -27m44s
Build , Vet Test, and Lint / Lint Code (push) Successful in -27m5s
Build , Vet Test, and Lint / Build (push) Successful in -27m29s
Tests / Unit Tests (push) Successful in -27m48s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in 2m22s
Tests / Integration Tests (push) Failing after -28m1s
* Change loop to use index for server instances * Simplify appending external URLs * Clean up commented code for clarity
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/bitechdev/ResolveSpec/pkg/config"
|
|
)
|
|
|
|
// CORSConfig holds CORS configuration
|
|
type CORSConfig struct {
|
|
AllowedOrigins []string
|
|
AllowedMethods []string
|
|
AllowedHeaders []string
|
|
MaxAge int
|
|
}
|
|
|
|
// DefaultCORSConfig returns a default CORS configuration suitable for HeadSpec
|
|
func DefaultCORSConfig() CORSConfig {
|
|
configManager := config.GetConfigManager()
|
|
cfg, _ := configManager.GetConfig()
|
|
hosts := make([]string, 0)
|
|
// hosts = append(hosts, "*")
|
|
|
|
_, _, ipsList := config.GetIPs()
|
|
|
|
for i := range cfg.Servers.Instances {
|
|
server := cfg.Servers.Instances[i]
|
|
hosts = append(hosts, fmt.Sprintf("http://%s:%d", server.Host, server.Port))
|
|
hosts = append(hosts, fmt.Sprintf("https://%s:%d", server.Host, server.Port))
|
|
hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port))
|
|
hosts = append(hosts, server.ExternalURLs...)
|
|
for _, ip := range ipsList {
|
|
hosts = append(hosts, fmt.Sprintf("http://%s:%d", ip.String(), server.Port))
|
|
hosts = append(hosts, fmt.Sprintf("https://%s:%d", ip.String(), server.Port))
|
|
}
|
|
}
|
|
|
|
return CORSConfig{
|
|
AllowedOrigins: hosts,
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
|
AllowedHeaders: GetHeadSpecHeaders(),
|
|
MaxAge: 86400, // 24 hours
|
|
}
|
|
}
|
|
|
|
// GetHeadSpecHeaders returns all headers used by HeadSpec
|
|
func GetHeadSpecHeaders() []string {
|
|
return []string{
|
|
// Standard headers
|
|
"Content-Type",
|
|
"Authorization",
|
|
"Accept",
|
|
"Accept-Language",
|
|
"Content-Language",
|
|
|
|
// Field Selection
|
|
"X-Select-Fields",
|
|
"X-Not-Select-Fields",
|
|
"X-Clean-JSON",
|
|
|
|
// Filtering & Search
|
|
"X-FieldFilter-*",
|
|
"X-SearchFilter-*",
|
|
"X-SearchOp-*",
|
|
"X-SearchOr-*",
|
|
"X-SearchAnd-*",
|
|
"X-SearchCols",
|
|
"X-Custom-SQL-W",
|
|
"X-Custom-SQL-W-*",
|
|
"X-Custom-SQL-Or",
|
|
"X-Custom-SQL-Or-*",
|
|
|
|
// Joins & Relations
|
|
"X-Preload",
|
|
"X-Preload-*",
|
|
"X-Expand",
|
|
"X-Expand-*",
|
|
"X-Custom-SQL-Join",
|
|
"X-Custom-SQL-Join-*",
|
|
|
|
// Sorting & Pagination
|
|
"X-Sort",
|
|
"X-Sort-*",
|
|
"X-Limit",
|
|
"X-Offset",
|
|
"X-Cursor-Forward",
|
|
"X-Cursor-Backward",
|
|
|
|
// Advanced Features
|
|
"X-AdvSQL-*",
|
|
"X-CQL-Sel-*",
|
|
"X-Distinct",
|
|
"X-SkipCount",
|
|
"X-SkipCache",
|
|
"X-Fetch-RowNumber",
|
|
"X-PKRow",
|
|
|
|
// Response Format
|
|
"X-SimpleAPI",
|
|
"X-DetailAPI",
|
|
"X-Syncfusion",
|
|
"X-Single-Record-As-Object",
|
|
|
|
// Transaction Control
|
|
"X-Transaction-Atomic",
|
|
|
|
// X-Files - comprehensive JSON configuration
|
|
"X-Files",
|
|
}
|
|
}
|
|
|
|
// SetCORSHeaders sets CORS headers on a response writer
|
|
func SetCORSHeaders(w ResponseWriter, config CORSConfig) {
|
|
// Set allowed origins
|
|
if len(config.AllowedOrigins) > 0 {
|
|
w.SetHeader("Access-Control-Allow-Origin", strings.Join(config.AllowedOrigins, ", "))
|
|
}
|
|
|
|
// Set allowed methods
|
|
if len(config.AllowedMethods) > 0 {
|
|
w.SetHeader("Access-Control-Allow-Methods", strings.Join(config.AllowedMethods, ", "))
|
|
}
|
|
|
|
// Set allowed headers
|
|
if len(config.AllowedHeaders) > 0 {
|
|
w.SetHeader("Access-Control-Allow-Headers", strings.Join(config.AllowedHeaders, ", "))
|
|
}
|
|
|
|
// Set max age
|
|
if config.MaxAge > 0 {
|
|
w.SetHeader("Access-Control-Max-Age", fmt.Sprintf("%d", config.MaxAge))
|
|
}
|
|
|
|
// Allow credentials
|
|
w.SetHeader("Access-Control-Allow-Credentials", "true")
|
|
|
|
// Expose headers that clients can read
|
|
w.SetHeader("Access-Control-Expose-Headers", "Content-Range, X-Api-Range-Total, X-Api-Range-Size")
|
|
}
|