Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 071625e

Browse files
jyecuschdavemooreuws
authored andcommitted
update Go middleware docs
1 parent 1292623 commit 071625e

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

src/pages/apis.mdx

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,9 @@ galaxyApi.post("/planets/unsecured-planet", (ctx) async {
572572

573573
## Defining Middleware
574574

575-
Behavior that's common to several APIs or routes can be applied using middleware services. Multiple middleware can also be composed to create a cascading set of steps to perform on incoming requests or outgoing responses.
575+
Behavior that's common to several APIs or routes can be applied using middleware. Multiple middleware can also be composed to create a cascading set of steps to perform on incoming requests or outgoing responses.
576576

577-
Middleware services look nearly identical to handlers except for an additional parameter called `next`, which is the next middleware or handler to be called in the chain. By providing each middleware the next middleware in the chain it allows them to intercept requests, response and errors to perform operations like logging, decoration, exception handling and many other common tasks.
577+
In most of Nitric's supported languages middleware functions look nearly identical to handlers except for an additional parameter called `next`, which is the next middleware or handler to be called in the chain. By providing each middleware the next middleware in the chain it allows them to intercept requests, response and errors to perform operations like logging, decoration, exception handling and many other common tasks.
578578

579579
<CodeGroup>
580580

@@ -599,15 +599,18 @@ async def validate(ctx, nxt: HttpMiddleware):
599599
```
600600

601601
```go
602-
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*apis.Ctx, error) {
603-
if ctx.Request.Headers()["content-type"] != nil {
604-
ctx.Response.Status = 400
605-
ctx.Response.Body = []byte("header Content-Type is required")
602+
// Using the Go SDK we recommend using higher-order functions to define middleware
603+
func validate(next nitric.Handler[apis.Ctx]) nitric.Handler[apis.Ctx] {
604+
return func (ctx *apis.Ctx) error {
605+
if ctx.Request.Headers()["content-type"] != nil {
606+
ctx.Response.Status = 400
607+
ctx.Response.Body = []byte("header Content-Type is required")
606608

607-
return ctx, nil
608-
}
609+
return nil
610+
}
609611

610-
return next(ctx)
612+
return next(ctx)
613+
}
611614
}
612615
```
613616

@@ -650,21 +653,23 @@ import (
650653
"github.com/nitrictech/go-sdk/nitric"
651654
)
652655

653-
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*apis.Ctx, error) {
654-
if ctx.Request.Headers()["content-type"] != nil {
655-
ctx.Response.Status = 400
656-
ctx.Response.Body = []byte("header Content-Type is required")
656+
func validate(next nitric.Handler[apis.Ctx]) nitric.Handler[apis.Ctx] {
657+
return func (ctx *apis.Ctx) error {
658+
if ctx.Request.Headers()["content-type"] != nil {
659+
ctx.Response.Status = 400
660+
ctx.Response.Body = []byte("header Content-Type is required")
657661

658-
return ctx, nil
659-
}
662+
return nil
663+
}
660664

661-
return next(ctx)
665+
return next(ctx)
666+
}
662667
}
663668

664669
func main() {
665670
customersApi, err := nitric.NewApi(
666671
"customers",
667-
nitric.WithMiddleware(validate))
672+
apis.WithMiddleware(validate))
668673

669674
if err != nil {
670675
return
@@ -715,26 +720,27 @@ import (
715720
"github.com/nitrictech/go-sdk/nitric"
716721
)
717722

718-
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*apis.Ctx, error) {
719-
if ctx.Request.Headers()["content-type"] != nil {
720-
ctx.Response.Status = 400
721-
ctx.Response.Body = []byte("header Content-Type is required")
723+
func validate(next nitric.Handler[apis.Ctx]) nitric.Handler[apis.Ctx] {
724+
return func (ctx *apis.Ctx) error {
725+
if ctx.Request.Headers()["content-type"] != nil {
726+
ctx.Response.Status = 400
727+
ctx.Response.Body = []byte("header Content-Type is required")
722728

723-
return ctx, nil
724-
}
729+
return nil
730+
}
725731

726-
return next(ctx)
732+
return next(ctx)
733+
}
727734
}
728735

729736
func main() {
730-
customersApi, err := nitric.NewApi(
731-
"customers")
737+
customersApi, err := nitric.NewApi("customers")
732738

733739
if err != nil {
734740
return
735741
}
736742

737-
customersApi.Get("/customers", nitric.Compose(validate, func(ctx *apis.Ctx) {
743+
customersApi.Get("/customers", validate(func(ctx *apis.Ctx) {
738744
// handle request
739745
}))
740746

0 commit comments

Comments
 (0)