Skip to content

Commit f156c6f

Browse files
Fix archiver
1 parent ec2dfce commit f156c6f

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

api/archive.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package api
2+
3+
import (
4+
"math/rand"
5+
"net/http"
6+
"net/http/httputil"
7+
"net/url"
8+
9+
"api.audius.co/config"
10+
"github.com/gofiber/fiber/v2"
11+
"github.com/gofiber/fiber/v2/middleware/adaptor"
12+
)
13+
14+
func archiveProxy(c *fiber.Ctx) error {
15+
// Handle preflight
16+
if c.Method() == fiber.MethodOptions {
17+
c.Response().Header.Set("Access-Control-Allow-Origin", "*")
18+
c.Response().Header.Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
19+
c.Response().Header.Set("Access-Control-Allow-Headers", "*")
20+
return c.Status(fiber.StatusNoContent).Send(nil)
21+
}
22+
randIdx := rand.Intn(len(config.Cfg.ArchiverNodes))
23+
upstream, _ := url.Parse(config.Cfg.ArchiverNodes[randIdx])
24+
proxy := httputil.NewSingleHostReverseProxy(upstream)
25+
proxy.Director = func(req *http.Request) {
26+
req.URL.Scheme = upstream.Scheme
27+
req.URL.Host = upstream.Host
28+
req.Host = upstream.Host
29+
}
30+
proxy.ModifyResponse = func(resp *http.Response) error {
31+
resp.Header.Del("Access-Control-Allow-Origin")
32+
return nil
33+
}
34+
c.Set("Access-Control-Allow-Origin", "*")
35+
return adaptor.HTTPHandler(proxy)(c)
36+
}

api/server.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import (
44
"context"
55
_ "embed"
66
"fmt"
7-
"math/rand"
87
"net"
98
"net/http"
10-
"net/http/httputil"
11-
"net/url"
129
"os"
1310
"os/signal"
1411
"reflect"
@@ -305,6 +302,12 @@ func NewApiServer(config config.Config) *ApiServer {
305302
return c.JSON(app.metricsCollector.Debug())
306303
})
307304

305+
// Unsplash proxy
306+
app.All("/unsplash/*", app.unsplash)
307+
308+
// Archiver proxy
309+
app.All("/archive/*", archiveProxy)
310+
308311
// resolve myId
309312
app.Use(app.isFullMiddleware)
310313
app.Use(app.resolveMyIdMiddleware)
@@ -595,28 +598,6 @@ func NewApiServer(config config.Config) *ApiServer {
595598
app.Get("/content/verbose", app.contentNodes)
596599
app.Get("/content-nodes/verbose", app.contentNodes)
597600

598-
// Unsplash proxy
599-
app.All("/unsplash/*", app.unsplash)
600-
601-
// Archiver proxy
602-
if len(config.ArchiverNodes) > 0 {
603-
app.All("/archive/*", func(c *fiber.Ctx) error {
604-
randIdx := rand.Intn(len(config.ArchiverNodes))
605-
upstreamUrl := config.ArchiverNodes[randIdx]
606-
upstream, err := url.Parse(upstreamUrl)
607-
if err != nil {
608-
return fiber.NewError(fiber.StatusBadGateway, "Invalid Archiver upstream url")
609-
}
610-
proxy := httputil.NewSingleHostReverseProxy(upstream)
611-
origDirector := proxy.Director
612-
proxy.Director = func(req *http.Request) {
613-
origDirector(req)
614-
req.Host = upstream.Host
615-
}
616-
return adaptor.HTTPHandler(proxy)(c)
617-
})
618-
}
619-
620601
app.Static("/", "./static")
621602

622603
// Disable swagger in test environments, because it will slow things down a lot

0 commit comments

Comments
 (0)