-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.go
616 lines (535 loc) · 17.1 KB
/
flow.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package flow
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log/slog"
"net/http"
"net/url"
"runtime"
"strconv"
"time"
"strings"
"github.com/oklog/ulid"
"github.com/go-zoo/bone"
"github.com/nerdynz/datastore"
"github.com/nerdynz/security"
"github.com/unrolled/render"
)
type Flow struct {
respWrt http.ResponseWriter
req *http.Request
Renderer *render.Render
Padlock *security.Padlock
Store *datastore.Datastore
scheme string
bucket *bucket
errLog string
hasPopulated bool
}
const NO_MASTER = "NO_MASTER"
// New manages every new request, set shortcuts here, be careful your within a "flowCtx" here
func New(w http.ResponseWriter, req *http.Request, renderer *render.Render, store *datastore.Datastore, key security.Key) *Flow {
flow := &Flow{}
flow.respWrt = w
flow.req = req
flow.Renderer = renderer
flow.Store = store
flow.Padlock = security.New(req, store.Settings, key)
flow.hasPopulated = false
proto := "http://"
if store.Settings.IsProduction() {
// should be secure
proto = "https://"
}
if req.Header.Get("X-Forwarded-Proto") == "https" {
proto = "https://"
}
flow.scheme = proto
req.URL.Scheme = proto // is this a bad idea???
// if store.Settings.LoggingEnabled {
// flow.Logger = datastore.NewLogger()
// }
return flow
}
func (flow *Flow) WebsiteBaseURL() string {
return flow.Store.Settings.Get("WEBSITE_BASE_URL")
}
func (flow *Flow) SiteULID() (string, error) {
return flow.Padlock.SiteULID()
}
// func (flow *flowCtx) GetCacheValue(key string) (string, error) {
// return flow.Store.GetCacheValue(key)
// }
// func (flow *flowCtx) SetCacheValue(key string, value interface{}, duration time.Duration) (string, error) {
// return flow.Store.SetCacheValue(key, value, duration)
// }
func (flow *Flow) Write(b []byte) (int, error) {
flow.errLog += string(b)
return len(b), nil
}
func (flow *Flow) catchAfterErr(err error) {
// currently a NO-OP
}
func (flow *Flow) populateCommonVars() {
flow.bucket = &bucket{
vars: make(map[string]interface{}),
}
flow.hasPopulated = true
loggedInUser, _, _ := flow.Padlock.LoggedInUser()
flow.Add("IsLoggedIn", loggedInUser != nil)
if loggedInUser != nil {
flow.Add("LoggedInUser", loggedInUser)
}
flow.Add("websiteBaseURL", flow.scheme+flow.req.Host+"/")
flow.Add("WebsiteBaseURL", flow.scheme+flow.req.Host+"/")
flow.Add("currentURL", flow.req.URL.Path)
flow.Add("CurrentURL", flow.req.URL.Path)
flow.Add("currentFullURL", flow.scheme+flow.req.Host+flow.req.URL.Path)
flow.Add("CurrentFullURL", flow.scheme+flow.req.Host+flow.req.URL.Path)
flow.Add("Now", time.Now())
flow.Add("Year", time.Now().Year())
}
type bucket struct {
vars map[string]any
}
func (flow *Flow) Vars() map[string]any {
return flow.bucket.vars
}
func (flow *Flow) Add(key string, value interface{}) {
if !flow.hasPopulated {
flow.populateCommonVars()
}
flow.bucket.vars[key] = value
}
func (flow *Flow) AddRenderer(renderer *render.Render) {
flow.Renderer = renderer
}
func (flow *Flow) URLValues(key string) ([]string, error) {
err := flow.req.ParseForm()
if err != nil {
return nil, err
}
return flow.req.Form[key], nil
}
func (flow *Flow) URLIntValues(key string) ([]int, error) {
ints := make([]int, 0)
vals, err := flow.URLValues(key)
if err != nil {
return ints, err
}
for _, val := range vals {
iVal, err := strconv.Atoi(val)
if err != nil {
return ints, err
}
ints = append(ints, iVal)
}
return ints, nil
}
func (flow *Flow) URLULIDParam(key string) (string, error) {
ul := strings.ToUpper(flow.URLParam(key))
id, err := ulid.Parse(ul)
if err != nil {
return "", err
}
return id.String(), nil
}
func (flow *Flow) URLParam(key string) string {
// try route param
value := bone.GetValue(flow.req, key)
// try qs
if value == "" {
value = flow.req.URL.Query().Get(key)
}
// do we have a value
if value != "" {
newValue, err := url.QueryUnescape(value)
if err == nil {
value = strings.Replace(newValue, "%20", " ", -1)
}
}
return value
}
func (flow *Flow) URLIntParam(key string) (int, error) {
return strconv.Atoi(flow.URLParam(key))
}
func (flow *Flow) URLDateParam(key string) (time.Time, error) {
var dt = flow.URLParam(key)
return time.Parse(time.RFC3339Nano, dt)
}
func (flow *Flow) URLShortDateParam(key string) (time.Time, error) {
var dt = flow.URLParam(key)
return time.Parse("20060102", dt)
}
func (flow *Flow) URLBoolParam(key string) bool {
val := flow.URLParam(key)
if val == "true" {
return true
}
if val == "yes" {
return true
}
if val == "1" {
return true
}
if val == "y" {
return true
}
if val == "✓" {
return true
}
b, err := strconv.ParseBool(val)
if err != nil {
return false
}
return b
}
func (flow *Flow) URLIntParamWithDefault(key string, deefault int) int {
val := flow.URLParam(key)
if val == "" {
return deefault // default
}
c, err := strconv.Atoi(flow.URLParam(key))
if err != nil {
return deefault // default
}
return c
}
func (flow *Flow) URLUnique() string {
val := flow.URLParam("uniqueid")
if val == "" {
val = flow.URLParam("ulid")
}
return strings.ToUpper(val)
}
func (flow *Flow) SetCookie(cookie *http.Cookie) {
http.SetCookie(flow.respWrt, cookie)
}
func (flow *Flow) Redirect(newUrl string, status int) {
if status == 301 || status == 302 || status == 303 || status == 304 || status == 401 {
http.Redirect(flow.respWrt, flow.req, newUrl, status)
return
}
flow.ErrorText(http.StatusInternalServerError, "Invalid Redirect", nil)
}
func (flow *Flow) StaticFile(status int, filepath string, mime string) {
file, err := ioutil.ReadFile(filepath)
if err != nil {
flow.ErrorText(500, "Failed to load %s", err)
return
}
flow.respWrt.Header().Add("content-type", mime)
err = flow.Renderer.Data(flow.respWrt, status, file)
flow.catchAfterErr(err)
}
func (flow *Flow) Data(status int, bytes []byte, mime string) {
flow.respWrt.Header().Add("content-type", mime)
err := flow.Renderer.Data(flow.respWrt, status, bytes)
flow.catchAfterErr(err)
}
func (flow *Flow) File(bytes []byte, filename string, mime string) {
flow.respWrt.Header().Set("Content-Type", mime)
flow.respWrt.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)
flow.respWrt.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
_, err := flow.respWrt.Write(bytes)
flow.catchAfterErr(err)
}
func (flow *Flow) InlineFile(bytes []byte, filename string, mime string) {
flow.respWrt.Header().Set("Content-Type", mime)
flow.respWrt.Header().Set("Content-Disposition", `inline; filename="`+filename+`"`)
flow.respWrt.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
_, err := flow.respWrt.Write(bytes)
flow.catchAfterErr(err)
}
func (flow *Flow) PDF(bytes []byte) {
flow.respWrt.Header().Set("Content-Type", "application/PDF")
flow.respWrt.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
_, err := flow.respWrt.Write(bytes)
flow.catchAfterErr(err)
}
func (flow *Flow) Excel(bytes []byte, filename string) {
flow.respWrt.Header().Set("Content-Type", "application/vnd.ms-excel")
flow.respWrt.Header().Set("Content-Disposition", `filename="`+filename+`.xlsx"`)
_, err := flow.respWrt.Write(bytes)
flow.catchAfterErr(err)
}
func (flow *Flow) Text(status int, str string) {
err := flow.Renderer.Text(flow.respWrt, status, str)
flow.catchAfterErr(err)
}
func (flow *Flow) HTMLalt(layout string, status int, master string) error {
return flow.htmlAlt(flow.respWrt, layout, status, master)
}
func (flow *Flow) htmlAlt(w io.Writer, layout string, status int, master string) error {
if !flow.hasPopulated {
flow.populateCommonVars()
}
if flow.req.URL.Query().Get("dump") == "1" {
return flow.Renderer.HTML(w, status, "error", flow.bucket.vars)
}
if flow.req.Header.Get("X-PJAX") == "true" {
return flow.Renderer.HTML(w, status, layout, flow.bucket.vars, render.HTMLOptions{
Layout: "pjax",
})
}
if master == NO_MASTER {
return flow.Renderer.HTML(w, status, layout, flow.bucket.vars, render.HTMLOptions{})
}
if master != "" {
return flow.Renderer.HTML(w, status, layout, flow.bucket.vars, render.HTMLOptions{
Layout: master,
})
}
return flow.Renderer.HTML(w, status, layout, flow.bucket.vars)
}
func (flow *Flow) HTML(layout string, status int) {
err := flow.HTMLalt(layout, status, "")
flow.catchAfterErr(err)
}
func (flow *Flow) HTMLAsText(layout string, status int) (*bytes.Buffer, error) {
return flow.HTMLAsTextAlt(layout, status, "")
}
func (flow *Flow) HTMLAsTextAlt(layout string, status int, master string) (*bytes.Buffer, error) {
buf := &bytes.Buffer{}
err := flow.htmlAlt(buf, layout, status, master)
return buf, err
}
func (flow *Flow) JSON(status int, data interface{}) {
// render.JSON(flow.respWrt, status, data)
err := flow.Renderer.JSON(flow.respWrt, status, data)
flow.catchAfterErr(err)
}
func (flow *Flow) ErrorText(status int, friendly string, errs ...error) {
if len(errs) > 0 {
flow.errorOut(true, status, friendly, errs...)
} else {
flow.errorOut(true, status, friendly, nil)
}
}
func (flow *Flow) ErrorJSON(status int, friendly string, errs ...error) {
if len(errs) > 0 {
flow.errorOut(false, status, friendly, errs...)
} else {
flow.errorOut(false, status, friendly, nil)
}
}
func (flow *Flow) errorOut(isText bool, status int, friendly string, errs ...error) {
//https: //stackoverflow.com/questions/24809287/how-do-you-get-a-golang-program-to-print-the-line-number-of-the-error-it-just-ca
errStr := ""
lineNumber := -1
funcName := "Not Specified"
fileName := "Not Specified"
if len(errs) > 0 {
for _, err := range errs {
if err != nil {
errStr += err.Error() + "\n"
} else {
errStr += "No Error Specified \n"
}
}
// notice that we're using 1, so it will actually log the where // actually one is errorOut
// the error happened, 0 = this function, we don't want that.
pc, file, line, _ := runtime.Caller(2)
lineNumber = line
funcName = runtime.FuncForPC(pc).Name()
fileName = file
}
data := &errorData{
friendly,
errStr,
lineNumber,
funcName,
fileName,
}
slog.Error(data.nicelyFormatted())
if isText {
err := flow.Renderer.Text(flow.respWrt, status, data.nicelyFormatted())
flow.catchAfterErr(err)
return
}
w := flow.respWrt
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
flow.catchAfterErr(err)
}
type errorData struct {
Friendly string
Error string
LineNumber int
FunctionName string
FileName string
}
func (e *errorData) nicelyFormatted() string {
str := ""
str += "Friendly Message: \n\t" + e.Friendly + "\n"
str += "Error: \n\t" + e.Error + "\n"
str += "File: \n\t" + e.FileName + ":" + strconv.Itoa(e.LineNumber) + "\n"
// str += "LineNumber: \n\t" + strconv.Itoa(e.LineNumber) + "\n"
str += "FunctionName: \n\t" + e.FunctionName + "\n"
return str
}
func (flow *Flow) ErrorHTML(status int, friendly string, errs ...error) {
errStr := ""
flow.Add("FriendlyError", friendly)
if len(errs) > 0 {
for _, err := range errs {
errStr += err.Error() + "\n"
}
flow.Add("NastyError", errStr)
// notice that we're using 1, so it will actually log the where
// the error happened, 0 = this function, we don't want that.
pc, file, line, _ := runtime.Caller(1)
lineNumber := line
funcName := runtime.FuncForPC(pc).Name()
fileName := file
flow.Add("LineNumber", lineNumber)
flow.Add("FuncName", funcName)
flow.Add("FileName", fileName)
}
flow.Add("ErrorCode", status)
flow.noTemplateHTML("error", status)
}
func (flow *Flow) noTemplateHTML(layout string, status int) {
opt := render.HTMLOptions{
Layout: "",
}
err := flow.Renderer.HTML(flow.respWrt, status, layout, flow.bucket.vars, opt)
flow.catchAfterErr(err)
}
// func (flow *flowCtx) BroadcastToCurrentSite(t string, data interface{}) error {
// s := strconv.Itoa(flow.SiteID())
// return flow.Broadcast("room-"+s, t, data)
// }
// func (flow *flowCtx) BroadcastToSite(siteID int, t string, data interface{}) error {
// s := strconv.Itoa(siteID)
// return flow.Broadcast("room-"+s, t, data)
// }
// func (flow *flowCtx) Broadcast(room string, t string, data interface{}) error {
// bc := &broadcast{
// Type: strings.Title(t),
// Data: data,
// }
// b, err := json.Marshal(bc)
// if err != nil {
// return err
// }
// err = flow.Store.Websocket.Broadcast(room, string(b))
// return err
// }
// type broadcast struct {
// Type string
// Data interface{}
// }
// func (flow *flowCtx) SPA(status int, pageInfo *PageInfo, data interface{}) {
// pageInfo.DocumentTitle = pageInfo.Title
// if pageInfo.SiteInfo != nil {
// pageInfo.DocumentTitle = pageInfo.Title + " - " + pageInfo.SiteInfo.Tagline + " - " + pageInfo.SiteInfo.Sitename
// }
// // logrus.Info(strings.ToLower(flow.req.Header.Get("Accept")))
// if strings.Contains(strings.ToLower(flow.req.Header.Get("Accept")), "application/json") {
// flow.JSON(status, data)
// } else {
// url := flow.req.URL
// buf := bytes.NewBufferString(`<!DOCTYPE html>
// <html>
// <head>
// <title>` + pageInfo.DocumentTitle + `</title>
// <link rel="apple-touch-icon" sizes="180x180" href="/icons/apple-touch-icon.png">
// <link rel="icon" type="image/png" sizes="32x32" href="/icons/favicon-32x32.png">
// <link rel="icon" type="image/png" sizes="16x16" href="/icons/favicon-16x16.png">
// <link rel="manifest" href="/icons/manifest.json">
// <link rel="mask-icon" href="/icons/safari-pinned-tab.svg" color="#5bbad5">
// <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="theme-color" content="#ffffff">
// <meta name="description" content="` + pageInfo.Description + `">
// <meta name="robots" content="index, follow">
// <meta property="og:title" content="` + pageInfo.Title + `">
// <meta property="og:type" content="website">
// <meta property="og:description" content="` + pageInfo.Description + `">
// <meta property="og:image" content="` + pageInfo.Image + `">
// <meta property="og:url" content="` + url.RequestURI() + `">
// <meta name="twitter:title" content="` + pageInfo.Title + `">
// <meta name="twitter:card" content="summary">
// <meta name="twitter:url" content="` + url.RequestURI() + `">
// <meta name="twitter:image" content="` + pageInfo.Image + `">
// <meta name="twitter:description" content="` + pageInfo.Description + `">
// </head>
// <body>
// <script>var cache = cache || {}; cache.root = '` + url.Path + `'; cache.data = `) // continues after render
// json.NewEncoder(buf).Encode(data)
// buf.Write([]byte(`</script>
// <link href=/assets/css/app.e4957b7c640ca81c405e048b4179579f.css rel=stylesheet><div id=app></div><script type=text/javascript src=/assets/js/manifest.0dfb595b05cd8e35b550.js></script><script type=text/javascript src=/assets/js/vendor.52b3c6544255470e9492.js></script><script type=text/javascript src=/assets/js/app.0838ee22ac8e436d61f3.js></script>
// </body>
// `))
// buf.Write([]byte("</html>"))
// flow.respWrt.Header().Set("Content-Type", "text/html")
// flow.respWrt.Header().Set("Content-Length", strconv.Itoa(len(buf.Bytes())))
//_, err = flow.respWrt.Write(buf.Bytes())
// flow.catchAfterErr(err)
// }
// }
// func (flow *flowCtx) Render(status int, buffer *bytes.Buffer) {
//_, err = flow.respWrt.WriteHeader(status)
// flow.catchAfterErr(err)
//_, err = flow.respWrt.Write(buffer.Bytes())
// flow.catchAfterErr(err)
// }
// func (flow *flowCtx) RenderPDF(bytes []byte) {
// flow.respWrt.Header().Set("Content-Type", "application/PDF")
// flow.respWrt.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
//_, err = flow.respWrt.Write(bytes)
// flow.catchAfterErr(err)
// }
// type SiteInfo struct {
// Tagline string
// Sitename string
// }
// type PageInfo struct {
// Title string
// Description string
// URL string
// Image string
// DocumentTitle string
// SiteInfo *SiteInfo
// }
// type ViewBucket struct {
// renderer *render.Render
// store *datastore.Datastore
// w http.ResponseWriter
// req *http.Request
// Data map[string]interface{}
// }
// func NewBucket(flow *flowCtx) *ViewBucket {
// viewBag := ViewBucket{}
// viewBag.w = flow.respWrt
// viewBag.req = flow.req
// viewBag.store = flow.Store
// viewBag.renderer = flow.Renderer
// viewBag.Data = make(map[string]interface{})
// viewBag.Add("Now", time.Now())
// viewBag.Add("Year", time.Now().Year())
// return &viewBag
// }
// func (viewBag *ViewBucket) Add(key string, value interface{}) {
// viewBag.Data[key] = value
// // spew.Dump(viewBag.data)
// }
// func (viewBag *ViewBucket) HTML(status int, templateName string) {
// // automatically show the flash message if it exists
// msg, _ := flash.GetFlash(viewBag.w, viewBag.req, "InfoMessage")
// viewBag.Add("InfoMessage", msg) // if its blank it can be blank but atleast it will exist
// viewBag.renderer.HTML(viewBag.w, status, templateName, viewBag.Data)
// }
// func (viewBag *ViewBucket) Text(status int, text string) {
// viewBag.renderer.Text(viewBag.w, status, text)
// }
// type Settings interface {
// Get(key string) string
// GetBool(key string) bool
// IsProduction() bool
// }