Go - Api.Route()

Creates a new route (path) within an API.

import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
route := api.NewRoute("/hello")
nitric.Run()
}

Parameters

  • Name
    match
    Required
    Required
    Type
    string
    Description

    The path matcher to use for this route. Calling NewRoute on the same API more than once with the same matcher will return the same route object. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers. See create a route with path params

  • Name
    middleware
    Optional
    Optional
    Type
    ...HttpMiddleware
    Description

    The middleware (code) that should be run on all requests to this route. Useful for applying universal middleware such as CORS headers or Auth, across an entire route from a single place.

Examples

Create a route

import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
route := api.NewRoute("/hello")
nitric.Run()
}

Create a route with path params

Route paths can include dynamic parameters. These values will automatically be parsed and provided in the context object for your middleware and handlers as a string.

For example, if you have a customers path and you want to include a customerId param you would define the route like this.

import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
// Define route with customerId param
route := api.NewRoute("/customers/:customerId")
nitric.Run()
}

Create a route with middleware

import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func authMiddleware(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {
// Perform auth validation
return next(ctx)
}
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
route := api.NewRoute("/hello", authMiddleware)
nitric.Run()
}
Last updated on Dec 19, 2024