Go - NewApi()
Creates a new HTTP API.
import (
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
name
- Required
- Required
- Type
- string
- Description
The unique name of this API within the app. Subsequent calls to
NewApi
with the same name will return the same object.
- Name
options
- Optional
- Optional
- Type
- ...ApiOption
- Description
Additional options for the API. See below.
API options
- Name
WithPath()
- Optional
- Optional
- Type
- ApiOption
- Description
Sets a base path for all the routes in the API.
- Name
path
- Required
- Required
- Type
- string
- Description
Base path for all routes in the API.
- Name
WithSecurity()
- Optional
- Optional
- Type
- OidcOptions
- Description
Security rules to apply with scopes to the entire API.
- Name
WithMiddleware()
- Optional
- Optional
- Type
- ApiOption
- Description
- Name
middleware
- Required
- Required
- Type
- HttpMiddleware
- Description
The middleware (code) that should be run on all requests to the API. Useful for applying universal middleware such as CORS headers or Auth, across an entire API from a single place.
OidcOptions Parameters
- Name
Name
- Required
- Required
- Type
- string
- Description
the name of the security definition
- Name
Issuer
- Required
- Required
- Type
- string
- Description
the issuer for the JWT tokens e.g.
https://account.region.auth0.com
- Name
Audiences
- Required
- Required
- Type
- []string
- Description
the
aud
that will be applied to JWT tokens from the issuer.
- Name
Scopes
- Required
- Required
- Type
- []string
- Description
the scopes that will be required to authenticate.
Examples
Create an API
import (
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("public")
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Create an API with universal middleware
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 main() {
api, err := nitric.NewApi("private", nitric.WithMiddleware(authMiddleware))
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Define middleware
func authMiddleware(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
// Perform auth validation
return next(ctx)
}
Notes
Middleware functions are supplied an HttpContext
object and a next()
function which calls the next middleware in the chain.
Create an API with a base path
If you need to put all the routes in your api below a shared base path, you can do that with the WithPath
option. In this example we ensure all routes start with /api/v1/
before the route specific path.
import (
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
v1Api, err := nitric.NewApi("public", nitric.WithPath("/api/v1/"))
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Apply JWT authentication to an API
import (
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
defaultSecurityRule := nitric.OidcRule("default", "https://example-issuer.com", []string{"YOUR-AUDIENCES"})
secureApi, err := nitric.NewApi(
"secure",
// apply the security definition to all routes in this API.
nitric.WithSecurity(defaultSecurityRule([]string{})),
)
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}