-
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.
Merge pull request #28 from wmentor/i26
I26
- Loading branch information
Showing
12 changed files
with
575 additions
and
465 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package serv | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
var ( | ||
server *Server | ||
) | ||
|
||
func init() { | ||
server = New() | ||
} | ||
|
||
func Start(addr string) error { | ||
return server.Start(addr) | ||
} | ||
|
||
func Shutdown() { | ||
server.Shutdown() | ||
} | ||
|
||
func SetLongQueryHandler(delta time.Duration, fn LongQueryHandler) { | ||
server.SetLongQueryHandler(delta, fn) | ||
} | ||
|
||
func SetErrorHandler(fn ErrorHandler) { | ||
server.SetErrorHandler(fn) | ||
} | ||
|
||
func SetAuthCheck(fn AuthCheck) { | ||
server.SetAuthCheck(fn) | ||
} | ||
|
||
func SetUID(enable bool) { | ||
server.SetUID(enable) | ||
} | ||
|
||
func SetLogger(l Logger) { | ||
server.SetLogger(l) | ||
} | ||
|
||
func Static(prefix string, dir string) { | ||
server.Static(prefix, dir) | ||
} | ||
|
||
func File(path string, filename string) { | ||
server.File(path, filename) | ||
} | ||
|
||
func Register(method string, path string, fn Handler) { | ||
server.Register(method, path, fn) | ||
} | ||
|
||
func RegisterAuth(method string, path string, fn Handler) { | ||
server.RegisterAuth(method, path, fn) | ||
} | ||
|
||
func RegMethod(method string, fn interface{}) { | ||
server.RegMethod(method, fn) | ||
} | ||
|
||
func RegisterJsonRPC(url string) { | ||
server.RegisterJsonRPC(url) | ||
} | ||
|
||
func LoadTemplates(dir string) { | ||
server.LoadTemplates(dir) | ||
} |
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
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", "*"}) | ||
} |
Oops, something went wrong.