Skip to content

Commit

Permalink
godoc, cleanup, create engine with aah app and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed May 22, 2018
1 parent 2f7528c commit 1db9266
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
type Context struct {
Req *Request
Conn net.Conn
Header http.Header
Header http.Header // These headers are sent to WS client during Connection upgrade

e *Engine
hs gws.Handshake
Expand Down
15 changes: 10 additions & 5 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ type IDGenerator func(ctx *Context) string
// EventCallbackFunc func type used for all WebSocket event callback.
type EventCallbackFunc func(eventName string, ctx *Context)

// aah application interface for minimal purpose
type application interface {
Config() *config.Config
Router() *router.Router
Log() log.Loggerer
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Engine type and its methods
//______________________________________________________________________________
Expand All @@ -70,15 +77,13 @@ type EventCallbackFunc func(eventName string, ctx *Context)
type Engine struct {
checkOrigin bool
originWhitelist []*url.URL
cfg *config.Config
router *router.Router
app application
registry *ainsp.TargetRegistry
onPreConnect EventCallbackFunc
onPostConnect EventCallbackFunc
onPostDisconnect EventCallbackFunc
onError EventCallbackFunc
idGenerator IDGenerator
logger log.Loggerer
}

// AddWebSocket method adds the given WebSocket implementation into engine.
Expand Down Expand Up @@ -133,7 +138,7 @@ func (e *Engine) SetIDGenerator(g IDGenerator) {
// Along with Check Origin, aah WebSocket events such as `OnPreConnect`,
// `OnPostConnect`, `OnPostDisconnect` and `OnError`.
func (e *Engine) Handle(w http.ResponseWriter, r *http.Request) {
domain := e.router.Lookup(ahttp.Host(r))
domain := e.app.Router().Lookup(ahttp.Host(r))
if domain == nil {
e.Log().Errorf("WS: domain not found: %s", ahttp.Host(r))
e.replyError(w, http.StatusNotFound)
Expand Down Expand Up @@ -173,7 +178,7 @@ func (e *Engine) Handle(w http.ResponseWriter, r *http.Request) {

// Log method provides logging methods at WebSocket engine.
func (e *Engine) Log() log.Loggerer {
return e.logger
return e.app.Log()
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand Down
17 changes: 13 additions & 4 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,27 @@ func TestEngineWSClient(t *testing.T) {
}
}

type app struct {
cfg *config.Config
r *router.Router
l log.Loggerer
}

func (a *app) Config() *config.Config { return a.cfg }
func (a *app) Router() *router.Router { return a.r }
func (a *app) Log() log.Loggerer { return a.l }

func newEngine(t *testing.T, cfg *config.Config) *Engine {
l, err := log.New(cfg)
assert.Nil(t, err)

r := router.New(filepath.Join(testdataBaseDir(), "routes.conf"), config.NewEmptyConfig())
r := router.New(filepath.Join(testdataBaseDir(), "routes.conf"), config.NewEmpty())
err = r.Load()
assert.Nil(t, err)

wse, err := New(cfg, l, r)
wse, err := New(&app{cfg: cfg, r: r, l: l})
assert.Nil(t, err)
assert.NotNil(t, wse.logger)
assert.NotNil(t, wse.cfg)
assert.NotNil(t, wse.app)

// Adding events
addWebSocketEvents(t, wse)
Expand Down
4 changes: 2 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

package ws

// Version no. of WebSocket library by aah framework
const Version = "0.2.3"
// Version no. of WebSocket library by aah framework.
const Version = "0.3.0"
25 changes: 13 additions & 12 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

// Package ws is a WebSocket library for aah framework (RFC 6455 compliant).
//
// aah ws internally uses tiny, efficient WebSocket library
// http://github.com/gobwas/ws developed by Sergey Kamardin
// aah ws internally it uses tiny, efficient WebSocket library
// (http://github.com/gobwas/ws) developed by Sergey Kamardin
// (https://github.com/gobwas).
package ws

import (
"fmt"
"net/url"

"aahframework.org/ainsp.v0"
"aahframework.org/config.v0"
"aahframework.org/log.v0"
"aahframework.org/router.v0"
)

// New method creates aah WebSocket engine :)
func New(cfg *config.Config, logger log.Loggerer, router *router.Router) (*Engine, error) {
// New method creates aah WebSocket engine with given aah application instance :)
func New(app interface{}) (*Engine, error) {
a, ok := app.(application)
if !ok {
return nil, fmt.Errorf("ws: not a valid aah application instance")
}

eng := &Engine{
cfg: cfg,
logger: logger,
router: router,
app: a,
registry: &ainsp.TargetRegistry{
Registry: make(map[string]*ainsp.Target),
SearchType: ctxPtrType,
Expand All @@ -32,11 +33,11 @@ func New(cfg *config.Config, logger log.Loggerer, router *router.Router) (*Engin

keyPrefix := "server.websocket"

eng.checkOrigin = cfg.BoolDefault(keyPrefix+".origin.check", false)
eng.checkOrigin = a.Config().BoolDefault(keyPrefix+".origin.check", false)

// parse whitelist origin urls
eng.originWhitelist = make([]*url.URL, 0)
if originWhitelist, found := cfg.StringList(keyPrefix + ".origin.whitelist"); found {
if originWhitelist, found := a.Config().StringList(keyPrefix + ".origin.whitelist"); found {
for _, o := range originWhitelist {
u, err := url.Parse(o)
if err != nil {
Expand Down

0 comments on commit 1db9266

Please sign in to comment.