@ -23,6 +23,7 @@ import (
"fmt"
"fmt"
"net/http"
"net/http"
"net/url"
"net/url"
"reflect"
"time"
"time"
)
)
@ -291,17 +292,34 @@ type Provider interface {
GC ( )
GC ( )
}
}
var providers = make ( map [ string ] Provider )
var providers = make ( map [ string ] func ( ) Provider )
// Register registers a provider.
// Register registers a provider.
func Register ( name string , provider Provider ) {
func Register ( name string , provider Provider ) {
if provider == nil {
if reflect . TypeOf ( provider ) . Kind ( ) == reflect . Ptr {
// Pointer:
RegisterFn ( name , func ( ) Provider {
return reflect . New ( reflect . ValueOf ( provider ) . Elem ( ) . Type ( ) ) . Interface ( ) . ( Provider )
} )
return
}
// Not a Pointer
RegisterFn ( name , func ( ) Provider {
return reflect . New ( reflect . TypeOf ( provider ) ) . Elem ( ) . Interface ( ) . ( Provider )
} )
}
// RegisterFn registers a provider function.
func RegisterFn ( name string , providerfn func ( ) Provider ) {
if providerfn == nil {
panic ( "session: cannot register provider with nil value" )
panic ( "session: cannot register provider with nil value" )
}
}
if _ , dup := providers [ name ] ; dup {
if _ , dup := providers [ name ] ; dup {
panic ( fmt . Errorf ( "session: cannot register provider '%s' twice" , name ) )
panic ( fmt . Errorf ( "session: cannot register provider '%s' twice" , name ) )
}
}
providers [ name ] = provider
providers [ name ] = providerfn
}
}
// _____
// _____
@ -318,12 +336,15 @@ type Manager struct {
}
}
// NewManager creates and returns a new session manager by given provider name and configuration.
// NewManager creates and returns a new session manager by given provider name and configuration.
// It panics when given provider isn't registered.
// It returns an error when requested provider name isn't registered.
func NewManager ( name string , opt Options ) ( * Manager , error ) {
func NewManager ( name string , opt Options ) ( * Manager , error ) {
p , ok := providers [ name ]
fn , ok := providers [ name ]
if ! ok {
if ! ok {
return nil , fmt . Errorf ( "session: unknown provider '%s'(forgotten import?)" , name )
return nil , fmt . Errorf ( "session: unknown provider '%s'(forgotten import?)" , name )
}
}
p := fn ( )
return & Manager { p , opt } , p . Init ( opt . Maxlifetime , opt . ProviderConfig )
return & Manager { p , opt } , p . Init ( opt . Maxlifetime , opt . ProviderConfig )
}
}