diff --git a/backend/controller/ingress/testdata/go/httpingress/httpingress.go b/backend/controller/ingress/testdata/go/httpingress/httpingress.go index f6e4a92323..e5711c9909 100644 --- a/backend/controller/ingress/testdata/go/httpingress/httpingress.go +++ b/backend/controller/ingress/testdata/go/httpingress/httpingress.go @@ -103,9 +103,7 @@ type QueryParamRequest struct { //ftl:ingress http GET /queryparams func Query(ctx context.Context, req builtin.HttpRequest[ftl.Unit, ftl.Unit, QueryParamRequest]) (builtin.HttpResponse[string, string], error) { - return builtin.HttpResponse[string, string]{ - Body: ftl.Some(req.Query.Foo.Default("No value")), - }, nil + return builtin.HttpSuccess[string](200, req.Query.Foo.Default("No value")), nil } //ftl:ingress http GET /html @@ -138,7 +136,7 @@ func Int(ctx context.Context, req builtin.HttpRequest[int, ftl.Unit, ftl.Unit]) //ftl:ingress http POST /float func Float(ctx context.Context, req builtin.HttpRequest[float64, ftl.Unit, ftl.Unit]) (builtin.HttpResponse[float64, string], error) { - return builtin.HttpResponse[float64, string]{Body: ftl.Some(req.Body)}, nil + return builtin.HttpSuccess[string](200, req.Body), nil } //ftl:ingress http POST /bool @@ -148,10 +146,7 @@ func Bool(ctx context.Context, req builtin.HttpRequest[bool, ftl.Unit, ftl.Unit] //ftl:ingress http GET /error func Error(ctx context.Context, req builtin.HttpRequest[ftl.Unit, ftl.Unit, ftl.Unit]) (builtin.HttpResponse[ftl.Unit, string], error) { - return builtin.HttpResponse[ftl.Unit, string]{ - Status: 500, - Error: ftl.Some("Error from FTL"), - }, nil + return builtin.HttpError[ftl.Unit](500, "Error from FTL"), nil } //ftl:ingress http POST /array/string diff --git a/go-runtime/compile/external-module-template/.ftl/go/modules/{{ .Module.Name }}/external_module.go.tmpl b/go-runtime/compile/external-module-template/.ftl/go/modules/{{ .Module.Name }}/external_module.go.tmpl index bff5bbb2fa..11c054ee38 100644 --- a/go-runtime/compile/external-module-template/.ftl/go/modules/{{ .Module.Name }}/external_module.go.tmpl +++ b/go-runtime/compile/external-module-template/.ftl/go/modules/{{ .Module.Name }}/external_module.go.tmpl @@ -93,3 +93,17 @@ func init() { ) } {{- end}} +{{- if eq .Module.Name "builtin"}} + +// A helper for successful HttpResponse +// +func HttpSuccess[E any, B any](code int, body B) HttpResponse[B, E] { + return HttpResponse[B, E]{Status: code, Body: ftl.Some(body)} +} + +// A helper for HttpResponse with error +// +func HttpError[B any, E any](code int, e E) HttpResponse[B, E] { + return HttpResponse[B, E]{Status: code, Error: ftl.Some(e)} +} +{{- end}}