-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebServer.go
212 lines (160 loc) · 4.6 KB
/
webServer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Created by Robert A. Baruch on 03/14/2015
// Copyright (c) 2015 Robert A. Baruch, All rights reserved.
//
package main
import (
"errors"
"fmt"
"html/template"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
const ()
type DemoPages struct {
pages map[string]DemoPage
}
type DemoPage struct {
urlString string
callback gin.HandlerFunc
template string
context interface{}
}
type Movies struct {
Name string `json:"name"`
Review string `json:"review"`
Stars string `json:"stars"`
Link string `json:"link"`
}
var (
nullFunc = func(c *gin.Context) {
c.String(http.StatusOK, "default func - stub - replace with actual function")
}
)
func main() {
var dp DemoPages
var foo map[string]int = map[string]int{"foo": 1, "bar": 2}
var bar map[string]string = map[string]string{"foo": "Alpha", "bar": "Beta"}
var grill map[string]string = map[string]string{"foo": "Omega", "bar": "Alpha"}
dp.pages = map[string]DemoPage{
"fred": DemoPage{"/fred", FredFunc, "fred.tmpl", foo},
"tom": DemoPage{"/tom", TomFunc, "tom.tmpl", grill},
"sally": DemoPage{"/sally", SallyFunc, "sally.tmpl", bar},
"core": DemoPage{"/feeds/api/rob", coreFunc, "", nil},
"core2": DemoPage{"/feeds/api/rob2", coreFunc2, "core.tmpl", nil},
}
// Use Gin-Gonic for our web framework
router := gin.Default()
router.Use(dp.Params())
// Register a template for each page in the pages table
var html = template.New("temps")
// Escape the {{ }} braces used by Polymer and Golang Templates
html.Delims("{[", "]}")
// Load all of the templates defined in the pages map
template.Must(html.ParseFiles(buildTmpArray("./templates", dp.pages)...))
router.SetHTMLTemplate(html)
// Serve static content
router.Static("/static", "./static")
// Add a default GET for /
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", nil)
})
// for each element in the pages map, add a GET route and corresponding handler
for _, p := range dp.pages {
router.GET(p.urlString, p.callback)
}
router.Run(":8080")
}
// GET callbacks: Fred
func FredFunc(c *gin.Context) {
dp, err := GetDPContext(c)
log.Printf("preparing %s\n", dp.pages["fred"].urlString)
log.Printf("context = %v\n", dp.pages["fred"].context)
ctx := (dp.pages["fred"].context).(map[string]int)
if err != nil {
log.Printf("failed to get server context\n")
} else {
obj := gin.H{"title": dp.pages["fred"].urlString,
"foo": ctx["foo"],
}
c.HTML(http.StatusOK, "fred.tmpl", obj)
}
}
// GET callbacks; Tom
func TomFunc(c *gin.Context) {
dp, err := GetDPContext(c)
ctx := (dp.pages["tom"].context).(map[string]string)
if err != nil {
log.Printf("failed to get server context\n")
} else {
obj := gin.H{"title": dp.pages["tom"].urlString,
"foo": ctx["foo"],
}
c.HTML(http.StatusOK, "tom.tmpl", obj)
}
}
// GET callbacks; Sally
func SallyFunc(c *gin.Context) {
dp, err := GetDPContext(c)
ctx := (dp.pages["sally"].context).(map[string]string)
if err != nil {
log.Printf("failed to get server context\n")
} else {
obj := gin.H{"title": dp.pages["sally"].urlString,
"foo": ctx["foo"],
}
c.HTML(http.StatusOK, "sally.tmpl", obj)
}
}
// GET callbacks; core
func coreFunc(c *gin.Context) {
// dp, err := GetDPContext(c)
p1 := c.Request.URL.Query().Get("alt")
p2 := c.Request.URL.Query().Get("q")
fmt.Printf("P1 = [%s] P2 = [%s]\n", p1, p2)
var core []Movies = []Movies{
Movies{"Throw Mama From the Train", "Sucked", "**", "http://www.nogo.com"},
Movies{"Die Hard", "Sucked Less", "***", "http://www.go.com"},
}
c.JSON(http.StatusOK, core)
}
// GET callbacks; core
func coreFunc2(c *gin.Context) {
// dp, err := GetDPContext(c)
p1 := c.Request.URL.Query().Get("alt")
p2 := c.Request.URL.Query().Get("q")
fmt.Printf("P1 = [%s] P2 = [%s]\n", p1, p2)
c.HTML(http.StatusOK, "core.tmpl", nil)
}
// Middleware handler for global Context
func (dp *DemoPages) Params() gin.HandlerFunc {
return func(c *gin.Context) {
_, err := c.Get("dp")
if err != nil {
log.Printf("Setting context\n")
c.Set("dp", dp)
}
c.Next()
}
}
// Function to retrieve global Context
func GetDPContext(c *gin.Context) (*DemoPages, error) {
dp, err := c.Get("dp")
if err != nil {
log.Printf("failed to find server parameter context")
return nil, errors.New("failed to find parameter context")
}
return dp.(*DemoPages), nil
}
// return an array of templates to pass to ParseFiles
func buildTmpArray(tdir string, p map[string]DemoPage) []string {
var temps []string = []string{}
for _, page := range p {
if page.template != "" {
pe := fmt.Sprintf("%s/%s", tdir, page.template)
temps = append(temps, pe)
}
}
return temps
}