-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
86 lines (68 loc) · 1.63 KB
/
server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"log"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
const (
serverHost = "http://papercast.io/"
)
func generateProxyLink(link string) (string, error) {
s, _ := url.Parse(serverHost)
u, err := url.Parse(link)
if err != nil {
return "", err
}
s.Path = "/proxy/" + u.Scheme + "/" + u.Host + u.RequestURI()
s.RawQuery = u.Query().Encode()
return s.String(), nil
}
func main() {
web := gin.Default()
web.Static("/static", "./assets")
web.StaticFile("/", "./assets/index.html")
web.GET("/rss/:user/:hash", func(c *gin.Context) {
userID := c.Param("user")
hash := c.Param("hash")
ifNoneMatch := c.Request.Header.Get("If-None-Match")
i := newInstapaper(userID, hash)
err := i.fetchInstapaperFeed(ifNoneMatch)
if err != nil {
log.Println(err)
c.XML(http.StatusBadRequest, nil)
return
}
if i.notModified {
c.Header("Etag", i.etag)
c.XML(http.StatusNotModified, nil)
return
}
feed := feedFromInstapaper(i.feed)
c.XML(http.StatusOK, feed)
})
web.GET("/proxy/:scheme/:host/*path", func(c *gin.Context) {
scheme := c.Param("scheme")
host := c.Param("host")
path := c.Param("path")
query := c.Request.URL.Query().Encode()
u := url.URL{Scheme: scheme, Host: host, Path: path, RawQuery: query}
article, err := extractFromURL(u)
if err != nil {
log.Println(err)
c.String(http.StatusInternalServerError, "")
return
}
audioBuffer, err := speak(article)
if err != nil {
log.Println(err)
c.String(http.StatusInternalServerError, "")
return
}
err = audioBuffer.WriteTo(c.Writer)
if err != nil {
log.Println(err)
}
})
web.Run(":3000")
}