Skip to content

Commit

Permalink
nodes: add CORS headers
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Aug 19, 2024
1 parent 2fa4104 commit 28f4118
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ ENV PGID=0

# copy binary and prepare data dir.
COPY --from=builder /app/bin/* /usr/bin/
VOLUME [ "/data" ]

EXPOSE 3000
EXPOSE 3001/tcp

USER ${PUID}:${PGID}

Expand Down
9 changes: 8 additions & 1 deletion nodes/hostd.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ func (m *Manager) StartHostd(ctx context.Context, ready chan<- struct{}) error {

server := http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
} else if strings.HasPrefix(r.URL.Path, "/api") {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
api.ServeHTTP(w, r)
return
Expand Down
9 changes: 8 additions & 1 deletion nodes/renterd.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ func (m *Manager) StartRenterd(ctx context.Context, ready chan<- struct{}) error
var workerHandler, busHandler, autopilotHandler http.Handler
server := &http.Server{
Handler: jape.BasicAuth("sia is cool")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/worker") {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
} else if strings.HasPrefix(r.URL.Path, "/api/worker") {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api/worker")
workerHandler.ServeHTTP(w, r)
return
Expand Down
9 changes: 8 additions & 1 deletion nodes/walletd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ func (m *Manager) StartWalletd(ctx context.Context, ready chan<- struct{}) error
api := jape.BasicAuth("sia is cool")(api.NewServer(cm, s, wm, api.WithLogger(log.Named("api"))))
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/") {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
} else if strings.HasPrefix(r.URL.Path, "/api/") {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
api.ServeHTTP(w, r)
return
Expand Down

0 comments on commit 28f4118

Please sign in to comment.