Skip to content

Commit d87897a

Browse files
author
Richard Connon
committed
Allow configuration of invoke URL pattern via environment variables
1 parent 6c43ac0 commit d87897a

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

api/server/fn_annotator.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ import (
88
"github.com/gin-gonic/gin"
99
)
1010

11-
//FnAnnotator Is used to inject trigger context (such as request URLs) into outbound trigger resources
11+
//FnAnnotator Is used to inject fn context (such as request URLs) into outbound fn resources
1212
type FnAnnotator interface {
1313
// Annotates a trigger on read
1414
AnnotateFn(ctx *gin.Context, a *models.App, fn *models.Fn) (*models.Fn, error)
1515
}
1616

17-
type requestBasedFnAnnotator struct{}
17+
type requestBasedFnAnnotator struct {
18+
group, template string
19+
}
1820

19-
func annotateFnWithBaseURL(baseURL string, app *models.App, fn *models.Fn) (*models.Fn, error) {
21+
func annotateFnWithBaseURL(baseURL, group, template string, app *models.App, fn *models.Fn) (*models.Fn, error) {
2022

2123
baseURL = strings.TrimSuffix(baseURL, "/")
22-
src := strings.TrimPrefix(fn.ID, "/")
23-
triggerPath := fmt.Sprintf("%s/invoke/%s", baseURL, src)
24+
path := strings.Replace(template, ":fn_id", fn.ID, -1)
25+
invokePath := fmt.Sprintf("%s%s%s", baseURL, group, path)
2426

2527
newT := fn.Clone()
26-
newAnnotations, err := newT.Annotations.With(models.FnInvokeEndpointAnnotation, triggerPath)
28+
newAnnotations, err := newT.Annotations.With(models.FnInvokeEndpointAnnotation, invokePath)
2729
if err != nil {
2830
return nil, err
2931
}
@@ -39,25 +41,25 @@ func (tp *requestBasedFnAnnotator) AnnotateFn(ctx *gin.Context, app *models.App,
3941
scheme = "https"
4042
}
4143

42-
return annotateFnWithBaseURL(fmt.Sprintf("%s://%s", scheme, ctx.Request.Host), app, t)
44+
return annotateFnWithBaseURL(fmt.Sprintf("%s://%s", scheme, ctx.Request.Host), tp.group, tp.template, app, t)
4345
}
4446

4547
//NewRequestBasedFnAnnotator creates a FnAnnotator that inspects the incoming request host and port, and uses this to generate fn invoke endpoint URLs based on those
46-
func NewRequestBasedFnAnnotator() FnAnnotator {
47-
return &requestBasedFnAnnotator{}
48+
func NewRequestBasedFnAnnotator(group, template string) FnAnnotator {
49+
return &requestBasedFnAnnotator{group: group, template: template}
4850
}
4951

5052
type staticURLFnAnnotator struct {
51-
baseURL string
53+
baseURL, group, template string
5254
}
5355

5456
//NewStaticURLFnAnnotator annotates triggers bases on a given, specified URL base - e.g. "https://my.domain" ---> "https://my.domain/t/app/source"
55-
func NewStaticURLFnAnnotator(baseURL string) FnAnnotator {
57+
func NewStaticURLFnAnnotator(baseURL, group, template string) FnAnnotator {
5658

57-
return &staticURLFnAnnotator{baseURL: baseURL}
59+
return &staticURLFnAnnotator{baseURL: baseURL, group: group, template: template}
5860
}
5961

6062
func (s *staticURLFnAnnotator) AnnotateFn(ctx *gin.Context, app *models.App, trigger *models.Fn) (*models.Fn, error) {
61-
return annotateFnWithBaseURL(s.baseURL, app, trigger)
63+
return annotateFnWithBaseURL(s.baseURL, s.group, s.template, app, trigger)
6264

6365
}

api/server/server.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ const (
119119
// EnvMaxRequestSize sets the limit in bytes for any API request's length.
120120
EnvMaxRequestSize = "FN_MAX_REQUEST_SIZE"
121121

122+
// EnvInvokeGroup sets the API group for the invoke endpoint
123+
EnvInvokeGroup = "FN_INVOKE_GROUP"
124+
125+
// EnvInvokeTemplate sets the API path for the invoke endpoing
126+
EnvInvokeTemplate = "FN_INVOKE_PATH"
127+
128+
// DefaultInvokeGroup is "/v2"
129+
DefaultInvokeGroup = "/v2"
130+
131+
// DefaultInvokeTemplate is "/:fn_id"
132+
DefaultInvokeTemplate = "/:fn_id"
133+
122134
// DefaultLogFormat is text
123135
DefaultLogFormat = "text"
124136

@@ -266,14 +278,16 @@ func NewFromEnv(ctx context.Context, opts ...Option) *Server {
266278

267279
opts = append(opts, LimitRequestBody(int64(getEnvInt(EnvMaxRequestSize, 0))))
268280

281+
invokeGroup := getEnv(EnvInvokeGroup, DefaultInvokeGroup)
282+
invokeTemplate := getEnv(EnvInvokeTemplate, DefaultInvokeTemplate)
269283
publicLBURL := getEnv(EnvPublicLoadBalancerURL, "")
270284
if publicLBURL != "" {
271285
logrus.Infof("using LB Base URL: '%s'", publicLBURL)
272286
opts = append(opts, WithTriggerAnnotator(NewStaticURLTriggerAnnotator(publicLBURL)))
273-
opts = append(opts, WithFnAnnotator(NewStaticURLFnAnnotator(publicLBURL)))
287+
opts = append(opts, WithFnAnnotator(NewStaticURLFnAnnotator(publicLBURL, invokeGroup, invokeTemplate)))
274288
} else {
275289
opts = append(opts, WithTriggerAnnotator(NewRequestBasedTriggerAnnotator()))
276-
opts = append(opts, WithFnAnnotator(NewRequestBasedFnAnnotator()))
290+
opts = append(opts, WithFnAnnotator(NewRequestBasedFnAnnotator(invokeGroup, invokeTemplate)))
277291
}
278292

279293
// Agent handling depends on node type and several other options so it must be the last processed option.
@@ -1105,9 +1119,7 @@ func (s *Server) bindHandlers(ctx context.Context) {
11051119
profilerSetup(admin, "/debug")
11061120

11071121
// Pure runners don't have any route, they have grpc
1108-
switch s.nodeType {
1109-
1110-
case ServerTypeFull, ServerTypeAPI:
1122+
if s.nodeType == ServerTypeFull || s.nodeType == ServerTypeAPI {
11111123
cleanv2 := engine.Group("/v2")
11121124
v2 := cleanv2.Group("")
11131125
v2.Use(s.apiMiddlewareWrapper())
@@ -1159,17 +1171,16 @@ func (s *Server) bindHandlers(ctx context.Context) {
11591171
}
11601172
}
11611173

1162-
switch s.nodeType {
1163-
case ServerTypeFull, ServerTypeLB, ServerTypeRunner:
1174+
if s.nodeType == ServerTypeFull || s.nodeType == ServerTypeLB || s.nodeType == ServerTypeRunner {
11641175
if !s.noHTTTPTriggerEndpoint {
11651176
lbTriggerGroup := engine.Group("/t")
11661177
lbTriggerGroup.Any("/:app_name", s.handleHTTPTriggerCall)
11671178
lbTriggerGroup.Any("/:app_name/*trigger_source", s.handleHTTPTriggerCall)
11681179
}
11691180

11701181
if !s.noFnInvokeEndpoint {
1171-
lbFnInvokeGroup := engine.Group("/invoke")
1172-
lbFnInvokeGroup.POST("/:fn_id", s.handleFnInvokeCall)
1182+
lbFnInvokeGroup := engine.Group(getEnv(EnvInvokeGroup, DefaultInvokeGroup))
1183+
lbFnInvokeGroup.POST(getEnv(EnvInvokeTemplate, DefaultInvokeTemplate), s.handleFnInvokeCall)
11731184
}
11741185
}
11751186

0 commit comments

Comments
 (0)