This is an automated email from the ASF dual-hosted git repository.
bsalzano pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openserverless-streamer.git
The following commit(s) were added to refs/heads/main by this push:
new 27cf4f4 feat: cors support
27cf4f4 is described below
commit 27cf4f4bc47a6a2c50b338b2a1a72cc95adf022d
Author: Bruno Salzano <[email protected]>
AuthorDate: Sat May 3 13:21:08 2025 +0200
feat: cors support
added cors support
---
README.md | 13 ++++++++++---
Taskfile.yml | 11 +++++++++++
src/httpserver.go | 44 ++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index 390634e..c5a80a3 100644
--- a/README.md
+++ b/README.md
@@ -28,12 +28,18 @@ socket for the action to write to, and relay the output to
the client.
It expects 2 environment variables to be set:
- `OW_APIHOST`: the OpenWhisk API host
-- `STREAMER_ADDR`: the address of the streamer server for the OpenWhisk
actions
- to connect to
+- `STREAMER_ADDR`: the address of the streamer server for the OpenWhisk
actions to connect to
Other environment variables can be set to configure the streamer:
- `HTTP_SERVER_PORT`: the port the streamer server listens on (default: 80)
+
+Cors handling is handled through these variables:
+
+- `CORS_ENABLED`: set to 1 or true to enable the CORS handler
+- `CORS_ALLOW_ORIGIN`: this defaults to `*`
+- `CORS_ALLOW_METHODS`: this defaults to `GET,POST,OPTIONS`
+- `CORS_ALLOW_HEADERS`: this defaults to `Authorization,Content-Type`
## Endpoints
@@ -61,7 +67,8 @@ Taskfile supports the following tasks:
```yaml
* build: Build the streamer binary locally. This will create a
binary named streamer in the current directory.
-* buildx: Build the docker image using buildx. Set PUSH=1 to push
the image to the registry.
+* buildx: Build the docker image using buildx. Set PUSH=1 to push
the image to the registry.
+* clean: Clean up the build artifacts. This will remove the
streamer binary and clean the go cache.
* docker-login: Login to the docker registry. Set REGISTRY=ghcr or
REGISTRY=dockerhub in .env to use the respective registry.
* image-tag: Create a new tag for the current git commit.
* run: Run the streamer binary locally, using configuration
from .env file
diff --git a/Taskfile.yml b/Taskfile.yml
index a1cf476..b6ae333 100644
--- a/Taskfile.yml
+++ b/Taskfile.yml
@@ -131,6 +131,17 @@ tasks:
fi
cd ../
+ clean:
+ desc: |
+ Clean up the build artifacts. This will remove the streamer binary and
clean the go cache.
+ cmds:
+ - |
+ rm -f streamer || true
+ cd src
+ go clean -cache -modcache -testcache -fuzzcache
+ cd ../
+ echo "Cleaned up build artifacts."
+
run:
desc: |
Run the streamer binary locally, using configuration from .env file
diff --git a/src/httpserver.go b/src/httpserver.go
index 1d41c62..b045423 100644
--- a/src/httpserver.go
+++ b/src/httpserver.go
@@ -25,6 +25,38 @@ import (
"github.com/apache/openserverless-streaming-proxy/handlers"
)
+func corsMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
+ // Set CORS headers
+ allowOrigin := os.Getenv("CORS_ALLOW_ORIGIN")
+ if allowOrigin == "" {
+ allowOrigin = "*"
+ }
+ w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
+
+ allowMethods := os.Getenv("CORS_ALLOW_METHODS")
+ if allowMethods == "" {
+ allowMethods = "GET, POST, OPTIONS"
+ }
+ w.Header().Set("Access-Control-Allow-Methods", allowMethods)
+
+ allowHeaders := os.Getenv("CORS_ALLOW_HEADERS")
+ if allowHeaders == "" {
+ allowHeaders = "*"
+ }
+ w.Header().Set("Access-Control-Allow-Headers", allowHeaders)
+
+ // Handle preflight request
+ if r.Method == http.MethodOptions {
+ w.WriteHeader(http.StatusNoContent)
+ return
+ }
+
+ next.ServeHTTP(w, r)
+ })
+}
+
func startHTTPServer(streamingProxyAddr string, apihost string) {
httpPort := os.Getenv("HTTP_SERVER_PORT")
if httpPort == "" {
@@ -47,9 +79,17 @@ func startHTTPServer(streamingProxyAddr string, apihost
string) {
router.HandleFunc("POST /action/{ns}/{action}",
handlers.ActionStreamHandler(streamingProxyAddr, apihost))
router.HandleFunc("POST /action/{ns}/{pkg}/{action}",
handlers.ActionStreamHandler(streamingProxyAddr, apihost))
+ corsEnabled := os.Getenv("CORS_ENABLED")
+ useCors := corsEnabled == "1" || corsEnabled == "true"
+
server := &http.Server{
- Addr: ":" + httpPort,
- Handler: router,
+ Addr: ":" + httpPort,
+ Handler: func() http.Handler {
+ if useCors {
+ return corsMiddleware(router)
+ }
+ return router
+ }(),
}
log.Println("HTTP server listening on port", httpPort)