Example app structure. round 1.

This commit is contained in:
Travis Reeder
2017-09-14 16:09:28 -07:00
parent fdc3e76359
commit 75e2051169
25 changed files with 194 additions and 3 deletions

58
examples/app/func.go Normal file
View File

@@ -0,0 +1,58 @@
package main
import (
"fmt"
"html/template"
"log"
"os"
)
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")
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("/r/%s/ruby", appName)},
Link{"Node", fmt.Sprintf("/r/%s/node", appName)},
Link{"Python", fmt.Sprintf("/r/%s/python", appName)},
},
}
err = t.Execute(os.Stdout, data)
check(err)
}