-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
53 lines (44 loc) · 1.02 KB
/
index.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
package main
import (
"html/template"
"net/http"
"net/url"
"google.golang.org/appengine/v2/log"
)
func Index(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
feedURL, err := generateFeedURLWithProxy(r)
if err == nil {
http.Redirect(w, r, feedURL, http.StatusFound)
return
}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t := template.Must(template.ParseFiles("index.html"))
err := t.Execute(w, map[string]string{
"origin": ServerOrigin(r.Host),
})
if err != nil {
log.Errorf(r.Context(), err.Error())
}
}
func generateFeedURLWithProxy(r *http.Request) (string, error) {
u, _ := url.Parse(ServerOrigin(r.Host))
u.Path = "/p"
q := u.Query()
setIfExist := func(k string) {
if r.FormValue(k) != "" {
q.Set(k, r.FormValue(k))
}
}
setIfExist("feed")
setIfExist("org")
setIfExist("channel")
// add a query param without value
encoded := q.Encode()
if r.FormValue("diet") != "" {
encoded = encoded + "&diet"
}
u.RawQuery = encoded
return u.String(), nil
}