Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Nov 22, 2024
1 parent 81d4922 commit 724e38a
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 158 deletions.
6 changes: 2 additions & 4 deletions interceptors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ type InterceptorInfo struct {
Method string
Endpoint Endpoint
Payload any
Result any // Only set for server-side result interceptors and client-side response interceptors
Error error // Only set for server-side result interceptors and client-side response interceptors
}
```

Example Server-side request interceptor:

Design:
```go
var DecodeTenant = ServerRequestInterceptor("DecodeTenant", func() {
var DecodeTenant = Interceptor("DecodeTenant", func() {
Description("Server-side interceptor which extracts the tenant ID from the JWT contained in the request Authorization header.")
Read("auth")
Write("tenantID")
Expand All @@ -55,7 +53,7 @@ Example Client-side response interceptor:

Design:
```go
var Retry = ClientResponseInterceptor("Retry", func() {
var Retry = Interceptor("Retry", func() {
Description("Client-side interceptor which retries the request if it fails in a retryable manner")
})
```
Expand Down
73 changes: 46 additions & 27 deletions interceptors/design/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,45 @@ import (
. "goa.design/goa/v3/dsl"
)

// var EncodeTenant = Interceptor("EncodeTenant", func() {
// Description("Client-side interceptor which writes the tenant ID to the JWT contained in the Authorization header")
// Read("tenantID")
// Write("auth")
// })

// var DecodeTenant = Interceptor("DecodeTenant", func() {
// Description("Server-side interceptor which extracts the tenant ID from the JWT contained in the request Authorization header.")
// Read("auth")
// Write("tenantID")
// })

// var Cache = Interceptor("Cache", func() {
// Description("Server-side interceptor which implements a transparent cache for the loaded records")
// Read("id")
// })

// var SetDeadline = Interceptor("SetDeadline", func() {
// Description("Server-side interceptor which sets the context deadline for the request")
// })

// var Retry = Interceptor("Retry", func() {
// Description("Client-side interceptor which retries the request if it fails in a retryable manner")
// })
var EncodeTenant = Interceptor("EncodeTenant", func() {
Description("Client-side interceptor which writes the tenant ID to the signed JWT contained in the Authorization header")

ReadPayload(func() {
Attribute("tenantID", String, "Tenant ID to encode")
})

WritePayload(func() {
Attribute("auth", String, "Generated JWT auth token")
})
})

var ValidateTenant = Interceptor("ValidateTenant", func() {
Description("Server-side interceptor which extracts the tenant ID from the signed JWT contained in the request Authorization header and validates that it matches the tenant ID in the request payload")

ReadPayload(func() {
Attribute("auth", String, "JWT auth token")
})
})

var Cache = Interceptor("Cache", func() {
Description("Server-side interceptor which implements a transparent cache for the loaded records")

ReadPayload(func() {
Attribute("id", String, "Record ID to cache")
})

WriteResult(func() {
Attribute("cachedAt", String, "Time at which the record was cached in RFC3339 format")
})
})

var SetDeadline = Interceptor("SetDeadline", func() {
Description("Server-side interceptor which sets the context deadline for the request")
})

var Retry = Interceptor("Retry", func() {
Description("Client-side interceptor which retries the request if it fails in a retryable manner")
})

var _ = Service("example", func() {
Description(`The example service demonstrates how to use interceptors. In this example we assume
Expand All @@ -36,8 +51,9 @@ The example uses interceptors to extract the tenant ID from the JWT and add it t
interceptors to implement a transparent cache for the loaded records.`)

Method("get_record", func() {
// ClientInterceptorChain(EncodeTenant, Retry)
// ServerInterceptorChain(SetDeadline, DecodeTenant, Cache)
ClientInterceptor(EncodeTenant, Retry)
ServerInterceptor(SetDeadline, ValidateTenant, Cache)

Payload(GetRecordPayload)

Result(Record)
Expand All @@ -62,5 +78,8 @@ var Record = Type("Record", func() {
Field(1, "tenantID", String, "The ID of the tenant where the record is located extracted from the JWT contained in the Authorization header")
Field(2, "recordID", String, "The ID of the record to fetch extracted from the JWT contained in the Authorization header")
Field(3, "fullName", String, "Record full name")
Required("tenantID", "recordID", "fullName")
Field(4, "cachedAt", String, "Time at which the record was cached in RFC3339 format", func() {
Format(FormatDateTime)
})
Required("tenantID", "recordID", "fullName", "cachedAt")
})
35 changes: 15 additions & 20 deletions interceptors/gen/example/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 34 additions & 44 deletions interceptors/gen/example/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 724e38a

Please sign in to comment.