Skip to content

Commit

Permalink
fix: go.work.tmpl go version
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed Dec 6, 2023
1 parent f230d63 commit 9a80d00
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
29 changes: 23 additions & 6 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,11 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

deployments, err := s.dal.GetActiveDeployments(r.Context())
sch, err := s.getActiveSchema(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sch := &schema.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *schema.Module {
return d.Schema
}),
}

body, err := ingress.ValidateAndExtractBody(route, r, sch)
if err != nil {
Expand Down Expand Up @@ -587,6 +582,16 @@ func (s *Service) Call(ctx context.Context, req *connect.Request[ftlv1.CallReque
}
verbRef := schema.VerbRefFromProto(req.Msg.Verb)

sch, err := s.getActiveSchema(ctx)
if err != nil {
return nil, err
}

err = ingress.ValidateCallBody(req.Msg.Body, verbRef, sch)
if err != nil {
return nil, err
}

module := verbRef.Module
s.routesMu.RLock()
routes, ok := s.routes[module]
Expand Down Expand Up @@ -1042,6 +1047,18 @@ func (s *Service) syncRoutes(ctx context.Context) error {
return nil
}

func (s *Service) getActiveSchema(ctx context.Context) (*schema.Schema, error) {
deployments, err := s.dal.GetActiveDeployments(ctx)
if err != nil {
return nil, err
}
return &schema.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *schema.Module {
return d.Schema
}),
}, nil
}

// Copied from the Apache-licensed connect-go source.
func connectCodeToHTTP(code connect.Code) int {
switch code {
Expand Down
19 changes: 19 additions & 0 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ func matchSegments(pattern, urlPath string, onMatch func(segment, value string))
return true
}

func ValidateCallBody(body []byte, verbRef *schema.VerbRef, sch *schema.Schema) error {
var requestMap map[string]any
err := json.Unmarshal(body, &requestMap)
if err != nil {
return fmt.Errorf("HTTP request body is not valid JSON: %w", err)
}

verb := sch.ResolveVerbRef(verbRef)
if verb == nil {
return fmt.Errorf("unknown verb %s", verbRef)
}

dataRef := verb.Request

fmt.Printf("dataRef: %v\n", dataRef)
fmt.Printf("requestMap: %v\n", requestMap)
return validateRequestMap(dataRef, []string{dataRef.String()}, requestMap, sch)
}

// ValidateAndExtractBody validates the request body against the schema and extracts the request body as a JSON blob.
func ValidateAndExtractBody(route *dal.IngressRoute, r *http.Request, sch *schema.Schema) ([]byte, error) {
requestMap, err := buildRequestMap(route, r)
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/compile/generate/go.work.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.21.4
go 1.21.5

use (
.
Expand Down

0 comments on commit 9a80d00

Please sign in to comment.