/
proxy.go
68 lines (58 loc) · 1.45 KB
/
proxy.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
61
62
63
64
65
66
67
68
package garbanzo
import (
"io"
"net/http"
"net/url"
"sync"
"github.com/labstack/echo/v4"
)
var proxyCache = make(map[string]string)
var proxyMutex = &sync.RWMutex{}
type proxyServ struct {
config *Config
}
func NewProxyRouter(c *Config) *echo.Echo {
ps := proxyServ{config: c}
e := echo.New()
e.GET("/", ps.homeHandler)
e.GET("/*", ps.ghHandler)
return e
}
func (p *proxyServ) homeHandler(c echo.Context) error {
md, err := buildHomeMD(p.config)
if err != nil {
return err
}
html := string(mdToHTML([]byte(md)))
return c.HTML(http.StatusOK, html)
}
func (p *proxyServ) ghHandler(c echo.Context) error {
var u string
reqpath := c.Request().URL.String()
h, err := url.Parse(reqpath)
if err != nil {
return err
}
originHost := h.Query()["origin"]
if len(originHost) == 1 {
u = "https://" + originHost[0] + reqpath
} else {
// FIXME: iframe内で開くのが相対リンクの場合、暗黙的にホストがlocalshostになる。ホストがわからないから、元のページを開けない。とりあえずgithub.comにしておく...
u = "https://github.com" + reqpath
}
// load cache
proxyMutex.RLock()
val, ok := proxyCache[u]
proxyMutex.RUnlock()
if ok {
return c.HTML(http.StatusOK, val)
}
resp, _ := http.Get(u)
defer resp.Body.Close()
byteArray, _ := io.ReadAll(resp.Body)
// save cache
proxyMutex.Lock()
proxyCache[u] = string(byteArray)
proxyMutex.Unlock()
return c.HTML(http.StatusOK, string(byteArray))
}