@@ -2,17 +2,13 @@ package flow
2
2
3
3
import (
4
4
"bytes"
5
- "encoding/json"
6
5
"io"
7
6
"net/http"
8
7
"net/url"
9
8
"runtime"
10
9
"strconv"
11
10
"time"
12
11
13
- runner "github.com/helloeave/dat/sqlx-runner"
14
- redis "gopkg.in/redis.v5"
15
-
16
12
"strings"
17
13
18
14
log "github.com/sirupsen/logrus"
@@ -25,85 +21,80 @@ import (
25
21
)
26
22
27
23
type Context struct {
28
- W http.ResponseWriter
29
- Req * http.Request
30
- Store * datastore.Datastore
31
- DB * runner.DB
32
- Cache * redis.Client
33
- Settings * datastore.Settings
34
- Renderer * render.Render
35
- Padlock * security.Padlock
36
- Logger * datastore.Logger
37
- Bucket map [string ]interface {}
38
- errLog string
24
+ W http.ResponseWriter
25
+ Req * http.Request
26
+ Renderer * render.Render
27
+ Padlock * security.Padlock
28
+ Store * datastore.Datastore
29
+ Settings datastore.Settings
30
+ Protocol string
31
+ Bucket map [string ]interface {}
32
+ errLog string
33
+ hasPopulated bool
39
34
}
40
35
41
36
const NO_MASTER = "NO_MASTER"
42
37
43
- func New (w http.ResponseWriter , req * http.Request , store * datastore.Datastore ) * Context {
38
+ // New manages every new request, set shortcuts here, be careful your within a "context" here
39
+ func New (w http.ResponseWriter , req * http.Request , renderer * render.Render , store * datastore.Datastore , key security.Key ) * Context {
44
40
c := & Context {}
45
41
c .W = w
46
42
c .Req = req
47
- c .DB = store .DB
48
- c .Settings = store .Settings
43
+ c .Renderer = renderer
49
44
c .Store = store
50
- c .Cache = store .Cache
51
- c .Renderer = store .Renderer
52
- c .Bucket = make (Bucket )
53
- c .Padlock = security .New (req , store )
54
- if store .Settings .LoggingEnabled {
55
- c .Logger = datastore .NewLogger ()
45
+ c .Settings = store .Settings
46
+ c .Padlock = security .New (req , store .Settings , key )
47
+ c .hasPopulated = false
48
+
49
+ proto := "http://"
50
+ if store .Settings .IsProduction () {
51
+ // should be secure
52
+ proto = "https://"
56
53
}
57
- c .populateCommonVars ()
54
+ if c .Req .Header .Get ("X-Forwarded-Proto" ) == "https" {
55
+ proto = "https://"
56
+ }
57
+ c .Protocol = proto
58
+
59
+ // if store.Settings.LoggingEnabled {
60
+ // c.Logger = datastore.NewLogger()
61
+ // }
58
62
return c
59
63
}
60
64
65
+ func (c * Context ) WebsiteBaseURL () string {
66
+ return c .Store .Settings .Get ("WEBSITE_BASE_URL" )
67
+ }
68
+
61
69
func (c * Context ) SiteID () int {
62
70
return c .Padlock .SiteID () // short hand... will panic if used improperly
63
71
}
64
72
65
- func (c * Context ) GetCacheValue (key string ) (string , error ) {
66
- return c .Store .GetCacheValue (key )
67
- }
73
+ // func (c *Context) GetCacheValue(key string) (string, error) {
74
+ // return c.Store.GetCacheValue(key)
75
+ // }
68
76
69
- func (c * Context ) SetCacheValue (key string , value interface {}, duration time.Duration ) (string , error ) {
70
- return c .Store .SetCacheValue (key , value , duration )
71
- }
77
+ // func (c *Context) SetCacheValue(key string, value interface{}, duration time.Duration) (string, error) {
78
+ // return c.Store.SetCacheValue(key, value, duration)
79
+ // }
72
80
73
81
func (c * Context ) Write (b []byte ) (int , error ) {
74
82
c .errLog += string (b )
75
83
return len (b ), nil
76
84
}
77
85
78
86
func (c * Context ) populateCommonVars () {
79
- proto := c .Settings .Proto
80
- if proto == "" {
81
- proto = "http://"
82
- }
83
-
84
- gaTag := c .Settings .Get ("GA_TAG" )
85
- if gaTag != "" {
86
- c .Add ("GAtag" , gaTag )
87
- }
88
-
89
- facebookRedirectURL := c .Settings .Get ("FACEBOOK_REDIRECT_URL" )
90
- facebookClientID := c .Settings .Get ("FACEBOOK_APP_ID" )
91
- if facebookRedirectURL != "" {
92
- // u, err := url.Parse(facebookRedirectURL)
93
- // if err != nil {
94
- c .Add ("FacebookRedirectURL" , facebookRedirectURL )
95
- // }
96
- }
97
- if facebookClientID != "" {
98
- c .Add ("FacebookAppID" , facebookClientID )
87
+ c .hasPopulated = true
88
+ c .Bucket = make (Bucket )
89
+ proto := "http://"
90
+ if c .Store .Settings .GetBool ("IS_HTTPS" ) {
91
+ proto = "https://"
99
92
}
100
-
101
- loggedInUser , _ := c .Padlock .LoggedInUser ()
93
+ loggedInUser , _ , _ := c .Padlock .LoggedInUser ()
102
94
c .Add ("IsLoggedIn" , loggedInUser != nil )
103
95
if loggedInUser != nil {
104
96
c .Add ("LoggedInUser" , loggedInUser )
105
97
}
106
-
107
98
c .Add ("websiteBaseURL" , proto + c .Req .Host + "/" )
108
99
c .Add ("currentURL" , c .Req .URL .Path )
109
100
c .Add ("currentFullURL" , proto + c .Req .Host + c .Req .URL .Path )
@@ -114,6 +105,9 @@ func (c *Context) populateCommonVars() {
114
105
type Bucket map [string ]interface {}
115
106
116
107
func (c * Context ) Add (key string , value interface {}) {
108
+ if ! c .hasPopulated {
109
+ c .populateCommonVars ()
110
+ }
117
111
c .Bucket [key ] = value
118
112
}
119
113
@@ -264,6 +258,9 @@ func (ctx *Context) HTMLalt(layout string, status int, master string) error {
264
258
return ctx .htmlAlt (ctx .W , layout , status , master )
265
259
}
266
260
func (ctx * Context ) htmlAlt (w io.Writer , layout string , status int , master string ) error {
261
+ if ! ctx .hasPopulated {
262
+ ctx .populateCommonVars ()
263
+ }
267
264
if ctx .Req .URL .Query ().Get ("dump" ) == "1" {
268
265
return ctx .Renderer .HTML (w , status , "error" , ctx .Bucket )
269
266
}
@@ -409,33 +406,33 @@ func (ctx *Context) noTemplateHTML(layout string, status int) {
409
406
ctx .Renderer .HTML (ctx .W , status , layout , ctx .Bucket , opt )
410
407
}
411
408
412
- func (ctx * Context ) BroadcastToCurrentSite (t string , data interface {}) error {
413
- s := strconv .Itoa (ctx .SiteID ())
414
- return ctx .Broadcast ("room-" + s , t , data )
415
- }
409
+ // func (ctx *Context) BroadcastToCurrentSite(t string, data interface{}) error {
410
+ // s := strconv.Itoa(ctx.SiteID())
411
+ // return ctx.Broadcast("room-"+s, t, data)
412
+ // }
416
413
417
- func (ctx * Context ) BroadcastToSite (siteID int , t string , data interface {}) error {
418
- s := strconv .Itoa (siteID )
419
- return ctx .Broadcast ("room-" + s , t , data )
420
- }
414
+ // func (ctx *Context) BroadcastToSite(siteID int, t string, data interface{}) error {
415
+ // s := strconv.Itoa(siteID)
416
+ // return ctx.Broadcast("room-"+s, t, data)
417
+ // }
421
418
422
- func (ctx * Context ) Broadcast (room string , t string , data interface {}) error {
423
- bc := & broadcast {
424
- Type : strings .Title (t ),
425
- Data : data ,
426
- }
427
- b , err := json .Marshal (bc )
428
- if err != nil {
429
- return err
430
- }
431
- err = ctx .Store .Websocket .Broadcast (room , string (b ))
432
- return err
433
- }
419
+ // func (ctx *Context) Broadcast(room string, t string, data interface{}) error {
420
+ // bc := &broadcast{
421
+ // Type: strings.Title(t),
422
+ // Data: data,
423
+ // }
424
+ // b, err := json.Marshal(bc)
425
+ // if err != nil {
426
+ // return err
427
+ // }
428
+ // err = ctx.Store.Websocket.Broadcast(room, string(b))
429
+ // return err
430
+ // }
434
431
435
- type broadcast struct {
436
- Type string
437
- Data interface {}
438
- }
432
+ // type broadcast struct {
433
+ // Type string
434
+ // Data interface{}
435
+ // }
439
436
440
437
// func (ctx *Context) SPA(status int, pageInfo *PageInfo, data interface{}) {
441
438
// pageInfo.DocumentTitle = pageInfo.Title
@@ -554,3 +551,9 @@ type broadcast struct {
554
551
// func (viewBag *ViewBucket) Text(status int, text string) {
555
552
// viewBag.renderer.Text(viewBag.w, status, text)
556
553
// }
554
+
555
+ // type Settings interface {
556
+ // Get(key string) string
557
+ // GetBool(key string) bool
558
+ // IsProduction() bool
559
+ // }
0 commit comments