Added parameter retrieval
This commit is contained in:
parent
c744c93215
commit
8edd3cd83f
12
Context.go
12
Context.go
@ -14,6 +14,7 @@ const maxParams = 16
|
||||
type Context interface {
|
||||
Bytes([]byte) error
|
||||
Error(status int, messages ...any) error
|
||||
Get(param string) string
|
||||
Reader(io.Reader) error
|
||||
SetStatus(status int)
|
||||
String(string) error
|
||||
@ -60,6 +61,17 @@ func (ctx *context) Error(status int, messages ...any) error {
|
||||
return errors.Join(combined...)
|
||||
}
|
||||
|
||||
// Get retrieves a parameter.
|
||||
func (ctx *context) Get(param string) string {
|
||||
for i := 0; i < ctx.paramCount; i++ {
|
||||
if ctx.paramNames[i] == param {
|
||||
return ctx.paramValues[i]
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Reader sends the contents of the io.Reader without creating an in-memory copy.
|
||||
func (ctx *context) Reader(reader io.Reader) error {
|
||||
_, err := io.Copy(ctx.response, reader)
|
||||
|
@ -19,8 +19,8 @@ func TestRouter(t *testing.T) {
|
||||
return ctx.Bytes([]byte("Hello"))
|
||||
})
|
||||
|
||||
s.Get("/blog/:post", func(ctx server.Context) error {
|
||||
return ctx.Bytes([]byte("Hello"))
|
||||
s.Get("/string", func(ctx server.Context) error {
|
||||
return ctx.String("Hello")
|
||||
})
|
||||
|
||||
s.Get("/error", func(ctx server.Context) error {
|
||||
@ -35,8 +35,14 @@ func TestRouter(t *testing.T) {
|
||||
return ctx.Reader(strings.NewReader("Hello"))
|
||||
})
|
||||
|
||||
s.Get("/string", func(ctx server.Context) error {
|
||||
return ctx.String("Hello")
|
||||
s.Get("/blog/:article", func(ctx server.Context) error {
|
||||
article := ctx.Get("article")
|
||||
return ctx.String(article)
|
||||
})
|
||||
|
||||
s.Get("/missing-parameter", func(ctx server.Context) error {
|
||||
missing := ctx.Get("missing")
|
||||
return ctx.String(missing)
|
||||
})
|
||||
|
||||
s.Post("/", func(ctx server.Context) error {
|
||||
@ -58,12 +64,13 @@ func TestRouter(t *testing.T) {
|
||||
Body string
|
||||
}{
|
||||
{Method: "GET", URL: "/", Status: http.StatusOK, Body: "Hello"},
|
||||
{Method: "GET", URL: "/blog/post", Status: http.StatusOK, Body: "Hello"},
|
||||
{Method: "GET", URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"},
|
||||
{Method: "GET", URL: "/error2", Status: http.StatusUnauthorized, Body: "Not logged in\nMissing auth token"},
|
||||
{Method: "GET", URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
|
||||
{Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"},
|
||||
{Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"},
|
||||
{Method: "GET", URL: "/blog/testing-my-router", Status: http.StatusOK, Body: "testing-my-router"},
|
||||
{Method: "GET", URL: "/missing-parameter", Status: http.StatusOK, Body: ""},
|
||||
{Method: "POST", URL: "/", Status: http.StatusOK, Body: "Post"},
|
||||
{Method: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"},
|
||||
{Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"},
|
||||
|
Loading…
Reference in New Issue
Block a user