Skip to content

Commit

Permalink
issue #26 separate path2list and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wmentor committed Jan 7, 2021
1 parent 0e71c83 commit a89393e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
21 changes: 21 additions & 0 deletions logger.go
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)
33 changes: 33 additions & 0 deletions path2list.go
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
}
28 changes: 28 additions & 0 deletions path2list_test.go
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", "*"})
}
43 changes: 0 additions & 43 deletions serv.go → router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"

Expand All @@ -17,24 +16,9 @@ import (
)

type Handler func(c *Context)
type LongQueryHandler func(delta time.Duration, c *Context)
type ErrorHandler func(error)
type AuthCheck func(user string, passwd string) bool

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 fileHandler struct {
Filename string
}
Expand Down Expand Up @@ -203,33 +187,6 @@ func RegisterJsonRPC(url string) {

}

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
}

func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

if handler, has := r.fileHandlers[req.URL.Path]; has {
Expand Down
21 changes: 0 additions & 21 deletions serv_test.go → router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import (
"github.com/wmentor/jrpc"
)

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+test/", []string{"/", "test+test"})
tF("/hello/world", []string{"/", "hello", "world"})
tF("/hello/:login/", []string{"/", "hello", ":login"})
tF("/posts/*", []string{"/", "posts", "*"})
}

func TestServ(t *testing.T) {

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

0 comments on commit a89393e

Please sign in to comment.