Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 2ef15df

Browse files
committed
Routine Commit
1 parent f5ebbc8 commit 2ef15df

File tree

1 file changed

+81
-78
lines changed

1 file changed

+81
-78
lines changed

flow.go

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ package flow
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"io"
76
"net/http"
87
"net/url"
98
"runtime"
109
"strconv"
1110
"time"
1211

13-
runner "github.com/helloeave/dat/sqlx-runner"
14-
redis "gopkg.in/redis.v5"
15-
1612
"strings"
1713

1814
log "github.com/sirupsen/logrus"
@@ -25,85 +21,80 @@ import (
2521
)
2622

2723
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
3934
}
4035

4136
const NO_MASTER = "NO_MASTER"
4237

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 {
4440
c := &Context{}
4541
c.W = w
4642
c.Req = req
47-
c.DB = store.DB
48-
c.Settings = store.Settings
43+
c.Renderer = renderer
4944
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://"
5653
}
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+
// }
5862
return c
5963
}
6064

65+
func (c *Context) WebsiteBaseURL() string {
66+
return c.Store.Settings.Get("WEBSITE_BASE_URL")
67+
}
68+
6169
func (c *Context) SiteID() int {
6270
return c.Padlock.SiteID() // short hand... will panic if used improperly
6371
}
6472

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+
// }
6876

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+
// }
7280

7381
func (c *Context) Write(b []byte) (int, error) {
7482
c.errLog += string(b)
7583
return len(b), nil
7684
}
7785

7886
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://"
9992
}
100-
101-
loggedInUser, _ := c.Padlock.LoggedInUser()
93+
loggedInUser, _, _ := c.Padlock.LoggedInUser()
10294
c.Add("IsLoggedIn", loggedInUser != nil)
10395
if loggedInUser != nil {
10496
c.Add("LoggedInUser", loggedInUser)
10597
}
106-
10798
c.Add("websiteBaseURL", proto+c.Req.Host+"/")
10899
c.Add("currentURL", c.Req.URL.Path)
109100
c.Add("currentFullURL", proto+c.Req.Host+c.Req.URL.Path)
@@ -114,6 +105,9 @@ func (c *Context) populateCommonVars() {
114105
type Bucket map[string]interface{}
115106

116107
func (c *Context) Add(key string, value interface{}) {
108+
if !c.hasPopulated {
109+
c.populateCommonVars()
110+
}
117111
c.Bucket[key] = value
118112
}
119113

@@ -264,6 +258,9 @@ func (ctx *Context) HTMLalt(layout string, status int, master string) error {
264258
return ctx.htmlAlt(ctx.W, layout, status, master)
265259
}
266260
func (ctx *Context) htmlAlt(w io.Writer, layout string, status int, master string) error {
261+
if !ctx.hasPopulated {
262+
ctx.populateCommonVars()
263+
}
267264
if ctx.Req.URL.Query().Get("dump") == "1" {
268265
return ctx.Renderer.HTML(w, status, "error", ctx.Bucket)
269266
}
@@ -409,33 +406,33 @@ func (ctx *Context) noTemplateHTML(layout string, status int) {
409406
ctx.Renderer.HTML(ctx.W, status, layout, ctx.Bucket, opt)
410407
}
411408

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+
// }
416413

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+
// }
421418

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+
// }
434431

435-
type broadcast struct {
436-
Type string
437-
Data interface{}
438-
}
432+
// type broadcast struct {
433+
// Type string
434+
// Data interface{}
435+
// }
439436

440437
// func (ctx *Context) SPA(status int, pageInfo *PageInfo, data interface{}) {
441438
// pageInfo.DocumentTitle = pageInfo.Title
@@ -554,3 +551,9 @@ type broadcast struct {
554551
// func (viewBag *ViewBucket) Text(status int, text string) {
555552
// viewBag.renderer.Text(viewBag.w, status, text)
556553
// }
554+
555+
// type Settings interface {
556+
// Get(key string) string
557+
// GetBool(key string) bool
558+
// IsProduction() bool
559+
// }

0 commit comments

Comments
 (0)