Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable cors-everywhere on smeshing-service #6586

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions activation_service_poc/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ services:
"-c", "/config.json",
"--node-service-address", "http://node-service:9099",
"--grpc-json-listener", "0.0.0.0:9071",
"--proxy-api-v2-address", "http://node-service:9070"
"--proxy-api-v2-address", "http://node-service:9070",
"--json-cors-everywhere"
]
volumes:
- /tmp/spacemesh-client:/tmp/spacemesh-client
Expand All @@ -19,8 +20,10 @@ services:

node-service:
image: ${IMAGE}
command: ["-c", "/config.json",
"--grpc-json-listener", "0.0.0.0:9070"]
command: [
"-c", "/config.json",
"--grpc-json-listener", "0.0.0.0:9070",
]
volumes:
- /tmp/spacemesh-node-service:/tmp/spacemesh-node-service
- ./config.standalone.node-service.json:/config.json
Expand Down
27 changes: 25 additions & 2 deletions api/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"time"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

Expand All @@ -29,7 +30,7 @@
Path() string
}

func NewServer(proxyListener, apiAddress string, logger *zap.Logger, local ...Service) (*Server, error) {
func NewServer(proxyListener, apiAddress string, corsEverywhere bool, logger *zap.Logger, local ...Service) (*Server, error) {
// Validate the API server URL
targetURL, err := url.Parse(apiAddress)
if err != nil {
Expand All @@ -39,6 +40,28 @@
// Create a reverse proxy
proxy := httputil.NewSingleHostReverseProxy(targetURL)
mux := http.NewServeMux()
var handler http.Handler = mux
if corsEverywhere {
logger.Info("enabling CORS on PROXY for all origins")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{
"Server",
"Date",
"Content-Type",
"Content-Length",
"Connection",
"Vary",
"X-Final-Url",
"Access-Control-Allow-Origin",
},
AllowCredentials: false,
MaxAge: 300,
})
handler = c.Handler(mux)
}

Check warning on line 64 in api/proxy/proxy.go

View check run for this annotation

Codecov / codecov/patch

api/proxy/proxy.go#L45-L64

Added lines #L45 - L64 were not covered by tests

// Register GRPC services handled locally
grpcMux := runtime.NewServeMux()
Expand All @@ -57,7 +80,7 @@
// Initialize the HTTP server
server := &http.Server{
Addr: proxyListener,
Handler: mux,
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
Expand Down
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,7 @@ func (app *App) startAPIServices(ctx context.Context) error {
p, err := proxy.NewServer(
app.Config.API.JSONListener,
app.Config.API.ProxyApiV2Address,
app.Config.API.JSONCorsEverywhere,
logger.Zap(),
localSvcs...,
)
Expand Down
Loading