Skip to content

Commit 5fe42fa

Browse files
committed
refactor: fork package and implement pathrouter
Signed-off-by: Christian Stewart <christian@paral.in>
1 parent 3425025 commit 5fe42fa

File tree

11 files changed

+397
-1172
lines changed

11 files changed

+397
-1172
lines changed

LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
BSD 3-Clause License
22

3+
Copyright 2023 Christian Stewart <christian@aperture.us>
4+
35
Copyright (c) 2013, Julien Schmidt
46
All rights reserved.
57

README.md

Lines changed: 65 additions & 190 deletions
Large diffs are not rendered by default.

example/example.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/aperturerobotics/pathrouter"
8+
)
9+
10+
// Responder is a custom struct used in place of a response writer.
11+
type Responder struct {
12+
}
13+
14+
func (r *Responder) Respond(ctx context.Context, path, resp string) error {
15+
fmt.Printf("path=%s responded with: %s", path, resp)
16+
return nil
17+
}
18+
19+
func Index(ctx context.Context, reqPath string, p pathrouter.Params, rw *Responder) (bool, error) {
20+
return true, rw.Respond(ctx, reqPath, "Welcome!\n")
21+
}
22+
23+
func Hello(ctx context.Context, reqPath string, p pathrouter.Params, rw *Responder) (bool, error) {
24+
return true, rw.Respond(ctx, reqPath, fmt.Sprintf("hello, %s!\n", p.ByName("name")))
25+
}
26+
27+
func main() {
28+
router := pathrouter.New[*Responder]()
29+
router.AddHandler("/", Index)
30+
router.AddHandler("/hello/:name", Hello)
31+
32+
ctx := context.Background()
33+
resp := &Responder{}
34+
router.Serve(ctx, "/", resp)
35+
router.Serve(ctx, "/hello/world", resp)
36+
router.Serve(ctx, "/hello/reader", resp)
37+
}

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
module github.com/julienschmidt/httprouter
1+
module github.com/aperturerobotics/pathrouter
22

3-
go 1.7
3+
go 1.19
4+
5+
require github.com/pkg/errors v0.9.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found
44
// in the LICENSE file.
55

6-
package httprouter
6+
package pathrouter
77

88
// CleanPath is the URL version of path.Clean, it returns a canonical URL path
99
// for p, eliminating . and .. elements.

path_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found
44
// in the LICENSE file.
55

6-
package httprouter
6+
package pathrouter
77

88
import (
99
"strings"

0 commit comments

Comments
 (0)