Skip to content

Commit

Permalink
feat: remove chi use go 1.22 router
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Mar 6, 2024
1 parent ce6a121 commit 6fd4cd7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
35 changes: 19 additions & 16 deletions router.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package gee

import (
"fmt"
"log/slog"
"net/http"
"path"
)

// Router defines all router handle interface.
type Router interface {
Use(middlewares ...Middleware)
Any(pattern string, h http.Handler)
Get(pattern string, h http.Handler)
Post(pattern string, h http.Handler)
Delete(pattern string, h http.Handler)
Patch(pattern string, h http.Handler)
Put(pattern string, h http.Handler)
Options(pattern string, h http.Handler)
Head(pattern string, h http.Handler)
Any(pattern string, h http.HandlerFunc)
Get(pattern string, h http.HandlerFunc)
Post(pattern string, h http.HandlerFunc)
Delete(pattern string, h http.HandlerFunc)
Patch(pattern string, h http.HandlerFunc)
Put(pattern string, h http.HandlerFunc)
Options(pattern string, h http.HandlerFunc)
Head(pattern string, h http.HandlerFunc)
Group(pattern string) Router
Handle(pattern string, h http.Handler)
ServeHTTP(w http.ResponseWriter, req *http.Request)
Expand All @@ -40,13 +42,14 @@ func NewRouter() *Route {
func (r *Route) Handle(pattern string, h http.Handler) {
r.handler = chain(r.mux, r.middlewares)
r.mux.Handle(pattern, h)
slog.Debug(fmt.Sprintf("[Gee-debug] %-25s --> %s", pattern, nameOfFunction(h)))
}

func (r *Route) Use(h ...Middleware) {
r.middlewares = append(r.middlewares, h...)
}

func (r *Route) Any(pattern string, h http.Handler) {
func (r *Route) Any(pattern string, h http.HandlerFunc) {
r.Get(pattern, h)
r.Post(pattern, h)
r.Delete(pattern, h)
Expand All @@ -56,31 +59,31 @@ func (r *Route) Any(pattern string, h http.Handler) {
r.Head(pattern, h)
}

func (r *Route) Get(pattern string, h http.Handler) {
func (r *Route) Get(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("GET", pattern), h)
}

func (r *Route) Post(pattern string, h http.Handler) {
func (r *Route) Post(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("POST", pattern), h)
}

func (r *Route) Delete(pattern string, h http.Handler) {
func (r *Route) Delete(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("DELETE", pattern), h)
}

func (r *Route) Patch(pattern string, h http.Handler) {
func (r *Route) Patch(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("PATCH", pattern), h)
}

func (r *Route) Put(pattern string, h http.Handler) {
func (r *Route) Put(pattern string, h http.HandlerFunc) {
r.mux.Handle(r.getPattern("PUT", pattern), h)
}

func (r *Route) Options(pattern string, h http.Handler) {
func (r *Route) Options(pattern string, h http.HandlerFunc) {
r.mux.Handle(r.getPattern("OPTIONS", pattern), h)
}

func (r *Route) Head(pattern string, h http.Handler) {
func (r *Route) Head(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("HEAD", pattern), h)
}

Expand Down
13 changes: 6 additions & 7 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ func TestNew2(t *testing.T) {
})
}

fn := func(w http.ResponseWriter, r *http.Request) {
t.Log(2)
w.Write([]byte("123"))
}
r := NewRouter()
r.Use(md1)
r.Get("/", http.HandlerFunc(fn))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
t.Log(2)
w.Write([]byte("123"))
})

r2 := r.Group("/api")

Expand All @@ -79,10 +78,10 @@ func TestNew2(t *testing.T) {
})
})

r2.Get("/test", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
r2.Get("/test", func(writer http.ResponseWriter, request *http.Request) {
t.Log(5)
writer.Write([]byte("test"))
}))
})

s := httptest.NewServer(r)
http.Get(s.URL)
Expand Down
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gee

import (
"reflect"
"runtime"
)

func nameOfFunction(f any) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}

0 comments on commit 6fd4cd7

Please sign in to comment.