mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Example app structure. round 1.
This commit is contained in:
58
examples/app/func.go
Normal file
58
examples/app/func.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user