-
Notifications
You must be signed in to change notification settings - Fork 19
/
urlshortner.go
60 lines (49 loc) · 1.07 KB
/
urlshortner.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"io"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
var baseURL = "https://github.com/mosajjal/binary-tools/blob/master/"
func handleURL(c *gin.Context, folders ...string) {
path := ""
if folders[0] == "" {
folders = folders[1:]
}
if folders[0] != "arm" && folders[0] != "x64" {
// prepend x64 to folders
folders = append([]string{"x64"}, folders...)
}
for _, folder := range folders {
if folder == "" {
continue
}
path = path + folder + "/"
}
u := baseURL + path + c.Param("binary") + "?raw=true"
resp, err := http.Get(u)
if err != nil {
c.Err()
}
if resp.StatusCode != http.StatusOK {
c.Status(404)
return
}
defer resp.Body.Close()
// clientGone := c.Request.Context().Done()
c.Stream(func(w io.Writer) bool {
// TODO: deal with clientGone
// case <-clientGone:
// return false
_, err := io.Copy(c.Writer, resp.Body)
return err != nil
})
}
func main() {
apiRoutes := gin.Default()
apiRoutes.GET("/*path", func(c *gin.Context) {
handleURL(c, strings.Split(c.Param("path"), "/")...)
})
apiRoutes.Run()
}