/
main.go
131 lines (117 loc) · 3.97 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
)
type stocksArray []singleStock
type singleStock struct {
Name string `json:"t"`
Amount string `json:"l"`
}
var (
err error
stocks stocksArray
response *http.Response
body []byte
)
const stocktickerTemplateHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Go, stocks, ticker, Google, Apple, Microsoft, sample app">
<meta name="author" content="Thomas Jaensch">
<title>Go Stock Ticker</title>
<!-- Bootstrap Core CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="/css/one-page-wonder.css" rel="stylesheet">
<!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Full Width Image Header -->
<header class="header-image">
<div class="headline">
<div class="container">
<h2>{{range .}}
{{.Name}}: ${{.Amount}} <br>
{{end}}</h2>
<p>Stock ticker* demo app written in Go with Bootstrap, code on <a href="https://github.com/tjaensch/stockticker_google_sdk">Github</a></p>
<p><small>*Not ticking? Then it's probably after hours on Wall Street.</small></p>
</div>
</div>
</header>
<!-- Footer -->
<footer>
<div class="row">
<div class="col-lg-12 text-center">
<p><small>© Thomas Jaensch 2015</small></p>
</div>
</div>
</footer>
</div>
<!-- /.container -->
<!-- Ajax call to refresh page periodically -->
<script type="text/javascript">
function startRefresh() {
$.get('', function(data) {
$(document.body).html(data);
});
}
$(function() {
setTimeout(startRefresh,1000);
});
</script>
</body>
</html>
`
var stockTemplate = template.Must(template.New("stockticker").Parse(stocktickerTemplateHTML))
func stockticker(w http.ResponseWriter, r *http.Request) {
// Use http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG to get a JSON response
response, err = http.Get("http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG,NASDAQ:AAPL,NASDAQ:MSFT")
if err != nil {
fmt.Println(err)
}
defer response.Body.Close()
// Read the data into a byte slice
body, err = ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
// Remove whitespace from response
data := bytes.TrimSpace(body)
// Remove leading slashes and blank space to get byte slice that can be unmarshaled from JSON
data = bytes.TrimPrefix(data, []byte("// "))
// Unmarshal the JSON byte slice to a predefined struct
err = json.Unmarshal(data, &stocks)
if err != nil {
fmt.Println(err)
}
// Parse struct data to template
tempErr := stockTemplate.Execute(w, stocks)
if tempErr != nil {
http.Error(w, tempErr.Error(), http.StatusInternalServerError)
}
}
func main() {
fs := http.FileServer(http.Dir("css"))
http.Handle("/css/", http.StripPrefix("/css/", fs))
http.HandleFunc("/", stockticker)
fmt.Printf("Listening on http://localhost:8080/")
http.ListenAndServe(":8080", nil)
}