Exos template

This commit is contained in:
Meutel 2017-07-26 20:34:08 +02:00
parent a030e81501
commit d383baba8a
8 changed files with 293 additions and 0 deletions

3
.gitignore vendored
View File

@ -54,3 +54,6 @@ example-httptcp/example-httptcp
example-httpserver/example-httpserver
http-router/http-router
tpl-text/tpl-text
tpl-ex1/tpl-ex1
tpl-ex2/tpl-ex2
tpl-ex3/tpl-ex3

20
tpl-ex1/main.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"html/template"
"net/http"
)
func main() {
tpl, err := template.ParseFiles("tpl.html")
if err != nil {
panic(err)
}
http.HandleFunc("/hello/", func(res http.ResponseWriter, req *http.Request) {
err = tpl.ExecuteTemplate(res, "tpl.html", "Hello World")
if err != nil {
panic(err)
}
})
http.ListenAndServe(":9000", nil)
}

7
tpl-ex1/tpl.html Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head><title>Test tpl 1</title></head>
<body>
<h1>{{ . }}</h1>
</body>
</html>

20
tpl-ex2/main.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"html/template"
"net/http"
)
func main() {
tpl, err := template.ParseFiles("tpl.html")
if err != nil {
panic(err)
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
err = tpl.ExecuteTemplate(res, "tpl.html", req)
if err != nil {
panic(err)
}
})
http.ListenAndServe(":9000", nil)
}

7
tpl-ex2/tpl.html Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head><title>Test tpl 1</title></head>
<body>
<h1>{{ .URL }}</h1>
</body>
</html>

122
tpl-ex3/main.go Normal file
View File

@ -0,0 +1,122 @@
package main
import (
"encoding/csv"
"html/template"
"io"
"log"
"net/http"
"os"
"strconv"
)
var COLUMNS []string
type FinancialData struct {
Date string
Open, High, Low, Close, Adj_Close, Volume float64
}
type TplData struct {
Columns []string
Rows []FinancialData
}
func printHTML(tpl *template.Template, data []FinancialData, out io.Writer) {
tplData := TplData{
COLUMNS,
data,
}
err := tpl.ExecuteTemplate(out, "tpl.html", tplData)
if err != nil {
panic(err)
}
}
func readCsvLine(line []string) *FinancialData {
data := new(FinancialData)
for i, col := range line {
switch COLUMNS[i] {
case "Date":
data.Date = col
case "Open":
data.Open = toFloat(col)
case "High":
data.High = toFloat(col)
case "Low":
data.Low = toFloat(col)
case "Close":
data.Close = toFloat(col)
case "Adj Close":
data.Adj_Close = toFloat(col)
case "Volume":
data.Volume = toFloat(col)
}
}
return data
}
func toFloat(v string) float64 {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Fatalln(err)
}
return f
}
func readCsvHeader(line []string) {
for _, col := range line {
COLUMNS = append(COLUMNS, col)
}
}
func readCsv(in io.Reader) []FinancialData {
data := []FinancialData{}
csvReader := csv.NewReader(in)
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
if len(COLUMNS) == 0 {
readCsvHeader(line)
} else {
data = append(data, *readCsvLine(line))
}
}
return data
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage go-financial <file>")
}
// read input
csv, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln("Error reading file", err)
}
defer csv.Close()
// parse data
data := readCsv(csv)
tpl, err := template.New("").Funcs(template.FuncMap{
"class": func(index int) string {
if index%2 == 0 {
return "even"
}
return "odd"
},
}).ParseFiles("tpl.html", "script.js")
if err != nil {
panic(err)
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
printHTML(tpl, data, res)
})
http.ListenAndServe(":9000", nil)
}

63
tpl-ex3/script.js Normal file
View File

@ -0,0 +1,63 @@
Highcharts.chart('container', {
title: {
text: 'Financial data'
},
subtitle: {
text: 'Source: yahoo.com'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Value'
}
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
series: [{
type: 'area',
name: 'Open',
data: [
{{ range .Rows }} [ {{ .Date }}, {{ .Open }} ],
{{ end }} ]
},{
type: 'area',
name: 'Close',
data: [
{{ range .Rows }} [ {{ .Date }}, {{ .Close }} ],
{{ end }} ]
}]
});

51
tpl-ex3/tpl.html Normal file
View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head><title>Financial</title></head>
<body>
<table border="1">
<tr>
{{ range .Columns }}
<th>{{ . }}</th>
{{ end }}
</tr>
{{ range $index, $element := .Rows }}
<tr class="{{ class $index }}">
{{ with $element }}
<td>{{ .Date }}</td>
<td>{{ .Open }}</td>
<td>{{ .High }}</td>
<td>{{ .Low }}</td>
<td>{{ .Close }}</td>
<td>{{ .Adj_Close }}</td>
<td>{{ .Volume }}</td>
{{ end }}
</tr>
{{ end }}
</table>
<div id="container"></div>
<style>
#container {
min-width: 310px;
max-width: 800px;
height: 400px;
margin: 0 auto
}
tr.odd td {
background-color: black;
color: white;
}
tr.even td {
background-color: white;
color: black;
}
</style>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script>
{{ template "script.js" . }}
</script>
</body>
</html>