Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Implement support for external HTTP(S) servers
Browse files Browse the repository at this point in the history
Add a new headless WebWire server constructor.
Make the server interface implement the HTTP handler interface.
  • Loading branch information
romshark committed May 16, 2018
1 parent 30790c5 commit 8de01cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
2 changes: 2 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

// Server defines the interface of a webwire server instance
type Server interface {
// ServeHTTP implements the HTTP handler interface
ServeHTTP(resp http.ResponseWriter, req *http.Request)

// Run will luanch the webwire server blocking the calling goroutine
// until the server is either gracefully shut down
Expand Down
63 changes: 39 additions & 24 deletions newServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,46 @@ import (
"sync"
)

// NewServer creates a new WebWire server instance
// NewServer creates a new headed WebWire server instance
// with a built-in HTTP server hosting it
func NewServer(
implementation ServerImplementation,
opts ServerOptions,
) (instance Server, err error) {
instance, err = NewHeadlessServer(implementation, opts)
if err != nil {
return nil, err
}

srv := instance.(*server)

// Initialize HTTP server
srv.httpServer = &http.Server{
Addr: opts.Address,
Handler: srv,
}

// Determine final address
if opts.Address == "" {
opts.Address = ":http"
}

// Initialize TCP/IP listener
srv.listener, err = net.Listen("tcp", opts.Address)
if err != nil {
return nil, fmt.Errorf("Failed setting up TCP/IP listener: %s", err)
}

srv.addr = srv.listener.Addr()

return srv, nil
}

// NewHeadlessServer creates a new headless WebWire server instance
// which relies on an external HTTP server to host it
func NewHeadlessServer(
implementation ServerImplementation,
opts ServerOptions,
) (instance Server, err error) {
if implementation == nil {
panic(fmt.Errorf("A headed webwire server requires a server implementation, got nil"))
Expand All @@ -23,7 +59,7 @@ func NewServer(
sessionsEnabled = true
}

srv := &server{
return &server{
impl: implementation,
sessionManager: opts.SessionManager,
sessionKeyGen: opts.SessionKeyGenerator,
Expand All @@ -44,26 +80,5 @@ func NewServer(
connUpgrader: newConnUpgrader(),
warnLog: opts.WarnLog,
errorLog: opts.ErrorLog,
}

// Initialize HTTP server
srv.httpServer = &http.Server{
Addr: opts.Address,
Handler: srv,
}

// Determine final address
if opts.Address == "" {
opts.Address = ":http"
}

// Initialize TCP/IP listener
srv.listener, err = net.Listen("tcp", opts.Address)
if err != nil {
return nil, fmt.Errorf("Failed setting up TCP/IP listener: %s", err)
}

srv.addr = srv.listener.Addr()

return srv, nil
}, nil
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (srv *server) Shutdown() error {
srv.shutdown = true
// Don't block if there's no currently processed operations
if srv.currentOps < 1 {
srv.opsLock.Unlock()
return srv.shutdownHTTPServer()
}
srv.opsLock.Unlock()
Expand Down

0 comments on commit 8de01cc

Please sign in to comment.