@@ -15,15 +15,15 @@ import (
15
15
_ "github.com/lib/pq"
16
16
)
17
17
18
- const bondTableTpl string = `
18
+ const stockTableTpl string = `
19
19
<html>
20
20
<h3>Stonks</h3>
21
21
<h3>from most recently updated</h3>
22
22
<table>
23
23
{{ range . }}
24
24
<tr>
25
- <td>{{ .Bond }}</td>
26
- <td>{{ .Factor }}</td>
25
+ <td>{{ .Stock }}</td>
26
+ <td>{{ .StockValue }}</td>
27
27
<td>{{ .Date }}</td>
28
28
</tr>
29
29
{{ end }}
@@ -33,20 +33,20 @@ const bondTableTpl string = `
33
33
34
34
const indexPage string = `
35
35
<html>
36
- <h3>Hello</h3>
36
+ <h3>Hello KubeCon! </h3>
37
37
38
38
<ul>
39
- <li>Get <a href="/latest">the latest bond values</a></li>
39
+ <li>Get <a href="/latest">the latest stock values</a></li>
40
40
<li>Add <a href="/update">random stock values</a></li>
41
41
</ul>
42
42
</html>
43
43
`
44
44
45
- // bondState represents the value of a bond at a given time
46
- type bondState struct {
47
- Bond string
48
- Factor float64
49
- Date time.Time
45
+ // stockTicker represents the value of a stock at a given time
46
+ type stockTicker struct {
47
+ Stock string
48
+ StockValue float64
49
+ Date time.Time
50
50
}
51
51
52
52
func main () {
@@ -56,7 +56,6 @@ func main() {
56
56
57
57
pgUser := os .Getenv ("PG_USER" )
58
58
pgPass := os .Getenv ("PG_PASSWORD" )
59
- pgService := "cluster-example-rw"
60
59
61
60
port := 8080
62
61
@@ -67,20 +66,20 @@ func main() {
67
66
var dbConnString string
68
67
if inside {
69
68
dbConnString = fmt .Sprintf ("postgres://%s:%s@%s/app?sslmode=require" ,
70
- pgUser , pgPass , pgService )
69
+ pgUser , pgPass , "cluster-example-rw" )
71
70
} else {
72
71
dbConnString = fmt .Sprintf ("postgres://%s:%s@%s/app?sslmode=require" ,
73
72
pgUser , pgPass , "localhost" )
74
73
}
75
74
76
- bondTable , err := template .New ("table" ).Parse (bondTableTpl )
75
+ stockTable , err := template .New ("table" ).Parse (stockTableTpl )
77
76
if err != nil {
78
77
log .Fatalf ("could not parse template: %v" , err )
79
78
}
80
79
81
80
rand .Seed (time .Now ().UnixNano ())
82
81
83
- // HTTP dispatch table
82
+ // HTTP ENDPOINTS
84
83
85
84
// handle route using handler function
86
85
http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
@@ -110,31 +109,31 @@ func main() {
110
109
}
111
110
112
111
rows , err := db .Query (`
113
- select bond , date, factor
112
+ select stock , date, stock_value
114
113
from (
115
- select bond , rank() over wd as rank,
114
+ select stock , rank() over wd as rank,
116
115
first_value(date) over wd as date,
117
- first_value(factor ) over wd as factor
118
- from factors
119
- window wd as (partition by bond order by date desc)
116
+ first_value(stock_value ) over wd as stock_value
117
+ from stock_values
118
+ window wd as (partition by stock order by date desc)
120
119
) as latest where rank = 1
121
- order by date desc, bond ;
120
+ order by date desc, stock ;
122
121
` )
123
122
if err != nil {
124
123
http .Error (w , err .Error (), http .StatusInternalServerError )
125
124
return
126
125
}
127
126
defer rows .Close ()
128
127
129
- var bonds []bondState
128
+ var stocks []stockTicker
130
129
for rows .Next () {
131
- var bondS bondState
132
- err = rows .Scan (& bondS . Bond , & bondS .Date , & bondS . Factor )
130
+ var stockT stockTicker
131
+ err = rows .Scan (& stockT . Stock , & stockT .Date , & stockT . StockValue )
133
132
if err != nil {
134
133
http .Error (w , err .Error (), http .StatusInternalServerError )
135
134
return
136
135
}
137
- bonds = append (bonds , bondS )
136
+ stocks = append (stocks , stockT )
138
137
}
139
138
140
139
if rErr := rows .Err (); rErr != nil {
@@ -144,14 +143,14 @@ order by date desc, bond;
144
143
145
144
if r .Header .Get ("Accept" ) == "application/json" {
146
145
jsonWriter := json .NewEncoder (w )
147
- err = jsonWriter .Encode (bonds )
146
+ err = jsonWriter .Encode (stocks )
148
147
if err != nil {
149
148
http .Error (w , err .Error (), http .StatusInternalServerError )
150
149
return
151
150
}
152
151
} else {
153
152
w .Header ().Set ("Content-Type" , "text/html" )
154
- err = bondTable .Execute (w , bonds )
153
+ err = stockTable .Execute (w , stocks )
155
154
if err != nil {
156
155
http .Error (w , err .Error (), http .StatusInternalServerError )
157
156
return
@@ -191,10 +190,10 @@ order by date desc, bond;
191
190
192
191
for i := 0 ; i < 5 ; i ++ {
193
192
n := rand .Intn (50 ) + 1 // between 1 and 50, like our stocks
194
- bond := fmt .Sprintf ("bn_ %d" , n )
193
+ stock := fmt .Sprintf ("stock_ %d" , n )
195
194
_ , err = tx .Exec (
196
- `INSERT INTO factors(bond , date, factor ) values ($1, $2, $3)` ,
197
- bond , time .Now (), rand .Float64 ())
195
+ `INSERT INTO stock_values(stock , date, stock_value ) values ($1, $2, $3)` ,
196
+ stock , time .Now (), rand .Float64 ())
198
197
if err != nil {
199
198
http .Error (w , err .Error (), http .StatusInternalServerError )
200
199
return
@@ -205,7 +204,7 @@ order by date desc, bond;
205
204
})
206
205
207
206
// listen to port
208
- log .Println ("ENV" , podName , podEnv , podIP , pgPass , pgService , pgUser )
207
+ log .Println ("ENV" , podName , podEnv , podIP , pgPass , "cluster-example-rw" , pgUser )
209
208
log .Printf ("running the server, listening on %d\n " , port )
210
209
log .Fatal (http .ListenAndServe (fmt .Sprintf (":%d" , port ), nil ))
211
210
}
0 commit comments