Skip to content

Commit

Permalink
replace router mux
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Jan 3, 2020
1 parent e724b00 commit e9ce6d6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 264 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ build/$(config)/test: PONYC_FLAGS += --bin-name=test
build/$(config)/test: .deps build jennet/*.pony
stable env ${PONYC} ${PONYC_FLAGS} jennet

build/$(config)/examples:
mkdir -p build/$(config)/examples
build/$(config):
mkdir -p build/$(config)

build:
mkdir -p build/$(config)
Expand All @@ -38,7 +38,7 @@ build:
test: build/$(config)/test
build/$(config)/test

examples: build/$(config)/examples .deps build jennet/*.pony examples/*/*.pony
examples: build/$(config) .deps build jennet/*.pony examples/*/*.pony
stable env ${PONYC} ${PONYC_FLAGS} examples/basicauth
stable env ${PONYC} ${PONYC_FLAGS} examples/params
stable env ${PONYC} ${PONYC_FLAGS} examples/servedir
Expand Down
48 changes: 2 additions & 46 deletions jennet/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,15 @@ use "collections"
use "encode/base64"
use "http"
use "ponytest"
use radix = "radix"

actor Main is TestList
new create(env: Env) => PonyTest(env, this)
new make() => None

fun tag tests(test: PonyTest) =>
test(_TestMultiplexer)
radix.Main.make().tests(test)
test(_TestBasicAuth)

class iso _TestMultiplexer is UnitTest
fun name(): String => "Multiplexer"

fun apply(h: TestHelper) ? =>
let ts = recover Array[(String, _HandlerGroup)] end
ts.push(("/", _HandlerGroup(_TestHandler("0"))))
ts.push(("/foo", _HandlerGroup(_TestHandler("1"))))
ts.push(("/:foo", _HandlerGroup(_TestHandler("2"))))
ts.push(("/foo/bar/", _HandlerGroup(_TestHandler("3"))))
ts.push(("/baz/bar", _HandlerGroup(_TestHandler("4"))))
ts.push(("/:foo/baz", _HandlerGroup(_TestHandler("5"))))
ts.push(("/foo/bar/*baz", _HandlerGroup(_TestHandler("6"))))
let tests = recover val consume ts end
let routes = recover Array[_Route] end
for (p, hg) in tests.values() do
routes.push(_Route("GET", p, hg))
end
let mux = recover val _Multiplexer(consume routes)? end

(var hg, var ps) = mux("GET", "/")?
h.assert_eq[String]("0", (hg.handler as _TestHandler val).msg)

(hg, ps) = mux("GET", "/foo")?
h.assert_eq[String]("1", (hg.handler as _TestHandler val).msg)

(hg, ps) = mux("GET", "/stuff")? // TODO error in non-debug mode
h.assert_eq[String]("2", (hg.handler as _TestHandler val).msg)
h.assert_eq[String]("stuff", ps("foo")?)

h.assert_error({()(mux) ? => mux("GET", "/foo/bar")? })
(hg, ps) = mux("GET", "/foo/bar/")?
h.assert_eq[String]("3", (hg.handler as _TestHandler val).msg)

(hg, ps) = mux("GET", "/baz/bar")?
h.assert_eq[String]("4", (hg.handler as _TestHandler val).msg)

(hg, ps) = mux("GET", "/stuff/baz")?
h.assert_eq[String]("5", (hg.handler as _TestHandler val).msg)
h.assert_eq[String]("stuff", ps("foo")?)

(hg, ps) = mux("GET", "/foo/bar/stuff/and/things")?
h.assert_eq[String]("6", (hg.handler as _TestHandler val).msg)
h.assert_eq[String]("stuff/and/things", ps("baz")?)

class iso _TestBasicAuth is UnitTest
fun name(): String => "BasicAuth"

Expand Down
8 changes: 3 additions & 5 deletions jennet/context.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ use "collections"
use "http"
use "time"

// TODO Separate map in context for iso values?

class iso Context
"""
Contains the data passed between middleware and the handler.
"""
let _responder: Responder
let _params: Map[String, String]
let _params: Map[String, String] val
let _data: Map[String, Any val]
let _start_time: U64

new iso create(responder': Responder, params': Map[String, String] iso) =>
new iso create(responder': Responder, params': Map[String, String] val) =>
_responder = responder'
_params = consume params'
_params = params'
_data = Map[String, Any val]
_start_time = Time.nanos()

Expand Down
6 changes: 3 additions & 3 deletions jennet/jennet.pony
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class iso Jennet
"""
Serve incomming HTTP requests.
"""
let mux = _Multiplexer(_routes)?
let mux = _Mux(_routes)?
let router_factory = _RouterFactory(consume mux, _responder, _notfound)
_server.set_handler(router_factory)

Expand All @@ -143,12 +143,12 @@ class iso Jennet
_routes.push(route)

class val _RouterFactory
let _mux: _Multiplexer val
let _mux: _Mux
let _responder: Responder
let _not_found: _HandlerGroup

new val create(
mux: _Multiplexer val,
mux: _Mux,
responder: Responder,
not_found: _HandlerGroup) =>
_mux = mux
Expand Down
195 changes: 0 additions & 195 deletions jennet/multiplexer.pony

This file was deleted.

35 changes: 35 additions & 0 deletions jennet/mux.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use "collections"
use "http"
use "radix"

// TODO weight optimization
// TODO path auto-correction

class val _Mux
let _methods: Map[String, Radix[_HandlerGroup]]

new trn create(routes: Array[_Route] val) ? =>
_methods = Map[String, Radix[_HandlerGroup]]
for r in routes.values() do
let method = r.method
let hg = _HandlerGroup(r.hg.handler, r.hg.middlewares)
if not _methods.contains(method) then
_methods(method) = Radix[_HandlerGroup]
end
_methods(method)?(r.path.clone())? = hg
end

// TODO: no unwind
fun val apply(method: String, path: String, params: Map[String, String])
: _HandlerGroup ?
=>
let path' =
if path(0)? != '/' then
let p = recover String(path.size() + 1) end
p.append("/")
p.append(path)
p
else
path
end
_methods(method)?(consume path', params) as _HandlerGroup
3 changes: 3 additions & 0 deletions jennet/radix/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ actor Main is TestList
new create(env: Env) =>
PonyTest(env, this)

new make() =>
None

fun tag tests(test: PonyTest) =>
test(_TestRadix)
test(Property1UnitTest[Array[String]](_TestRadixBasic))
Expand Down
Loading

0 comments on commit e9ce6d6

Please sign in to comment.