Skip to content

[WIP] Extendability #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
router: add pat and possum
The Possum implementation was previously broken. Possum’s router.Simple
is not sufficient for dynamic path segment routing. Further makes
Possum’s current implementation of router.RegEx it impossible to
benchmark ParameterReadWrite.

ParameterReadWrite should be re-enabled once Possum implements named
RegEx groups.
  • Loading branch information
coss committed Jun 12, 2015
commit 39931e5b82f063c9b742f2b59e4221696ee61892
69 changes: 69 additions & 0 deletions router/pat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2014-2015 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

// +build !martini

package router

import (
"io"
"net/http"

"github.com/bmizerany/pat"
"github.com/julienschmidt/go-http-routing-benchmark/driver"
)

const patRWP = ":" + driver.ParamNameReadWrite

func patHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}

func patHandlerReadWrite(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, r.URL.Query().Get(patRWP))
}

func patFactory(t driver.Type) http.Handler {
switch t {
case driver.Static:
return http.HandlerFunc(patHandler)
case driver.Parameterized:
return http.HandlerFunc(patHandler)
case driver.ParameterReadWrite:
return http.HandlerFunc(patHandlerReadWrite)
default:
panic("Unknown benchmark type passed")
}
}

func patRouter(t driver.Type, f driver.Fixtures) http.Handler {
m := pat.New()
h := patFactory(t)

for _, r := range f {
switch r.Method {
case "GET":
m.Get(r.Path, h)
case "POST":
m.Post(r.Path, h)
case "PUT":
m.Put(r.Path, h)
case "DELETE":
m.Del(r.Path, h)
default:
panic("Unknow HTTP method: " + r.Method)
}
}

return m
}

func init() {
driver.RegisterPackage(&driver.Package{
Name: "Pat",
Router: patRouter,
Homepage: "http://github.com/bmizerany/pat",
Supports: driver.Static | driver.Parameterized | driver.ParameterReadWrite,
})
}
68 changes: 68 additions & 0 deletions router/possum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2014-2015 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

// +build !possum

package router

import (
"io"
"net/http"
"strings"

"github.com/julienschmidt/go-http-routing-benchmark/driver"
"github.com/mikespook/possum"
"github.com/mikespook/possum/router"
"github.com/mikespook/possum/view"
)

func possumHandler(c *possum.Context) error {
c.Response.WriteHeader(http.StatusOK)
return nil
}

func possumHandlerReadWrite(c *possum.Context) error {
io.WriteString(c.Response, c.Request.URL.Query().Get(driver.ParamNameReadWrite))
return nil
}

func possumFactory(t driver.Type) func(*possum.Context) error {
switch t {
case driver.Static:
return possumHandler
case driver.Parameterized:
return possumHandler
case driver.ParameterReadWrite:
return possumHandlerReadWrite
default:
panic("Unknown benchmark type passed")
}
}

func possumRouter(t driver.Type, f driver.Fixtures) http.Handler {
h := possumFactory(t)
m := possum.NewServerMux()

for _, r := range f {
var routable router.Router = router.Simple(r.Path)

if strings.Contains(r.Path, "[^/]*)") {
routable = router.RegEx(r.Path)
}

m.HandleFunc(routable, h, view.Simple("text/html", "utf-8"))
}

return m
}

func init() {
driver.RegisterPackage(&driver.Package{
Name: "Possum",
Router: possumRouter,
Normalizer: driver.RegExAllNormalizer,
Homepage: "http://github.com/mikespook/possum",
Supports: driver.Static | driver.Parameterized, /* | driver.ParameterReadWrite */
})
}