Skip to content

Commit

Permalink
wip: cors support
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws committed Oct 20, 2023
1 parent 5d8ae02 commit 8f83aee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/codeconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Api struct {
parent *FunctionDependencies
securityDefinitions map[string]*v1.ApiSecurityDefinition
security map[string][]string
cors *v1.ApiCorsDefinition
workers []*v1.ApiWorker
lock sync.RWMutex
}
Expand Down Expand Up @@ -108,3 +109,10 @@ func (a *Api) AddSecurity(name string, scopes []string) {
a.security[name] = []string{}
}
}

func (a *Api) AddCors(cors *v1.ApiCorsDefinition) {
a.lock.Lock()
defer a.lock.Unlock()

a.cors = cors
}
4 changes: 3 additions & 1 deletion pkg/codeconfig/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ func (a *FunctionDependencies) AddApiSecurityDefinitions(name string, sds map[st
}
}

func (a *FunctionDependencies) AddApiSecurity(name string, security map[string]*v1.ApiScopes) {
func (a *FunctionDependencies) AddApiSecurity(name string, security map[string]*v1.ApiScopes, cors *v1.ApiCorsDefinition) {
a.lock.Lock()
defer a.lock.Unlock()

if a.apis[name] == nil {
a.apis[name] = newApi(a)
}

a.apis[name].AddCors(cors)

for n, scopes := range security {
a.apis[name].AddSecurity(n, scopes.Scopes)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codeconfig/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Server) Declare(ctx context.Context, req *v1.ResourceDeclareRequest) (*
s.function.AddSecret(req.Resource.Name, req.GetSecret())
case v1.ResourceType_Api:
s.function.AddApiSecurityDefinitions(req.Resource.Name, req.GetApi().SecurityDefinitions)
s.function.AddApiSecurity(req.Resource.Name, req.GetApi().Security)
s.function.AddApiSecurity(req.Resource.Name, req.GetApi().Security, req.GetApi().GetCors())
case v1.ResourceType_Websocket:
// TODO: Add websocket configuration here when available
break
Expand Down
3 changes: 2 additions & 1 deletion pkg/codeconfig/uprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *codeConfig) ToUpRequest() (*deploy.DeployUpRequest, error) {
builder.set(res)
}

for k := range f.apis {
for k, api := range f.apis {
spec, err := c.apiSpec(k, nil)
if err != nil {
errs.Push(fmt.Errorf("could not build spec for api: %s; %w", k, err))
Expand All @@ -216,6 +216,7 @@ func (c *codeConfig) ToUpRequest() (*deploy.DeployUpRequest, error) {
Document: &deploy.Api_Openapi{
Openapi: string(apiBody),
},
Cors: api.cors,
},
},
})
Expand Down

0 comments on commit 8f83aee

Please sign in to comment.