Permalink
Browse files

Improvements to HTTP sessions and static content

  • Loading branch information...
1 parent 7d2a644 commit 76f9570fac2c03f0f527eac80507d28025312291 Brendon Hogger committed Mar 14, 2013
Showing with 51 additions and 15 deletions.
  1. +43 −13 httprpc.go
  2. +5 −2 server.go
  3. +3 −0 websocket.go
View
@@ -9,27 +9,56 @@ import (
"encoding/json"
)
-type SessionResolver func(*http.Request, *HttpRpcEndpoint) (Session, error)
+type SessionResolver func(*http.Request, http.ResponseWriter, *HttpRpcEndpoint) (Session, error)
type HttpRpcEndpoint struct {
Address string
+ Mux *http.ServeMux
listener net.Listener
context ServerContext
resolver SessionResolver
+ stripLength int
logPrefix string
}
-func NewHttpRpcEndpoint(address string, context ServerContext, resolver SessionResolver) Endpoint {
- if resolver == nil {
- resolver = DefaultSessionResolver
+type HttpRpcEndpointOptions struct {
+ Resolver SessionResolver
+ Static bool
+ StaticPath string
+ StaticUri string
+ APIUri string
+}
+
+var defaultOptions = &HttpRpcEndpointOptions{
+ Resolver: DefaultSessionResolver,
+ Static: false,
+ APIUri: "/",
+}
+
+func NewHttpRpcEndpoint(address string, context ServerContext, options *HttpRpcEndpointOptions) Endpoint {
+ if options == nil {
+ options = defaultOptions
}
- return &HttpRpcEndpoint{
+
+ var mux = http.NewServeMux()
+ mux.HandleFunc("/favicon.ico", http.NotFound)
+
+ if options.Static {
+ mux.Handle(options.StaticUri, http.FileServer(http.Dir(options.StaticPath)))
+ }
+
+ var endpoint = &HttpRpcEndpoint{
+ Mux: mux,
Address: address,
context: context,
- resolver: resolver,
+ resolver: options.Resolver,
+ stripLength: len(options.APIUri),
logPrefix: "HTTP " + address,
}
+ mux.Handle(options.APIUri, endpoint)
+
+ return endpoint
}
@@ -41,7 +70,7 @@ func (sessConn *HttpSessionConnection) Send(msg []byte) {
}
-func DefaultSessionResolver(req *http.Request, endpoint *HttpRpcEndpoint) (Session, error) {
+func DefaultSessionResolver(req *http.Request, response http.ResponseWriter, endpoint *HttpRpcEndpoint) (Session, error) {
// XXX TODO: Session tracking, sending
var sender = &HttpSessionConnection{}
return endpoint.context.CreateSession(sender), nil
@@ -60,10 +89,7 @@ func (endpoint *HttpRpcEndpoint) Start() bool {
endpoint.listener = listener
- mux := http.NewServeMux()
- mux.HandleFunc("/favicon.ico", http.NotFound)
- mux.Handle("/", endpoint)
- go http.Serve(listener, mux)
+ go http.Serve(listener, endpoint.Mux)
endpoint.Log("HTTP endpoint started at %s", endpoint.Address)
@@ -85,12 +111,16 @@ func (endpoint *HttpRpcEndpoint) Stop() bool {
return true
}
+func (endpoint *HttpRpcEndpoint) Context() ServerContext {
+ return endpoint.context
+}
+
func (endpoint *HttpRpcEndpoint) Log(fmt string, args... interface{}) {
endpoint.context.LogPrefix(endpoint.logPrefix, fmt, args...)
}
func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *http.Request) {
- bits := strings.SplitN(req.URL.Path[1:], "/", 2)
+ bits := strings.SplitN(req.URL.Path[endpoint.stripLength:], "/", 2)
if len(bits) != 2 {
http.NotFound(response, req)
@@ -104,7 +134,7 @@ func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *ht
form[k] = v[0]
}
- var session, err = endpoint.resolver(req, endpoint)
+ var session, err = endpoint.resolver(req, response, endpoint)
if err != nil {
response.WriteHeader(400)
response.Header().Add("Content-Type", "text/plain")
View
@@ -15,12 +15,15 @@ type Server struct {
stopper chan os.Signal
}
-func NewServer(services API, sessionCreator SessionCreator) *Server {
+func NewServer(services API, sessionCreator SessionCreator, logger *log.Logger) *Server {
+ if logger == nil {
+ logger = log.New(os.Stdout, "", log.LstdFlags)
+ }
return &Server {
services: services,
sessionCreator: sessionCreator,
endpoints: make([]Endpoint, 0),
- logger: log.New(os.Stdout, "", log.LstdFlags),
+ logger: logger,
}
}
View
@@ -98,6 +98,9 @@ func (endpoint *WebsocketEndpoint) Stop() bool {
return true
}
+func (endpoint *WebsocketEndpoint) Context() ServerContext {
+ return endpoint.context
+}
func (endpoint *WebsocketEndpoint) Handle(ws *websocket.Conn) {
ws.PayloadType = websocket.BinaryFrame

0 comments on commit 76f9570

Please sign in to comment.