From 29771902f4c4a2e5d24ebd36928c913dbd3e8c05 Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Mon, 5 Feb 2024 16:05:35 +0100 Subject: [PATCH] feat: make API accept JSON --- _examples/text/regex/regex.yml | 7 +++++++ cmd/serve.go | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 _examples/text/regex/regex.yml diff --git a/_examples/text/regex/regex.yml b/_examples/text/regex/regex.yml new file mode 100644 index 0000000..163956f --- /dev/null +++ b/_examples/text/regex/regex.yml @@ -0,0 +1,7 @@ +variables: + - name: Text + type: text + regex: ^[a-z]+$ # only allow lowercase letters + description: A string of lowercase letters +template: |- + {{ .Text }} diff --git a/cmd/serve.go b/cmd/serve.go index d2a9b7c..cdf4b49 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -6,6 +6,7 @@ import ( "github.com/gttp-cli/gttp/pkg/model" "github.com/gttp-cli/gttp/pkg/parser" "github.com/spf13/cobra" + "strings" ) func init() { @@ -47,7 +48,15 @@ var serveCmd = &cobra.Command{ }) } - tmpl, err := model.FromYAML(body.Template) + var tmpl model.Template + var err error + + if strings.HasPrefix(body.Template, "{") { + tmpl, err = model.FromJSON(body.Template) + } else { + tmpl, err = model.FromYAML(body.Template) + } + if err != nil { return c.Status(400).JSON(map[string]string{ "error": err.Error(),