From 522b612ef88d67722c710d8756d20e0027f04a3b Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Thu, 18 Dec 2025 22:50:07 +0000 Subject: [PATCH 1/8] feat: start adding router groups --- examples/group/README.md | 3 + examples/group/main.go | 20 ++++++ pkg/amp/group.go | 137 +++++++++++++++++++++++++++++++++++++++ pkg/amp/group_test.go | 1 + pkg/amp/mux.go | 48 ++++++++++++++ 5 files changed, 209 insertions(+) create mode 100644 examples/group/README.md create mode 100644 examples/group/main.go create mode 100644 pkg/amp/group.go create mode 100644 pkg/amp/group_test.go diff --git a/examples/group/README.md b/examples/group/README.md new file mode 100644 index 0000000..3296fe1 --- /dev/null +++ b/examples/group/README.md @@ -0,0 +1,3 @@ +# group + +create router groups using the amp mux. diff --git a/examples/group/main.go b/examples/group/main.go new file mode 100644 index 0000000..ffab38a --- /dev/null +++ b/examples/group/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "log" + + "github.com/joseph-beck/amp/pkg/amp" +) + +func main() { + g := amp.Group("/group") + g.Get("/hello", func(ctx *amp.Ctx) error { + return ctx.Render(200, "hello world!") + }) + + a := amp.New() + + a.Group(g) + + log.Fatalln(a.ListenAndServe()) +} diff --git a/pkg/amp/group.go b/pkg/amp/group.go new file mode 100644 index 0000000..e1a19d5 --- /dev/null +++ b/pkg/amp/group.go @@ -0,0 +1,137 @@ +package amp + +type group struct { + // Prefix used for all routes in this group. + // For example "/api/v1" + prefix string + + // All handlers used in the group. + // This internal struct store the method, path, handler and any middleware for each handler. + handlers []handlerConfig + + // All middlewares that are applied to this group. + // These are applied before the route specific middleware. + middleware []Handler +} + +type handlerConfig struct { + method string + path string + handler Handler + middleware []Handler +} + +// Create a new group with a given prefix and optional middleware. +// Uses a variadic variable here, can give one or many middlewares here. +// All routes in this group will use these middlewares. +func Group(prefix string, middleware ...Handler) group { + if len(middleware) < 1 { + middleware = make([]Handler, 0) + } + + return group{ + prefix: prefix, + handlers: make([]handlerConfig, 0), + middleware: middleware, + } +} + +// Adds middleware to the group. +// Uses a variadic variable here, can give one or many middlewares here. +// All routes in this group will use these middlewares. +func (g *group) Use(middleware ...Handler) { + g.middleware = append(g.middleware, middleware...) +} + +// Get the prefix of the group. +func (g group) Prefix() string { + return g.prefix +} + +// Internal handler function, shortcut for generating a handlerConfig. +// Appends handlerConfig to the group. +func (g *group) handler(method string, path string, handler Handler, middleware ...Handler) { + g.handlers = append(g.handlers, handlerConfig{ + method, + path, + handler, + middleware, + }) +} + +// Generic handler, this can be used for a variety of http methods unlike specified ones, like Get. +// All given middleware will only be applied to this route. +// Will likely have to use a switch case statement within the handler to specify method. +// +// func(ctx *amp.Ctx) error { +// switch ctx.Method() { +// case "GET": +// ... +// default: +// ... +// } +// } +// +// Generally recommended to use a specified method. +func (g *group) Handler(path string, handler Handler, middleware ...Handler) { + g.handler("HANDLER", path, handler, middleware...) +} + +// Create a Get route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +// Get requests should be used to retrieve data. +func (g *group) Get(path string, handler Handler, middleware ...Handler) { + g.handler("GET", path, handler, middleware...) +} + +// Create a Post route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +// Post methods should be used for posting data or changing state. +func (g *group) Post(path string, handler Handler, middleware ...Handler) { + g.handler("POST", path, handler, middleware...) +} + +// Create a Put route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +// Put methods should be used for posting data, changing state or updating state. +func (g *group) Put(path string, handler Handler, middleware ...Handler) { + g.handler("PUT", path, handler, middleware...) +} + +// Create a Patch route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +// Patch methods should be used for changing state or updating data. +func (g *group) Patch(path string, handler Handler, middleware ...Handler) { + g.handler("PATCH", path, handler, middleware...) +} + +// Create a Delete route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +// Delete methods should be used for deleting data or a piece of state. +func (g *group) Delete(path string, handler Handler, middleware ...Handler) { + g.handler("DELETE", path, handler, middleware...) +} + +// Create a Head route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +func (g *group) Head(path string, handler Handler, middleware ...Handler) { + g.handler("HEAD", path, handler, middleware...) +} + +// Create an Options route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +func (g *group) Options(path string, handler Handler, middleware ...Handler) { + g.handler("OPTIONS", path, handler, middleware...) +} + +// Create a Connect route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +func (g *group) Connect(path string, handler Handler, middleware ...Handler) { + g.handler("CONNECT", path, handler, middleware...) +} + +// Create a Trace route with a given path, handler and optional middleware. +// All given middleware will only be applied to this route. +func (g *group) Trace(path string, handler Handler, middleware ...Handler) { + g.handler("TRACE", path, handler, middleware...) +} diff --git a/pkg/amp/group_test.go b/pkg/amp/group_test.go new file mode 100644 index 0000000..8cc38d7 --- /dev/null +++ b/pkg/amp/group_test.go @@ -0,0 +1 @@ +package amp diff --git a/pkg/amp/mux.go b/pkg/amp/mux.go index 65a8e8b..38d8dee 100644 --- a/pkg/amp/mux.go +++ b/pkg/amp/mux.go @@ -296,6 +296,53 @@ func (m *Mux) Trace(path string, handler Handler, middleware ...Handler) { m.mux.HandleFunc(fmt.Sprintf("TRACE %s", path), m.Make(handler, middleware...)) } +// Create a group of routes with a given group. +// All routes within the group will have the prefix of the group added to their path. +// All middleware within the group will be applied to all routes within the group. +// Middleware given to individual routes will also be applied to those routes. +// +// g := amp.Group("/group") +// g.Get("/hello", func(ctx *amp.Ctx) error { +// return ctx.Render(200, "hello world!") +// }) +// +// a := amp.New() +// +// a.Group(g) +// +// Can now use the route /group/hello +func (m *Mux) Group(group group) { + slog.Info("GROUP " + group.prefix) + for _, handler := range group.handlers { + middleware := append([]Handler{}, group.middleware...) + middleware = append(middleware, handler.middleware...) + path := group.prefix + handler.path + + switch handler.method { + case "HANDLER": + m.Handler(path, handler.handler, middleware...) + case "GET": + m.Get(path, handler.handler, middleware...) + case "POST": + m.Post(path, handler.handler, middleware...) + case "PUT": + m.Put(path, handler.handler, middleware...) + case "PATCH": + m.Patch(path, handler.handler, middleware...) + case "DELETE": + m.Delete(path, handler.handler, middleware...) + case "HEAD": + m.Head(path, handler.handler, middleware...) + case "OPTIONS": + m.Options(path, handler.handler, middleware...) + case "CONNECT": + m.Connect(path, handler.handler, middleware...) + case "TRACE": + m.Trace(path, handler.handler, middleware...) + } + } +} + // Serve HTTP for the given writer and request. // Serves the current instance of routes in Mux. // @@ -341,6 +388,7 @@ func (m *Mux) ListenAndServe() error { } fmt.Print(amp + "\n") + slog.Info(fmt.Sprintf("amp is running on %s:%d", m.host, m.port)) return http.ListenAndServe(fmt.Sprintf("%s:%d", m.host, m.port), m.mux) } From cac2b2a5ce18b6bf4a7c95e2c2c6e7172456dbfe Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Thu, 18 Dec 2025 22:53:48 +0000 Subject: [PATCH 2/8] docs: update docs for group --- pkg/amp/group.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/amp/group.go b/pkg/amp/group.go index e1a19d5..9f250d1 100644 --- a/pkg/amp/group.go +++ b/pkg/amp/group.go @@ -1,5 +1,6 @@ package amp +// Group struct, used to group routes with a common prefix and middleware. type group struct { // Prefix used for all routes in this group. // For example "/api/v1" @@ -14,16 +15,38 @@ type group struct { middleware []Handler } +// Internal struct to store handler configuration within a group. type handlerConfig struct { - method string - path string - handler Handler + // HTTP method of the handler + // e.g. GET, POST, PUT, DELETE, etc. + method string + + // Path of the handler. + // This is without the group prefix. + path string + + // The handler function itself. + handler Handler + + // Any middleware specific to this route. + // These are applied after the group middleware. middleware []Handler } // Create a new group with a given prefix and optional middleware. // Uses a variadic variable here, can give one or many middlewares here. // All routes in this group will use these middlewares. +// +// g := amp.Group("/group") +// g.Get("/hello", func(ctx *amp.Ctx) error { +// return ctx.Render(200, "hello world!") +// }) +// +// a := amp.New() +// +// a.Group(g) +// +// Can now use the route /group/hello func Group(prefix string, middleware ...Handler) group { if len(middleware) < 1 { middleware = make([]Handler, 0) From 8a009a5dc16ba87271e4e0ac505db4c7a0a7778c Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Thu, 18 Dec 2025 22:55:07 +0000 Subject: [PATCH 3/8] feat: update go version to 1.25 --- .github/workflows/go.yml | 29 ++++++++++++++--------------- go.mod | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 634bc52..30405bf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,29 +4,28 @@ on: push: jobs: - build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: '1.24' + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.25" - - name: Build - run: make build v=1 + - name: Build + run: make build v=1 test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: '1.24' + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.25" - - name: Test - run: make test + - name: Test + run: make test diff --git a/go.mod b/go.mod index 1491e84..9de3d9d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/joseph-beck/amp -go 1.24 +go 1.25 require github.com/stretchr/testify v1.9.0 From 6b6640cae4d03c35fa9a27bb2958a6c3dcce50a4 Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Fri, 19 Dec 2025 18:12:35 +0000 Subject: [PATCH 4/8] test: router groups --- pkg/amp/group.go | 22 +++---- pkg/amp/group_test.go | 144 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 11 deletions(-) diff --git a/pkg/amp/group.go b/pkg/amp/group.go index 9f250d1..c1596d4 100644 --- a/pkg/amp/group.go +++ b/pkg/amp/group.go @@ -59,6 +59,17 @@ func Group(prefix string, middleware ...Handler) group { } } +// Internal handler function, shortcut for generating a handlerConfig. +// Appends handlerConfig to the group. +func (g *group) handler(method string, path string, handler Handler, middleware ...Handler) { + g.handlers = append(g.handlers, handlerConfig{ + method, + path, + handler, + middleware, + }) +} + // Adds middleware to the group. // Uses a variadic variable here, can give one or many middlewares here. // All routes in this group will use these middlewares. @@ -71,17 +82,6 @@ func (g group) Prefix() string { return g.prefix } -// Internal handler function, shortcut for generating a handlerConfig. -// Appends handlerConfig to the group. -func (g *group) handler(method string, path string, handler Handler, middleware ...Handler) { - g.handlers = append(g.handlers, handlerConfig{ - method, - path, - handler, - middleware, - }) -} - // Generic handler, this can be used for a variety of http methods unlike specified ones, like Get. // All given middleware will only be applied to this route. // Will likely have to use a switch case statement within the handler to specify method. diff --git a/pkg/amp/group_test.go b/pkg/amp/group_test.go index 8cc38d7..d26d073 100644 --- a/pkg/amp/group_test.go +++ b/pkg/amp/group_test.go @@ -1 +1,145 @@ package amp + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGroup(t *testing.T) { + { + g := Group("/test") + + assert.Equal(t, "/test", g.prefix) + assert.Equal(t, 0, len(g.handlers)) + assert.Equal(t, 0, len(g.middleware)) + } + + { + g := Group("/test", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, "/test", g.prefix) + assert.Equal(t, 0, len(g.handlers)) + assert.Equal(t, 1, len(g.middleware)) + } +} + +func TestGroupUse(t *testing.T) { + g := Group("/test") + + g.Use(func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.middleware)) +} + +func TestGroupPrefix(t *testing.T) { + g := Group("/test") + + assert.Equal(t, "/test", g.Prefix()) +} + +func TestGroupHandler(t *testing.T) { + g := Group("/test") + + g.Handler("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "HANDLER", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) +} + +func TestGroupGet(t *testing.T) { + g := Group("/test") + + g.Get("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "GET", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) +} + +func TestGroupPost(t *testing.T) { + g := Group("/test") + + g.Post("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "POST", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) +} + +func TestGroupPut(t *testing.T) { + g := Group("/test") + + g.Put("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "PUT", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) +} + +func TestGroupPatch(t *testing.T) { + g := Group("/test") + + g.Patch("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "PATCH", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} + +func TestGroupDelete(t *testing.T) { + g := Group("/test") + + g.Delete("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "DELETE", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} + +func TestGroupHead(t *testing.T) { + g := Group("/test") + + g.Head("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "HEAD", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} + +func TestGroupOptions(t *testing.T) { + g := Group("/test") + + g.Options("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "OPTIONS", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} + +func TestGroupConnect(t *testing.T) { + g := Group("/test") + + g.Connect("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "CONNECT", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} + +func TestGroupTrace(t *testing.T) { + g := Group("/test") + + g.Trace("/hello", func(ctx *Ctx) error { return nil }) + + assert.Equal(t, 1, len(g.handlers)) + assert.Equal(t, "TRACE", g.handlers[0].method) + assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, "/hello", len(g.handlers[0].middleware)) +} From 86e46c55a600b6658626108bb02ea00fd87a1f6f Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Fri, 19 Dec 2025 18:13:15 +0000 Subject: [PATCH 5/8] docs: update group docs --- pkg/amp/group.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/amp/group.go b/pkg/amp/group.go index c1596d4..6d0c5f2 100644 --- a/pkg/amp/group.go +++ b/pkg/amp/group.go @@ -1,3 +1,8 @@ +// Github Repository: https://github.com/joseph-beck/amp +// GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp + +// Package Amp is a web framework made using the Go 1.22 Mux. +// Please ensure you are using Go 1.22, minimum, when using Amp. package amp // Group struct, used to group routes with a common prefix and middleware. From 01358d629f8cda52099056946902a3b280eba025 Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Fri, 19 Dec 2025 18:15:06 +0000 Subject: [PATCH 6/8] fix: router test for middleware --- pkg/amp/group_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/amp/group_test.go b/pkg/amp/group_test.go index d26d073..5158e78 100644 --- a/pkg/amp/group_test.go +++ b/pkg/amp/group_test.go @@ -46,6 +46,7 @@ func TestGroupHandler(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "HANDLER", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupGet(t *testing.T) { @@ -56,6 +57,7 @@ func TestGroupGet(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "GET", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupPost(t *testing.T) { @@ -66,6 +68,7 @@ func TestGroupPost(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "POST", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupPut(t *testing.T) { @@ -76,6 +79,7 @@ func TestGroupPut(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "PUT", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupPatch(t *testing.T) { @@ -86,7 +90,7 @@ func TestGroupPatch(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "PATCH", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupDelete(t *testing.T) { @@ -97,7 +101,7 @@ func TestGroupDelete(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "DELETE", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupHead(t *testing.T) { @@ -108,7 +112,7 @@ func TestGroupHead(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "HEAD", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupOptions(t *testing.T) { @@ -119,7 +123,7 @@ func TestGroupOptions(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "OPTIONS", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupConnect(t *testing.T) { @@ -130,7 +134,7 @@ func TestGroupConnect(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "CONNECT", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } func TestGroupTrace(t *testing.T) { @@ -141,5 +145,5 @@ func TestGroupTrace(t *testing.T) { assert.Equal(t, 1, len(g.handlers)) assert.Equal(t, "TRACE", g.handlers[0].method) assert.Equal(t, "/hello", g.handlers[0].path) - assert.Equal(t, "/hello", len(g.handlers[0].middleware)) + assert.Equal(t, 0, len(g.handlers[0].middleware)) } From 571481f5db9b93c151733ea0ae55e46fc5d59fe6 Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Fri, 19 Dec 2025 18:27:22 +0000 Subject: [PATCH 7/8] fix: github typo --- pkg/amp/ctx.go | 2 +- pkg/amp/group.go | 2 +- pkg/amp/handler.go | 2 +- pkg/amp/mux.go | 2 +- pkg/amp/util.go | 2 +- pkg/binding/binding.go | 2 +- pkg/binding/json.go | 2 +- pkg/binding/toml.go | 2 +- pkg/binding/xml.go | 2 +- pkg/binding/yaml.go | 2 +- pkg/error/err.go | 2 +- pkg/middleware/auth/auth.go | 2 +- pkg/middleware/auth/cfg.go | 2 +- pkg/middleware/cors/cfg.go | 2 +- pkg/middleware/cors/cors.go | 2 +- pkg/middleware/limiter/cfg.go | 2 +- pkg/middleware/limiter/limiter.go | 2 +- pkg/middleware/limiter/store.go | 2 +- pkg/status/status.go | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pkg/amp/ctx.go b/pkg/amp/ctx.go index be59de6..22c4b85 100644 --- a/pkg/amp/ctx.go +++ b/pkg/amp/ctx.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Amp is a web framework made using the Go 1.22 Mux. diff --git a/pkg/amp/group.go b/pkg/amp/group.go index 6d0c5f2..263db13 100644 --- a/pkg/amp/group.go +++ b/pkg/amp/group.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Amp is a web framework made using the Go 1.22 Mux. diff --git a/pkg/amp/handler.go b/pkg/amp/handler.go index 459f2ff..ec04985 100644 --- a/pkg/amp/handler.go +++ b/pkg/amp/handler.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Amp is a web framework made using the Go 1.22 Mux. diff --git a/pkg/amp/mux.go b/pkg/amp/mux.go index 38d8dee..e5ba7f1 100644 --- a/pkg/amp/mux.go +++ b/pkg/amp/mux.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Amp is a web framework made using the Go 1.22 Mux. diff --git a/pkg/amp/util.go b/pkg/amp/util.go index 106d462..9c0d6ff 100644 --- a/pkg/amp/util.go +++ b/pkg/amp/util.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Amp is a web framework made using the Go 1.22 Mux. diff --git a/pkg/binding/binding.go b/pkg/binding/binding.go index 6341d33..ce30730 100644 --- a/pkg/binding/binding.go +++ b/pkg/binding/binding.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Binding is used for binding data in modelling languages. diff --git a/pkg/binding/json.go b/pkg/binding/json.go index d902d06..1c4b0f0 100644 --- a/pkg/binding/json.go +++ b/pkg/binding/json.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Binding is used for binding data in modelling languages. diff --git a/pkg/binding/toml.go b/pkg/binding/toml.go index b4edc0d..6f26cae 100644 --- a/pkg/binding/toml.go +++ b/pkg/binding/toml.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Binding is used for binding data in modelling languages. diff --git a/pkg/binding/xml.go b/pkg/binding/xml.go index 028fddb..59eba22 100644 --- a/pkg/binding/xml.go +++ b/pkg/binding/xml.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Binding is used for binding data in modelling languages. diff --git a/pkg/binding/yaml.go b/pkg/binding/yaml.go index a083c02..38e9388 100644 --- a/pkg/binding/yaml.go +++ b/pkg/binding/yaml.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Binding is used for binding data in modelling languages. diff --git a/pkg/error/err.go b/pkg/error/err.go index 35fccb2..f8604a7 100644 --- a/pkg/error/err.go +++ b/pkg/error/err.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Error is used for defining structured errors. diff --git a/pkg/middleware/auth/auth.go b/pkg/middleware/auth/auth.go index 5b9786d..e379724 100644 --- a/pkg/middleware/auth/auth.go +++ b/pkg/middleware/auth/auth.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Auth is a middleware used for authorizing requests. diff --git a/pkg/middleware/auth/cfg.go b/pkg/middleware/auth/cfg.go index 70267c7..2df2c42 100644 --- a/pkg/middleware/auth/cfg.go +++ b/pkg/middleware/auth/cfg.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Auth is a middleware used for authorizing requests. diff --git a/pkg/middleware/cors/cfg.go b/pkg/middleware/cors/cfg.go index de46d17..384c82a 100644 --- a/pkg/middleware/cors/cfg.go +++ b/pkg/middleware/cors/cfg.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Cors is a middleware used for cross-origin requests. diff --git a/pkg/middleware/cors/cors.go b/pkg/middleware/cors/cors.go index a2ec515..697698d 100644 --- a/pkg/middleware/cors/cors.go +++ b/pkg/middleware/cors/cors.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Cors is a middleware used for cross-origin requests. diff --git a/pkg/middleware/limiter/cfg.go b/pkg/middleware/limiter/cfg.go index 3f88fea..ad74211 100644 --- a/pkg/middleware/limiter/cfg.go +++ b/pkg/middleware/limiter/cfg.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Limiter is a middleware used for rate limiting requests. diff --git a/pkg/middleware/limiter/limiter.go b/pkg/middleware/limiter/limiter.go index 5077f4f..3226d92 100644 --- a/pkg/middleware/limiter/limiter.go +++ b/pkg/middleware/limiter/limiter.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Limiter is a middleware used for rate limiting requests. diff --git a/pkg/middleware/limiter/store.go b/pkg/middleware/limiter/store.go index 2cebb4b..da79814 100644 --- a/pkg/middleware/limiter/store.go +++ b/pkg/middleware/limiter/store.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Limiter is a middleware used for rate limiting requests. diff --git a/pkg/status/status.go b/pkg/status/status.go index 242b470..0925f43 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -1,4 +1,4 @@ -// Github Repository: https://github.com/joseph-beck/amp +// GitHub Repository: https://github.com/joseph-beck/amp // GoDocs: https://pkg.go.dev/github.com/joseph-beck/amp // Package Status is used to define HTTP Status codes used within Amp. From 82b08292de0b1f439dd16161f5bbea7caebd13cf Mon Sep 17 00:00:00 2001 From: Joseph Beck Date: Fri, 19 Dec 2025 18:30:12 +0000 Subject: [PATCH 8/8] chore: tidy up --- pkg/amp/group.go | 6 +----- pkg/amp/mux.go | 7 ++++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/amp/group.go b/pkg/amp/group.go index 263db13..633fac2 100644 --- a/pkg/amp/group.go +++ b/pkg/amp/group.go @@ -12,7 +12,7 @@ type group struct { prefix string // All handlers used in the group. - // This internal struct store the method, path, handler and any middleware for each handler. + // This internal struct stores the method, path, handler and any middleware for each handler. handlers []handlerConfig // All middlewares that are applied to this group. @@ -53,10 +53,6 @@ type handlerConfig struct { // // Can now use the route /group/hello func Group(prefix string, middleware ...Handler) group { - if len(middleware) < 1 { - middleware = make([]Handler, 0) - } - return group{ prefix: prefix, handlers: make([]handlerConfig, 0), diff --git a/pkg/amp/mux.go b/pkg/amp/mux.go index e5ba7f1..4403fbb 100644 --- a/pkg/amp/mux.go +++ b/pkg/amp/mux.go @@ -296,7 +296,7 @@ func (m *Mux) Trace(path string, handler Handler, middleware ...Handler) { m.mux.HandleFunc(fmt.Sprintf("TRACE %s", path), m.Make(handler, middleware...)) } -// Create a group of routes with a given group. +// Create a group of routes with a given prefix. // All routes within the group will have the prefix of the group added to their path. // All middleware within the group will be applied to all routes within the group. // Middleware given to individual routes will also be applied to those routes. @@ -339,6 +339,11 @@ func (m *Mux) Group(group group) { m.Connect(path, handler.handler, middleware...) case "TRACE": m.Trace(path, handler.handler, middleware...) + default: + slog.Error("method not recognised, failed to add route", + "method", handler.method, + "path", path, + ) } } }