Go - Api.Route.All()

Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on the route.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  customersRoute, err := nitric.NewApi("public").NewRoute("/customers")
  if err != nil {
    return
  }

  customersRoute.All(func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    ctx.Response.Body = []byte("")
    return next(ctx)
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    middleware
    Required
    Required
    Type
    HttpMiddleware
    Description

    The middleware function to use as the handler for HTTP requests. If you want to compose more than one middleware use handler.ComposeHttpMiddleware.

Method options

  • Name
    WithNoMethodSecurity()
    Optional
    Optional
    Type
    MethodOption
    Description

    Sets a base path for all the routes in the API.

  • Name
    WithMethodSecurity()
    Optional
    Optional
    Type
    MethodOption
    Description

    Overrides a security rule from API defined JWT rules.

    • Name
      name
      Required
      Required
      Type
      string
      Description

      The name of the security rule.

    • Name
      scopes
      Required
      Required
      Type
      []string
      Description

      The scopes of the security rule.

Notes

When using the all() method to register a single function as the handler for all HTTP methods, none of the other methods should be defined on that route.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  customersRoute, err := nitric.NewApi("public").NewRoute("/customers")
  if err != nil {
    return
  }

  customersRoute.All(func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    /* handle all requests */
    return next(ctx)
  })

  customersRoute.Get(func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
     /* this handler won't work */
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Examples

Register a method handler function

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  customersRoute, err := nitric.NewApi("public").NewRoute("/customers")
  if err != nil {
    return
  }

  customersRoute.All(func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    ctx.Response.Body = []byte("")
    return next(ctx)
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Chain services as a single method handler

When multiple services are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func authMiddleware(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
  // Perform auth validation
  return next(ctx)
}

func handleRequest(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
  name := ctx.Request.PathParams()["name"]
  ctx.Response.Body = []byte("Hello " + name)
  return next(ctx)
}

func main() {
  customersRoute, err := nitric.NewApi("private").NewRoute("/customers")
  if err != nil {
    return
  }

  customersRoute.All(handler.ComposeHttpMiddleware(authMiddleware, handleRequest))

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Access the request body

For methods that include a request body, such as POST and PUT, you can access the body from the ctx.Request.Data() object.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  customersRoute, err := nitric.NewApi("public").NewRoute("/customers")
  if err != nil {
    return
  }

  customersRoute.All(func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    data := ctx.Request.Data()

    ctx.Response.Body = data

    return next(ctx)
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}