Skip to content

Commit

Permalink
Allow changing the prefix for cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
robfig committed Oct 14, 2012
1 parent b84f304 commit 0732377
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions mvc.go
Expand Up @@ -240,7 +240,7 @@ func (c *Controller) Invoke(appControllerPtr reflect.Value, method reflect.Value
flashValue += "\x00" + key + ":" + value + "\x00"
}
c.SetCookie(&http.Cookie{
Name: "REVEL_FLASH",
Name: COOKIE_PREFIX + "_FLASH",
Value: url.QueryEscape(flashValue),
Path: "/",
})
Expand All @@ -255,7 +255,7 @@ func (c *Controller) Invoke(appControllerPtr reflect.Value, method reflect.Value
}
}
c.SetCookie(&http.Cookie{
Name: "REVEL_ERRORS",
Name: COOKIE_PREFIX + "_ERRORS",
Value: url.QueryEscape(errorsValue),
Path: "/",
})
Expand All @@ -267,7 +267,7 @@ func (c *Controller) Invoke(appControllerPtr reflect.Value, method reflect.Value
}
sessionData := url.QueryEscape(sessionValue)
c.SetCookie(&http.Cookie{
Name: "REVEL_SESSION",
Name: COOKIE_PREFIX + "_SESSION",
Value: Sign(sessionData) + "-" + sessionData,
Path: "/",
})
Expand Down Expand Up @@ -502,7 +502,7 @@ func restoreFlash(req *http.Request) Flash {
Data: make(map[string]string),
Out: make(map[string]string),
}
if cookie, err := req.Cookie("REVEL_FLASH"); err == nil {
if cookie, err := req.Cookie(COOKIE_PREFIX + "_FLASH"); err == nil {
ParseKeyValueCookie(cookie.Value, func(key, val string) {
flash.Data[key] = val
})
Expand All @@ -513,7 +513,7 @@ func restoreFlash(req *http.Request) Flash {
// Restore Validation.Errors from a request.
func restoreValidationErrors(req *http.Request) []*ValidationError {
errors := make([]*ValidationError, 0, 5)
if cookie, err := req.Cookie("REVEL_ERRORS"); err == nil {
if cookie, err := req.Cookie(COOKIE_PREFIX + "_ERRORS"); err == nil {
ParseKeyValueCookie(cookie.Value, func(key, val string) {
errors = append(errors, &ValidationError{
Key: key,
Expand All @@ -526,7 +526,7 @@ func restoreValidationErrors(req *http.Request) []*ValidationError {

func restoreSession(req *http.Request) Session {
session := make(map[string]string)
cookie, err := req.Cookie("REVEL_SESSION")
cookie, err := req.Cookie(COOKIE_PREFIX + "_SESSION")
if err != nil {
return Session(session)
}
Expand Down Expand Up @@ -656,6 +656,14 @@ func (resp *Response) WriteHeader(defaultStatusCode int, defaultContentType stri
resp.Out.WriteHeader(resp.Status)
}

var COOKIE_PREFIX string

func init() {
InitHooks = append(InitHooks, func() {
COOKIE_PREFIX = Config.StringDefault("session.cookie", "REVEL")
})
}

// Internal bookeeping

type ControllerType struct {
Expand Down
1 change: 1 addition & 0 deletions skeleton/conf/app.conf.template
Expand Up @@ -2,6 +2,7 @@ app.name={{ .AppName }}
app.secret={{ .Secret }}
http.addr=
http.port=9000
session.cookie=REVEL

[dev]
results.pretty=true
Expand Down

0 comments on commit 0732377

Please sign in to comment.