mirror of
https://github.com/nkanaev/yarr.git
synced 2025-11-09 19:08:57 +00:00
reorganizing server-related packages
This commit is contained in:
61
src/server/router/context.go
Normal file
61
src/server/router/context.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Req *http.Request
|
||||
Out http.ResponseWriter
|
||||
|
||||
Vars map[string]string
|
||||
|
||||
chain []Handler
|
||||
index int
|
||||
}
|
||||
|
||||
func (c *Context) Next() {
|
||||
c.index++
|
||||
c.chain[c.index](c)
|
||||
}
|
||||
|
||||
func (c *Context) JSON(status int, data interface{}) {
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
c.Out.WriteHeader(status)
|
||||
c.Out.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
c.Out.Write(body)
|
||||
c.Out.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
func (c *Context) HTML(status int, tmpl *template.Template, data interface{}) {
|
||||
c.Out.WriteHeader(status)
|
||||
c.Out.Header().Set("Content-Type", "text/html")
|
||||
tmpl.Execute(c.Out, data)
|
||||
}
|
||||
|
||||
func (c *Context) VarInt64(key string) (int64, error) {
|
||||
if val, ok := c.Vars[key]; ok {
|
||||
return strconv.ParseInt(val, 10, 64)
|
||||
}
|
||||
return 0, fmt.Errorf("no such var: %s", key)
|
||||
}
|
||||
|
||||
func (c *Context) QueryInt64(key string) (int64, error) {
|
||||
query := c.Req.URL.Query()
|
||||
return strconv.ParseInt(query.Get(key), 10, 64)
|
||||
}
|
||||
|
||||
func (c *Context) Redirect(url string) {
|
||||
if url == "" {
|
||||
url = "/"
|
||||
}
|
||||
http.Redirect(c.Out, c.Req, url, http.StatusFound)
|
||||
}
|
||||
24
src/server/router/match.go
Normal file
24
src/server/router/match.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package router
|
||||
|
||||
import "regexp"
|
||||
|
||||
func regexGroups(input string, regex *regexp.Regexp) map[string]string {
|
||||
groups := make(map[string]string)
|
||||
matches := regex.FindStringSubmatchIndex(input)
|
||||
for i, key := range regex.SubexpNames()[1:] {
|
||||
groups[key] = input[matches[i*2+2]:matches[i*2+3]]
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
func routeRegexp(route string) *regexp.Regexp {
|
||||
chunks := regexp.MustCompile(`[\*\:]\w+`)
|
||||
output := chunks.ReplaceAllStringFunc(route, func(m string) string {
|
||||
if m[0:1] == `*` {
|
||||
return "(?P<" + m[1:] + ">.+)"
|
||||
}
|
||||
return "(?P<" + m[1:] + ">[^/]+)"
|
||||
})
|
||||
output = "^" + output + "$"
|
||||
return regexp.MustCompile(output)
|
||||
}
|
||||
76
src/server/router/match_test.go
Normal file
76
src/server/router/match_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRouteRegexpPart(t *testing.T) {
|
||||
in := "/hello/:world"
|
||||
re := routeRegexp(in)
|
||||
|
||||
pos := []string{
|
||||
"/hello/world",
|
||||
"/hello/1234",
|
||||
"/hello/bbc1",
|
||||
}
|
||||
for _, c := range pos {
|
||||
if !re.MatchString(c) {
|
||||
t.Errorf("%v must match %v", in, c)
|
||||
}
|
||||
}
|
||||
|
||||
neg := []string{
|
||||
"/hello",
|
||||
"/hello/world/",
|
||||
"/sub/hello/123",
|
||||
"//hello/123",
|
||||
"/hello/123/hello/",
|
||||
}
|
||||
for _, c := range neg {
|
||||
if re.MatchString(c) {
|
||||
t.Errorf("%q must not match %q", in, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteRegexpStar(t *testing.T) {
|
||||
in := "/hello/*world"
|
||||
re := routeRegexp(in)
|
||||
|
||||
pos := []string{"/hello/world", "/hello/world/test"}
|
||||
for _, c := range pos {
|
||||
if !re.MatchString(c) {
|
||||
t.Errorf("%q must match %q", in, c)
|
||||
}
|
||||
}
|
||||
|
||||
neg := []string{"/hello/", "/hello"}
|
||||
for _, c := range neg {
|
||||
if re.MatchString(c) {
|
||||
t.Errorf("%v must not match %v", in, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegexGroupsPart(t *testing.T) {
|
||||
re := routeRegexp("/foo/:bar/1/:baz")
|
||||
|
||||
expect := map[string]string{"bar": "one", "baz": "two"}
|
||||
actual := regexGroups("/foo/one/1/two", re)
|
||||
|
||||
if !reflect.DeepEqual(expect, actual) {
|
||||
t.Errorf("expected: %q, actual: %q", expect, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegexGroupsStar(t *testing.T) {
|
||||
re := routeRegexp("/foo/*bar")
|
||||
|
||||
expect := map[string]string{"bar": "bar/baz/"}
|
||||
actual := regexGroups("/foo/bar/baz/", re)
|
||||
|
||||
if !reflect.DeepEqual(expect, actual) {
|
||||
t.Errorf("expected: %q, actual: %q", expect, actual)
|
||||
}
|
||||
}
|
||||
73
src/server/router/router.go
Normal file
73
src/server/router/router.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Handler func(*Context)
|
||||
|
||||
type Router struct {
|
||||
middle []Handler
|
||||
routes []Route
|
||||
base string
|
||||
}
|
||||
|
||||
type Route struct {
|
||||
regex *regexp.Regexp
|
||||
chain []Handler
|
||||
}
|
||||
|
||||
func NewRouter(base string) *Router {
|
||||
router := &Router{}
|
||||
router.middle = make([]Handler, 0)
|
||||
router.routes = make([]Route, 0)
|
||||
router.base = base
|
||||
return router
|
||||
}
|
||||
|
||||
func (r *Router) Use(h Handler) {
|
||||
r.middle = append(r.middle, h)
|
||||
}
|
||||
|
||||
func (r *Router) For(path string, handler Handler) {
|
||||
x := Route{}
|
||||
x.regex = routeRegexp(path)
|
||||
x.chain = append(r.middle, handler)
|
||||
|
||||
r.routes = append(r.routes, x)
|
||||
}
|
||||
|
||||
func (r *Router) resolve(path string) *Route {
|
||||
for _, route := range r.routes {
|
||||
if route.regex.MatchString(path) {
|
||||
return &route
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
// autoclose open base url
|
||||
if r.base != "" && r.base == req.URL.Path {
|
||||
http.Redirect(rw, req, r.base+"/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
path := strings.TrimPrefix(req.URL.Path, r.base)
|
||||
|
||||
route := r.resolve(path)
|
||||
if route == nil {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
context := &Context{}
|
||||
context.Req = req
|
||||
context.Out = rw
|
||||
context.Vars = regexGroups(path, route.regex)
|
||||
context.index = -1
|
||||
context.chain = route.chain
|
||||
context.Next()
|
||||
}
|
||||
147
src/server/router/router_test.go
Normal file
147
src/server/router/router_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRouter(t *testing.T) {
|
||||
middlecalled := false
|
||||
router := NewRouter("")
|
||||
router.Use(func(c *Context) {
|
||||
middlecalled = true
|
||||
c.Next()
|
||||
})
|
||||
router.For("/hello/:place", func(c *Context) {
|
||||
c.Out.Write([]byte(c.Vars["place"]))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/hello/world", nil)
|
||||
router.ServeHTTP(recorder, request)
|
||||
body, _ := io.ReadAll(recorder.Result().Body)
|
||||
|
||||
if !middlecalled {
|
||||
t.Error("middleware not called")
|
||||
}
|
||||
if recorder.Result().StatusCode != 200 {
|
||||
t.Error("expected 200")
|
||||
}
|
||||
if string(body) != "world" {
|
||||
t.Errorf("invalid response body, got %#v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterPaths(t *testing.T) {
|
||||
router := NewRouter("")
|
||||
router.For("/path/to/foo", func(c *Context) {
|
||||
c.Out.Write([]byte("foo"))
|
||||
})
|
||||
router.For("/path/to/bar", func(c *Context) {
|
||||
c.Out.Write([]byte("bar"))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/path/to/bar", nil)
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
body, _ := io.ReadAll(recorder.Result().Body)
|
||||
if string(body) != "bar" {
|
||||
t.Error("expected 2nd route to be called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterMiddlewareIntercept(t *testing.T) {
|
||||
router := NewRouter("")
|
||||
router.Use(func(c *Context) {
|
||||
c.Out.WriteHeader(404)
|
||||
})
|
||||
router.For("/hello/:place", func(c *Context) {
|
||||
c.Out.WriteHeader(200)
|
||||
c.Out.Write([]byte(c.Vars["place"]))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/hello/world", nil)
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Result().StatusCode != 404 {
|
||||
t.Error("expected 404")
|
||||
}
|
||||
body, _ := io.ReadAll(recorder.Result().Body)
|
||||
if len(body) != 0 {
|
||||
t.Errorf("expected empty body, got %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterMiddlewareOrder(t *testing.T) {
|
||||
router := NewRouter("")
|
||||
|
||||
router.Use(func(c *Context) {
|
||||
c.Out.Write([]byte("foo"))
|
||||
c.Next()
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
c.Out.Write([]byte("bar"))
|
||||
c.Next()
|
||||
})
|
||||
router.For("/hello/:place", func(c *Context) {
|
||||
c.Out.Write([]byte("!!!"))
|
||||
})
|
||||
|
||||
router.Use(func(c *Context) {
|
||||
c.Out.Write([]byte("baz"))
|
||||
c.Next()
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/hello/world", nil)
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Result().StatusCode != 200 {
|
||||
t.Error("expected 200")
|
||||
}
|
||||
body, _ := io.ReadAll(recorder.Result().Body)
|
||||
if string(body) != "foobar!!!" {
|
||||
t.Errorf("invalid body, got %#v", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterBase(t *testing.T) {
|
||||
router := NewRouter("/foo")
|
||||
router.For("/bar", func(c *Context) {
|
||||
c.Out.Write([]byte("!!!"))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/foo/bar", nil)
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Result().StatusCode != 200 {
|
||||
t.Error("expected 200")
|
||||
}
|
||||
body, _ := io.ReadAll(recorder.Result().Body)
|
||||
if string(body) != "!!!" {
|
||||
t.Errorf("invalid body, got %#v", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterBaseRedirect(t *testing.T) {
|
||||
router := NewRouter("/foo")
|
||||
router.For("/", func(c *Context) {
|
||||
c.Out.Write([]byte("!!!"))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/foo", nil)
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Result().StatusCode != 302 {
|
||||
t.Errorf("expected 302, got %d", recorder.Result().StatusCode)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user