-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
45 lines (39 loc) · 1.13 KB
/
proxy.go
File metadata and controls
45 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"net/http"
"github.com/labstack/echo"
)
func proxy(repo string, addr string) {
e := echo.New()
e.GET("/dl/:file", func(c echo.Context) error {
rel, err := getLatestRelease(repo)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
for _, asset := range rel.Assets {
if asset.Name == c.Param("file") {
res, err := http.Get(asset.Link)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
defer res.Body.Close()
return c.Stream(http.StatusOK, asset.ContentType, res.Body)
}
}
return c.JSON(http.StatusNotFound, "file not found")
})
e.GET("/dl/:tag/:file", func(c echo.Context) error {
r, err := http.Get(fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", repo, c.Param("tag"), c.Param("file")))
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
defer r.Body.Close()
contentType := r.Header.Get("Content-type")
if contentType == "" {
contentType = "application/octet-stream"
}
return c.Stream(r.StatusCode, contentType, r.Body)
})
e.Start(addr)
}