-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunc.go
69 lines (62 loc) · 1.24 KB
/
func.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
package main
import (
"fmt"
"html/template"
"log"
"net/url"
"os"
"strings"
)
type Link struct {
Text string
Href string
}
func main() {
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<p>{{.Body}}</p>
<div>
{{range .Items}}<div><a href="{{.Href}}">{{ .Text }}</a></div>{{else}}<div><strong>no rows</strong></div>{{end}}
</div>
</body>
</html>`
check := func(err error) {
if err != nil {
log.Fatal(err)
}
}
t, err := template.New("webpage").Parse(tpl)
check(err)
appName := os.Getenv("FN_APP_NAME")
reqURL := os.Getenv("FN_REQUEST_URL")
url, err := url.Parse(reqURL)
if err != nil {
panic(err)
}
pathPrefix := ""
if strings.HasPrefix(url.Path, "/r/") {
pathPrefix = fmt.Sprintf("/r/%s", appName)
}
data := struct {
Title string
Body string
Items []Link
}{
Title: "My App",
Body: "This is my app. It may not be the best app, but it's my app. And it's multilingual!",
Items: []Link{
Link{"Ruby", fmt.Sprintf("%s/ruby", pathPrefix)},
Link{"Node", fmt.Sprintf("%s/node", pathPrefix)},
Link{"Python", fmt.Sprintf("%s/python", pathPrefix)},
},
}
err = t.Execute(os.Stdout, data)
check(err)
}