Skip to content

Commit

Permalink
Merge pull request #28 from wmentor/i26
Browse files Browse the repository at this point in the history
I26
  • Loading branch information
wmentor authored Jan 9, 2021
2 parents 1d8aa81 + 7b91ce3 commit 4c1b4e3
Show file tree
Hide file tree
Showing 12 changed files with 575 additions and 465 deletions.
28 changes: 15 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
)

type Context struct {
rw http.ResponseWriter
req *http.Request
params Params
qw Query
statusCode int
rw http.ResponseWriter
req *http.Request
params Params
qw Query
statusCode int
errorHandler ErrorHandler
tt *tt.TT
}

func (c *Context) StandardError(code int) {
Expand Down Expand Up @@ -221,34 +223,34 @@ func (c *Context) SetCookie(cookie *http.Cookie) {

func (c *Context) Render(tmpl string, vars map[string]interface{}) {

v := tt.MakeVars()
v := c.tt.MakeVars()

for k, val := range vars {
v.Set(k, val)
}

if res, err := tt.Render(tmpl, v); err == nil {
if res, err := c.tt.Render(tmpl, v); err == nil {
c.Write(res)
} else {
if rt.errorHandler != nil {
rt.errorHandler(err)
if c.errorHandler != nil {
c.errorHandler(err)
}
}
}

func (c *Context) RenderStr(tmpl string, vars map[string]interface{}) {

v := tt.MakeVars()
v := c.tt.MakeVars()

for k, val := range vars {
v.Set(k, val)
}

if res, err := tt.RenderString(tmpl, v); err == nil {
if res, err := c.tt.RenderString(tmpl, v); err == nil {
c.Write(res)
} else {
if rt.errorHandler != nil {
rt.errorHandler(err)
if c.errorHandler != nil {
c.errorHandler(err)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var (
errorCodes map[int][]byte

errorInvalidRequestMethod error = errors.New("invalid request method")

ErrServerAlreadyStarted error = errors.New("routerer already started")
)

func init() {
Expand Down
69 changes: 69 additions & 0 deletions global.go
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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.14
require (
github.com/wmentor/jrpc v1.0.3
github.com/wmentor/latency v1.0.0
github.com/wmentor/tt v1.0.0
github.com/wmentor/tt v1.0.1
github.com/wmentor/uniq v1.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ github.com/wmentor/latency v1.0.0 h1:G+JZVaRG6qW2Lzgv5Y9T1aiFETEqiQEYXuTSWvh2hZ8
github.com/wmentor/latency v1.0.0/go.mod h1:WLQCiWrolPQZPot5HRFE5kMaanoOlOfbJraUyHePPCg=
github.com/wmentor/tt v1.0.0 h1:TaVIwklNHxCJf6ZGdGFBMMed2VCMp33D3tlm3TzSB5k=
github.com/wmentor/tt v1.0.0/go.mod h1:UInfmaL3BqW7CqBKvLHVwinfsM2HuII9iCaiwakkCPU=
github.com/wmentor/tt v1.0.1 h1:1dnRAA9Ru0t+qbcSqKZ3Dv/01PPsJhfvFOzwrhYklkE=
github.com/wmentor/tt v1.0.1/go.mod h1:UInfmaL3BqW7CqBKvLHVwinfsM2HuII9iCaiwakkCPU=
github.com/wmentor/uniq v1.0.0 h1:h4//Rt/C6+SOQn5HhURRvJFeBUPzglip996sG8ln6y8=
github.com/wmentor/uniq v1.0.0/go.mod h1:v7DrXGBZ/eKhNMvgKOOB93NIVaJnIy8ESYYg3+4/Qgg=
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", "*"})
}
Loading

0 comments on commit 4c1b4e3

Please sign in to comment.