Skip to content

Commit

Permalink
feat: make API accept JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Feb 5, 2024
1 parent 6a60db0 commit 2977190
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions _examples/text/regex/regex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variables:
- name: Text
type: text
regex: ^[a-z]+$ # only allow lowercase letters
description: A string of lowercase letters
template: |-
{{ .Text }}
11 changes: 10 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 2977190

Please sign in to comment.