Skip to content

Commit

Permalink
Updated session package for consistency and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jul 4, 2015
1 parent 0fb3fd8 commit b0f6707
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -23,7 +23,7 @@ Navigate to the login page, and then to the register page. Create a new user and

The web app has a public home page, authenticated home page, login page, register page, and about page.

The entrypoint for the web app is gowebapp.go. The file loads the initial configuration,
The entrypoint for the web app is gowebapp.go. The file loads the application settings,
starts the session, connects to the database, sets up the templates, loads
the routes, attaches the middleware, and starts the web server.

Expand Down Expand Up @@ -174,7 +174,7 @@ Handle the database query:

~~~ go
// Get database result
result, err := database.UserByEmail(email)
result, err := model.UserByEmail(email)

// Determine if password is correct
if err == sql.ErrNoRows {
Expand All @@ -192,7 +192,7 @@ if err == sql.ErrNoRows {

It's a good idea to abstract the database layer out so if you need to make
changes, you don't have to look through business logic to find the queries. All
the queries are stored in database.go:
the queries are stored in the models folder:

Connect to the database (only once needed in your application):

Expand Down Expand Up @@ -220,8 +220,9 @@ return err

To make the web app a little more flexible, you can make changes to different
components in one place through the config.json file. If you want to add any
of your own settings, you can add them to config.json and then to config.go so
you can reference them in your code. This is config.json:
of your own settings, you can add them to config.json and update the structs
in gowebapp.go and the individual files so you can reference them in your code.
This is config.json:

~~~ json
{
Expand Down
7 changes: 3 additions & 4 deletions gowebapp.go
Expand Up @@ -31,9 +31,8 @@ func main() {
// Load the configuration file
jsonconfig.Load("config"+string(os.PathSeparator)+"config.json", config)

// Start the session
session.Start(config.Session.SecretKey, config.Session.Options,
config.Session.Name)
// Configure the session cookie store
session.Configure(config.Session)

// Connect to database
database.Connect(config.Database)
Expand All @@ -44,7 +43,7 @@ func main() {
view.LoadPlugins(plugin.TemplateFuncMap(config.View))

// Start the listener
server.Run(route.Load(config.Session), config.Server)
server.Run(route.Load(), config.Server)
}

// *****************************************************************************
Expand Down
8 changes: 4 additions & 4 deletions route/route.go
Expand Up @@ -15,8 +15,8 @@ import (
)

// Load the routes and middleware
func Load(s session.Session) http.Handler {
return middleware(s, routes())
func Load() http.Handler {
return middleware(routes())
}

// *****************************************************************************
Expand Down Expand Up @@ -57,9 +57,9 @@ func routes() *httprouter.Router {
// Middleware
// *****************************************************************************

func middleware(s session.Session, h http.Handler) http.Handler {
func middleware(h http.Handler) http.Handler {
// Prevents CSRF and Double Submits
cs := csrfbanana.New(h, session.Store, s.Name)
cs := csrfbanana.New(h, session.Store, session.Name)
cs.FailureHandler(http.HandlerFunc(controller.InvalidToken))
cs.ClearAfterUsage(true)
cs.ExcludeRegexPaths([]string{"/static(.*)"})
Expand Down
14 changes: 8 additions & 6 deletions shared/session/session.go
Expand Up @@ -7,8 +7,10 @@ import (
)

var (
// Store is the cookie store
Store *sessions.CookieStore
Name string
// Name is the session name
Name string
)

// Session stores session level information
Expand All @@ -18,11 +20,11 @@ type Session struct {
SecretKey string `json:"SecretKey"` // Key for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.New
}

// Start a session
func Start(secretKey string, options sessions.Options, name string) {
Store = sessions.NewCookieStore([]byte(secretKey))
Store.Options = &options
Name = name
// Configure the session cookie store
func Configure(s Session) {
Store = sessions.NewCookieStore([]byte(s.SecretKey))
Store.Options = &s.Options
Name = s.Name
}

// Session returns a new session, never returns an error
Expand Down

0 comments on commit b0f6707

Please sign in to comment.