Skip to content

Commit

Permalink
api: Add storj failing proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Jun 27, 2024
1 parent 69c3cc5 commit 4cc41f3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package api
import (
"context"
"encoding/json"
"math/rand/v2"
"net/http"
"net/http/httputil"
"path"
"strings"
"time"

"github.com/golang/glog"
Expand Down Expand Up @@ -38,6 +42,35 @@ func NewHandler(serverCtx context.Context, opts APIHandlerOptions, runner task.R
hookHandler = authorized(opts.Catalyst.Secret, hookHandler)
hookHandler = logger(hookHandler)
router.Handler("POST", clients.CatalystHookPath(opts.APIRoot, ":id"), hookHandler)

proxyPath := path.Join(opts.APIRoot, "/proxy/os")
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = "gateway.storjshare.io"
req.URL.Path = strings.TrimPrefix(req.URL.Path, proxyPath)
},
}
router.NotFound = logger(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/") {
r.URL.Path = "/" + r.URL.Path
}
if !strings.HasPrefix(r.URL.Path, proxyPath) {
http.NotFound(w, r)
return
}

errorChance := float32(0.5)
if r.Method != "GET" {
errorChance = 0.95
}
if rand.Float32() < errorChance {
glog.Errorf("Random error for path=%s", r.URL.Path)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
proxy.ServeHTTP(w, r)
}))
return router
}

Expand Down

0 comments on commit 4cc41f3

Please sign in to comment.