Skip to content
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

feat: added tree-based router #48

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ func TestGroup_Add(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
}, []string{http.MethodGet, http.MethodPost})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 2, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, []string{http.MethodGet, http.MethodPost}, k.router.routes[0].methods)

testCases := []struct {
req *http.Request
res *httptest.ResponseRecorder
Expand Down
33 changes: 16 additions & 17 deletions kid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type (
//
// It's a framework instance.
Kid struct {
router Router
router Tree
middlewares []MiddlewareFunc
notFoundHandler HandlerFunc
methodNotAllowedHandler HandlerFunc
Expand All @@ -43,7 +43,7 @@ type (
// Version of Kid.
const Version string = "0.1.0"

// allMethods is all of the HTTP methods.
// allMethods is a list of all HTTP methods.
var allMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut,
http.MethodPatch, http.MethodDelete, http.MethodHead,
Expand All @@ -53,7 +53,7 @@ var allMethods = []string{
// New returns a new instance of Kid.
func New() *Kid {
kid := Kid{
router: newRouter(),
router: newTree(),
middlewares: make([]MiddlewareFunc, 0),
notFoundHandler: defaultNotFoundHandler,
methodNotAllowedHandler: defaultMethodNotAllowedHandler,
Expand Down Expand Up @@ -94,70 +94,70 @@ func (k *Kid) Use(middleware MiddlewareFunc) {
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Get(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodGet}, middlewares)
k.router.insertNode(path, []string{http.MethodGet}, middlewares, handler)
}

// Post registers a new handler for the given path for POST method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Post(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodPost}, middlewares)
k.router.insertNode(path, []string{http.MethodPost}, middlewares, handler)
}

// Put registers a new handler for the given path for PUT method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Put(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodPut}, middlewares)
k.router.insertNode(path, []string{http.MethodPut}, middlewares, handler)
}

// Patch registers a new handler for the given path for PATCH method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Patch(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodPatch}, middlewares)
k.router.insertNode(path, []string{http.MethodPatch}, middlewares, handler)
}

// Delete registers a new handler for the given path for DELETE method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Delete(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodDelete}, middlewares)
k.router.insertNode(path, []string{http.MethodDelete}, middlewares, handler)
}

// Head registers a new handler for the given path for HEAD method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Head(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodHead}, middlewares)
k.router.insertNode(path, []string{http.MethodHead}, middlewares, handler)
}

// Options registers a new handler for the given path for OPTIONS method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Options(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodOptions}, middlewares)
k.router.insertNode(path, []string{http.MethodOptions}, middlewares, handler)
}

// Connect registers a new handler for the given path for CONNECT method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Connect(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodConnect}, middlewares)
k.router.insertNode(path, []string{http.MethodConnect}, middlewares, handler)
}

// Trace registers a new handler for the given path for TRACE method.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Trace(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, []string{http.MethodTrace}, middlewares)
k.router.insertNode(path, []string{http.MethodTrace}, middlewares, handler)
}

// Any registers a new handler for the given path for all of the HTTP methods.
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Any(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, allMethods, middlewares)
k.router.insertNode(path, allMethods, middlewares, handler)
}

// Group creates a new router group.
Expand All @@ -172,7 +172,7 @@ func (k *Kid) Group(prefix string, middlewares ...MiddlewareFunc) Group {
//
// Specifying middlewares is optional. Middlewares will only be applied to this route.
func (k *Kid) Add(path string, handler HandlerFunc, methods []string, middlewares ...MiddlewareFunc) {
k.router.add(path, handler, methods, middlewares)
k.router.insertNode(path, methods, middlewares, handler)
}

// Static registers a new route for serving static files.
Expand All @@ -188,18 +188,17 @@ func (k *Kid) Static(urlPath, staticRoot string, middlewares ...MiddlewareFunc)
func (k *Kid) StaticFS(urlPath string, fs http.FileSystem, middlewares ...MiddlewareFunc) {
fileServer := newFileServer(urlPath, fs)

methods := []string{http.MethodGet}
path := appendSlash(urlPath) + "{*filePath}"

k.router.add(path, WrapHandler(fileServer), methods, middlewares)
k.router.insertNode(path, []string{http.MethodGet}, middlewares, WrapHandler(fileServer))
}

// ServeHTTP implements the http.HandlerFunc interface.
func (k *Kid) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := k.pool.Get().(*Context)
c.reset(r, w)

route, params, err := k.router.find(getPath(r.URL), r.Method)
route, params, err := k.router.search(getPath(r.URL), r.Method)

c.setParams(params)

Expand Down
64 changes: 1 addition & 63 deletions kid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNew(t *testing.T) {
k := New()

assert.NotNil(t, k)
assert.Equal(t, newRouter(), k.router)
assert.Equal(t, newTree(), k.router)
assert.Equal(t, 0, len(k.middlewares))
assert.Equal(t, serializer.NewJSONSerializer(), k.jsonSerializer)
assert.Equal(t, serializer.NewXMLSerializer(), k.xmlSerializer)
Expand Down Expand Up @@ -57,11 +57,6 @@ func TestKid_Get(t *testing.T) {
c.JSON(http.StatusOK, Map{"message": fmt.Sprintf("Hello %s", name)})
})

assert.Equal(t, 2, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodGet, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodGet, "/test", nil)
res := httptest.NewRecorder()

Expand Down Expand Up @@ -91,11 +86,6 @@ func TestKid_Post(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "ok"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodPost, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodPost, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -117,11 +107,6 @@ func TestKid_Put(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "put"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodPut, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodPut, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -143,11 +128,6 @@ func TestKid_Delete(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "deleted"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodDelete, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodDelete, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -169,11 +149,6 @@ func TestKid_Patch(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "patch"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodPatch, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodPatch, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -195,11 +170,6 @@ func TestKid_Trace(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "trace"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodTrace, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodTrace, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -221,11 +191,6 @@ func TestKid_Connect(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "connect"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodConnect, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodConnect, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -247,11 +212,6 @@ func TestKid_Options(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "options"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodOptions, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodOptions, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -273,11 +233,6 @@ func TestKid_Head(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": "head"})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 1, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, http.MethodHead, k.router.routes[0].methods[0])

req := httptest.NewRequest(http.MethodHead, "/test", nil)
res := httptest.NewRecorder()

Expand All @@ -299,11 +254,6 @@ func TestKid_Add(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
}, []string{http.MethodGet, http.MethodPost})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 2, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t, []string{http.MethodGet, http.MethodPost}, k.router.routes[0].methods)

testCases := []struct {
req *http.Request
res *httptest.ResponseRecorder
Expand Down Expand Up @@ -335,18 +285,6 @@ func TestKid_Any(t *testing.T) {
c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
})

assert.Equal(t, 1, len(k.router.routes))
assert.Equal(t, 9, len(k.router.routes[0].methods))
assert.Equal(t, 0, len(k.router.routes[0].middlewares))
assert.Equal(t,
[]string{
http.MethodGet, http.MethodPost, http.MethodPut,
http.MethodPatch, http.MethodDelete, http.MethodHead,
http.MethodOptions, http.MethodConnect, http.MethodTrace,
},
k.router.routes[0].methods,
)

testCases := []struct {
req *http.Request
res *httptest.ResponseRecorder
Expand Down
Loading