Skip to content

Commit

Permalink
Improve the expansion of the session
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Sep 30, 2019
1 parent 52e237d commit ab623dd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
40 changes: 36 additions & 4 deletions README.md
Expand Up @@ -59,8 +59,8 @@ import (
)

func main() {
app := thinkgo.BootStrap()
app.RegisterRoute(func(route *router.Route) {
think := thinkgo.BootStrap()
think.RegisterRoute(func(route *router.Route) {

route.Get("/", func(req *context.Request) *context.Response {
return thinkgo.Text("Hello ThinkGo !")
Expand All @@ -78,7 +78,7 @@ func main() {
})
})
// listen and serve on 0.0.0.0:9011
app.Run()
think.Run()
}
```

Expand All @@ -102,7 +102,7 @@ func main() {
The most basic routes accept a URI and a Closure, providing a very simple and expressive method of defining routes:

```go
app.RegisterRoute(func(route *router.Route) {
think.RegisterRoute(func(route *router.Route) {
route.Get("/foo", func(req *context.Request) *context.Response {
return thinkgo.Text("Hello ThinkGo !")
})
Expand Down Expand Up @@ -381,6 +381,19 @@ route.Get("/tpl", func(request *context.Request) *context.Response {

## HTTP Session

When the app starts, you need to register the session handler.

```go
think.RegisterHandler(app.NewSessionHandler)
```

`ThinkGo` ships with several great drivers out of the box:

- cookie - sessions are stored in cookies
- file - sessions are stored in files.

#### Using The Session

retrieving Data like this:

```go
Expand All @@ -393,6 +406,25 @@ storing Data like this:
request.Session().Set("user", "alice")
```

#### Adding Custom Session Drivers

Your custom session driver should implement the `Handler`.

```go
type Handler interface {
Read(id string) string
Write(id string, data string)
}
```

Once your driver has been implemented, you are ready to register it:

```go
import "github.com/thinkoner/thinkgo/session"

session.Extend("my_session", MySessionHandler)
```

## Logging

The logger provides the eight logging levels defined in [RFC 5424]( https://tools.ietf.org/html/rfc5424 ): **emergency**, **alert**, **critical**, **error**, **warning**, **notice**, **info** and **debug**.
Expand Down
8 changes: 4 additions & 4 deletions app/session_handler.go
Expand Up @@ -7,14 +7,14 @@ import (
)

type SessionHandler struct {
manager *session.Manager
Manager *session.Manager
app *Application
}

// SessionHandler The default SessionHandler
func NewSessionHandler(app *Application) Handler {
handler := &SessionHandler{}
handler.manager = session.NewManager(&session.Config{
handler.Manager = session.NewManager(&session.Config{
Driver: config.Session.Driver,
CookieName: config.Session.CookieName,
Lifetime: config.Session.Lifetime,
Expand Down Expand Up @@ -42,9 +42,9 @@ func (h *SessionHandler) Process(req *context.Request, next Closure) interface{}
}

func (h *SessionHandler) startSession(req *context.Request) *session.Store {
return h.manager.SessionStart(req)
return h.Manager.SessionStart(req)
}

func (h *SessionHandler) saveSession(res session.Response) {
h.manager.SessionSave(res)
h.Manager.SessionSave(res)
}
35 changes: 29 additions & 6 deletions session/manager.go
Expand Up @@ -4,6 +4,8 @@ import (
"time"
)

var customHandlers map[string]Handler

type Config struct {
//Default Session Driver
Driver string
Expand All @@ -21,21 +23,21 @@ type Config struct {
}

type Manager struct {
store *Store
Config *Config
store *Store
Config *Config
}

func NewManager(config *Config) *Manager {
m := &Manager{
Config: config,
}
m.store = m.buildSession(
m.parseStoreHandler(),
)

return m
}

func (m *Manager) SessionStart(req Request) *Store {
m.parseStore()

if handler, ok := m.usingCookieSessions(); ok {
handler.SetRequest(req)
}
Expand All @@ -54,6 +56,13 @@ func (m *Manager) SessionSave(res Response) *Store {
return m.store
}

func Extend(driver string, handler Handler) {
if customHandlers == nil {
customHandlers = make(map[string]Handler)
}
customHandlers[driver] = handler
}

func (m *Manager) buildSession(handler Handler) *Store {
store := NewStore(m.Config.CookieName, handler)
return store
Expand All @@ -64,6 +73,16 @@ func (m *Manager) usingCookieSessions() (handler *CookieHandler, ok bool) {
return
}

func (m *Manager) parseStore() {
if m.store != nil {
return
}

m.store = m.buildSession(
m.parseStoreHandler(),
)
}

func (m *Manager) parseStoreHandler() Handler {
var storeHandler Handler
switch m.Config.Driver {
Expand All @@ -75,7 +94,11 @@ func (m *Manager) parseStoreHandler() Handler {
Lifetime: m.Config.Lifetime,
}
default:
panic("Unsupported session driver: " + m.Config.Driver)
var ok bool
storeHandler, ok = customHandlers[m.Config.Driver]
if !ok {
panic("Unsupported session driver: " + m.Config.Driver)
}
}

return storeHandler
Expand Down
12 changes: 6 additions & 6 deletions thinkgo_test.go
Expand Up @@ -31,16 +31,16 @@ func testRequest(t *testing.T, url string) {
}

func TestRunEmpty(t *testing.T) {
app := BootStrap()
think := BootStrap()

go func() {
app.RegisterRoute(func(route *router.Route) {
think.RegisterRoute(func(route *router.Route) {
route.Get("/", func(req *context.Request) *context.Response {
return Text("it worked")
})
})
// listen and serve on 0.0.0.0:9011
app.Run()
think.Run()
}()

time.Sleep(5 * time.Millisecond)
Expand All @@ -49,16 +49,16 @@ func TestRunEmpty(t *testing.T) {
}

func TestRunWithPort(t *testing.T) {
app := BootStrap()
think := BootStrap()

go func() {
app.RegisterRoute(func(route *router.Route) {
think.RegisterRoute(func(route *router.Route) {
route.Get("/", func(req *context.Request) *context.Response {
return Text("it worked")
})
})
// listen and serve on 0.0.0.0:9011
app.Run(":8100")
think.Run(":8100")
}()

time.Sleep(5 * time.Millisecond)
Expand Down

0 comments on commit ab623dd

Please sign in to comment.