-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #26 separate path2list and logger
- Loading branch information
Showing
5 changed files
with
82 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package serv | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type LogData struct { | ||
Method string | ||
Addr string | ||
Auth string | ||
RequestURL string | ||
StatusCode int | ||
Seconds float64 | ||
Referer string | ||
UserAgent string | ||
UID string | ||
} | ||
|
||
type Logger func(*LogData) | ||
|
||
type LongQueryHandler func(delta time.Duration, c *Context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package serv | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func path2list(path string) []string { | ||
|
||
if len(path) < 1 || path[0] != '/' { | ||
return nil | ||
} | ||
|
||
list := make([]string, 1, 32) | ||
|
||
list[0] = "/" | ||
|
||
for _, v := range strings.Split(path[1:], "/") { | ||
if v != "" { | ||
if uri, e := url.PathUnescape(v); e == nil { | ||
list = append(list, uri) | ||
if uri == "*" { | ||
return list | ||
} | ||
} else { | ||
return nil | ||
} | ||
|
||
} | ||
} | ||
|
||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package serv | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPath(t *testing.T) { | ||
|
||
tF := func(path string, wait []string) { | ||
res := path2list(path) | ||
if strings.Join(res, "#") != strings.Join(wait, "#") { | ||
t.Fatalf("path2list faild for: %s", path) | ||
} | ||
} | ||
|
||
tF("", nil) | ||
tF("123", nil) | ||
tF("/", []string{"/"}) | ||
tF("//", []string{"/"}) | ||
tF("/test", []string{"/", "test"}) | ||
tF("/test/", []string{"/", "test"}) | ||
tF("/test//", []string{"/", "test"}) | ||
tF("/test+test/", []string{"/", "test+test"}) | ||
tF("/hello/world", []string{"/", "hello", "world"}) | ||
tF("/hello/:login/", []string{"/", "hello", ":login"}) | ||
tF("/posts/*", []string{"/", "posts", "*"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters