-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Session extension improvements, fewer props considered unsafe for use…
…r model
- Loading branch information
1 parent
6ffcb1c
commit 7e23ff0
Showing
5 changed files
with
124 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# Nibbler Session | ||
|
||
Provides a session for the server to use. | ||
|
||
Users can provide their own connector, or a Cookie connector will be used | ||
if one is not provided. It is recommended that users use their own connector | ||
for production apps. A sample has been provided in ./sample.connector. | ||
|
||
The default MaxAge is 30 days (86400 * 30) in all cases (which is pretty long). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gorilla/sessions" | ||
"github.com/jinzhu/gorm" | ||
"github.com/markdicksonjr/nibbler" | ||
"github.com/markdicksonjr/nibbler/database/sql" | ||
"github.com/markdicksonjr/nibbler/session" | ||
"github.com/markdicksonjr/nibbler/user" | ||
NibUserSql "github.com/markdicksonjr/nibbler/user/database/sql" | ||
_ "github.com/michaeljs1990/sqlitestore" | ||
"github.com/wader/gormstore" | ||
"log" | ||
) | ||
|
||
type SqlMemoryStoreConnector struct { | ||
} | ||
|
||
func (s SqlMemoryStoreConnector) Connect() (error, sessions.Store) { | ||
db, err := gorm.Open("sqlite3", ":memory:") | ||
|
||
if err != nil { | ||
return err, nil | ||
} | ||
|
||
store := gormstore.NewOptions(db, | ||
gormstore.Options{}, | ||
[]byte("some-key"), | ||
) | ||
|
||
store.SessionOpts.MaxAge = 60 * 60 * 24 * 15 // 15 days | ||
|
||
return nil, store | ||
} | ||
|
||
func main() { | ||
|
||
// allocate logger and configuration | ||
var logger nibbler.Logger = nibbler.DefaultLogger{} | ||
|
||
// allocate configuration | ||
config, err := nibbler.LoadConfiguration(nil) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// prepare models for initialization | ||
var models []interface{} | ||
models = append(models, user.User{}) | ||
|
||
// allocate an SQL controller, providing an sql extension | ||
sqlController := NibUserSql.Extension{ | ||
SqlExtension: &sql.Extension{ | ||
Models: models, | ||
}, | ||
} | ||
|
||
// allocate user extension, providing sql extension to it | ||
userExtension := user.Extension{ | ||
PersistenceExtension: &sqlController, // &elasticController, | ||
} | ||
|
||
// allocate session extension, with an optional custom connector | ||
var sessionConnector session.SessionStoreConnector = &SqlMemoryStoreConnector{} | ||
sessionExtension := session.Extension{ | ||
StoreConnector: &sessionConnector, | ||
} | ||
|
||
// prepare extensions for initialization | ||
extensions := []nibbler.Extension{ | ||
sqlController.SqlExtension, | ||
&userExtension, | ||
&sessionExtension, | ||
} | ||
|
||
// initialize the application | ||
appContext := nibbler.Application{} | ||
err = appContext.Init(config, &logger, &extensions) | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
// start the app | ||
err = appContext.Run() | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters