Skip to content

Commit a89393e

Browse files
committed
issue #26 separate path2list and logger
1 parent 0e71c83 commit a89393e

File tree

5 files changed

+82
-64
lines changed

5 files changed

+82
-64
lines changed

logger.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package serv
2+
3+
import (
4+
"time"
5+
)
6+
7+
type LogData struct {
8+
Method string
9+
Addr string
10+
Auth string
11+
RequestURL string
12+
StatusCode int
13+
Seconds float64
14+
Referer string
15+
UserAgent string
16+
UID string
17+
}
18+
19+
type Logger func(*LogData)
20+
21+
type LongQueryHandler func(delta time.Duration, c *Context)

path2list.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package serv
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
)
7+
8+
func path2list(path string) []string {
9+
10+
if len(path) < 1 || path[0] != '/' {
11+
return nil
12+
}
13+
14+
list := make([]string, 1, 32)
15+
16+
list[0] = "/"
17+
18+
for _, v := range strings.Split(path[1:], "/") {
19+
if v != "" {
20+
if uri, e := url.PathUnescape(v); e == nil {
21+
list = append(list, uri)
22+
if uri == "*" {
23+
return list
24+
}
25+
} else {
26+
return nil
27+
}
28+
29+
}
30+
}
31+
32+
return list
33+
}

path2list_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package serv
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestPath(t *testing.T) {
9+
10+
tF := func(path string, wait []string) {
11+
res := path2list(path)
12+
if strings.Join(res, "#") != strings.Join(wait, "#") {
13+
t.Fatalf("path2list faild for: %s", path)
14+
}
15+
}
16+
17+
tF("", nil)
18+
tF("123", nil)
19+
tF("/", []string{"/"})
20+
tF("//", []string{"/"})
21+
tF("/test", []string{"/", "test"})
22+
tF("/test/", []string{"/", "test"})
23+
tF("/test//", []string{"/", "test"})
24+
tF("/test+test/", []string{"/", "test+test"})
25+
tF("/hello/world", []string{"/", "hello", "world"})
26+
tF("/hello/:login/", []string{"/", "hello", ":login"})
27+
tF("/posts/*", []string{"/", "posts", "*"})
28+
}

serv.go renamed to router.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9-
"net/url"
109
"strings"
1110
"time"
1211

@@ -17,24 +16,9 @@ import (
1716
)
1817

1918
type Handler func(c *Context)
20-
type LongQueryHandler func(delta time.Duration, c *Context)
2119
type ErrorHandler func(error)
2220
type AuthCheck func(user string, passwd string) bool
2321

24-
type LogData struct {
25-
Method string
26-
Addr string
27-
Auth string
28-
RequestURL string
29-
StatusCode int
30-
Seconds float64
31-
Referer string
32-
UserAgent string
33-
UID string
34-
}
35-
36-
type Logger func(*LogData)
37-
3822
type fileHandler struct {
3923
Filename string
4024
}
@@ -203,33 +187,6 @@ func RegisterJsonRPC(url string) {
203187

204188
}
205189

206-
func path2list(path string) []string {
207-
208-
if len(path) < 1 || path[0] != '/' {
209-
return nil
210-
}
211-
212-
list := make([]string, 1, 32)
213-
214-
list[0] = "/"
215-
216-
for _, v := range strings.Split(path[1:], "/") {
217-
if v != "" {
218-
if uri, e := url.PathUnescape(v); e == nil {
219-
list = append(list, uri)
220-
if uri == "*" {
221-
return list
222-
}
223-
} else {
224-
return nil
225-
}
226-
227-
}
228-
}
229-
230-
return list
231-
}
232-
233190
func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
234191

235192
if handler, has := r.fileHandlers[req.URL.Path]; has {

serv_test.go renamed to router_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@ import (
1111
"github.com/wmentor/jrpc"
1212
)
1313

14-
func TestPath(t *testing.T) {
15-
16-
tF := func(path string, wait []string) {
17-
res := path2list(path)
18-
if strings.Join(res, "#") != strings.Join(wait, "#") {
19-
t.Fatalf("path2list faild for: %s", path)
20-
}
21-
}
22-
23-
tF("", nil)
24-
tF("123", nil)
25-
tF("/", []string{"/"})
26-
tF("//", []string{"/"})
27-
tF("/test", []string{"/", "test"})
28-
tF("/test/", []string{"/", "test"})
29-
tF("/test+test/", []string{"/", "test+test"})
30-
tF("/hello/world", []string{"/", "hello", "world"})
31-
tF("/hello/:login/", []string{"/", "hello", ":login"})
32-
tF("/posts/*", []string{"/", "posts", "*"})
33-
}
34-
3514
func TestServ(t *testing.T) {
3615

3716
Register("GET", "/", func(c *Context) {

0 commit comments

Comments
 (0)