Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sessions pluggable

  • Loading branch information...
commit 97ba086ccc87aab9bcf370f17dd854ddf7b837ae 1 parent 54cd679
@brendonh authored
Showing with 33 additions and 17 deletions.
  1. +14 −3 httprpc.go
  2. +2 −2 service.go
  3. +5 −5 session.go
  4. +10 −5 types.go
  5. +2 −2 websocket.go
View
17 httprpc.go
@@ -9,20 +9,32 @@ import (
"encoding/json"
)
+type SessionResolver func(*http.Request) Session
+
type HttpRpcEndpoint struct {
Address string
listener net.Listener
context ServerContext
+ resolver SessionResolver
}
-func NewHttpRpcEndpoint(address string, context ServerContext) Endpoint {
+func NewHttpRpcEndpoint(address string, context ServerContext, resolver SessionResolver) Endpoint {
+ if resolver == nil {
+ resolver = DefaultSessionResolver
+ }
return &HttpRpcEndpoint{
Address: address,
context: context,
+ resolver: resolver,
}
}
+func DefaultSessionResolver(req *http.Request) Session {
+ // XXX TODO: Session tracking
+ return NewBasicSession()
+}
+
func (endpoint *HttpRpcEndpoint) Start() bool {
if endpoint.listener != nil {
return false
@@ -77,8 +89,7 @@ func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *ht
form[k] = v[0]
}
- // XXX TODO: Session tracking
- var session = NewSession()
+ var session = endpoint.resolver(req)
ok, errors, resp := endpoint.context.API().HandleCall(
bits[0], bits[1], form, session, endpoint.context)
View
4 service.go
@@ -66,7 +66,7 @@ var requestArgSpec = []APIArg {
APIArg{Name: "data", ArgType: RawArg},
}
-func (collection ServiceCollection) HandleRequest(request APIData, session *Session, context ServerContext) APIData {
+func (collection ServiceCollection) HandleRequest(request APIData, session Session, context ServerContext) APIData {
ok, resolutionErrors, args := Parse(requestArgSpec, request)
if !ok {
@@ -85,7 +85,7 @@ func (collection ServiceCollection) HandleCall(
serviceName string,
methodName string,
data APIData,
- session *Session,
+ session Session,
context ServerContext) (bool, []string, APIData) {
View
10 session.go
@@ -8,8 +8,8 @@ import (
)
-func NewSession() *Session {
- return &Session {
+func NewBasicSession() *BasicSession {
+ return &BasicSession {
id: uuid.New(),
user: nil,
Mutex: new(sync.Mutex),
@@ -21,15 +21,15 @@ func NewSession() *Session {
// Session API
// ------------------------------------------
-func (session *Session) ID() string {
+func (session *BasicSession) ID() string {
return session.id;
}
-func (session *Session) User() User {
+func (session *BasicSession) User() User {
return session.user;
}
-func (session *Session) SetUser(user User) {
+func (session *BasicSession) SetUser(user User) {
fmt.Printf("Session login: %s (%s)\n", user.DisplayName(), session.id)
session.user = user
}
View
15 types.go
@@ -28,13 +28,18 @@ type User interface {
DisplayName() string
}
-type Session struct {
+type Session interface {
+ ID() string
+ User() User
+ SetUser(User)
+}
+
+type BasicSession struct {
id string
user User
*sync.Mutex
}
-
// ------------------------------------------
// API
// ------------------------------------------
@@ -63,7 +68,7 @@ type APIMethod struct {
type APIData map[string]interface{}
-type APIHandler func(APIData, *Session, ServerContext) (bool, APIData)
+type APIHandler func(APIData, Session, ServerContext) (bool, APIData)
// ------------------------------------------
@@ -78,6 +83,6 @@ type APIService interface {
type API interface {
AddService(APIService)
- HandleRequest(APIData, *Session, ServerContext) APIData
- HandleCall(string, string, APIData, *Session, ServerContext) (bool, []string, APIData)
+ HandleRequest(APIData, Session, ServerContext) APIData
+ HandleCall(string, string, APIData, Session, ServerContext) (bool, []string, APIData)
}
View
4 websocket.go
@@ -78,7 +78,7 @@ func (endpoint *WebsocketEndpoint) Handle(ws *websocket.Conn) {
ws.PayloadType = websocket.BinaryFrame
var buf = make([]byte, 1024 * 64)
- var session *Session = NewSession()
+ var session Session = NewBasicSession()
fmt.Printf("New session: %s\n", session.ID())
@@ -113,7 +113,7 @@ func (endpoint *WebsocketEndpoint) Handle(ws *websocket.Conn) {
}
-func (endpoint *WebsocketEndpoint) HandleAPI(buf []byte, session *Session, ws *websocket.Conn) {
+func (endpoint *WebsocketEndpoint) HandleAPI(buf []byte, session Session, ws *websocket.Conn) {
var data APIData
var resolver = msgpack.DefaultDecoderContainerResolver
resolver.MapType = reflect.TypeOf(make(APIData))
Please sign in to comment.
Something went wrong with that request. Please try again.