Skip to content

Commit

Permalink
feat: added serve command that exposes an API
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Feb 5, 2024
1 parent d8e1fb8 commit 6a60db0
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 77 deletions.
17 changes: 17 additions & 0 deletions .bruno/Parse Template.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
meta {
name: Parse Template
type: http
seq: 1
}

post {
url: http://127.0.0.1:8080/api/v1/parse
body: json
auth: none
}

body:json {
{
"template": "variables:\n - name: Text\n type: text\n regex: ^[a-z]+$\n value: abc\ntemplate: |-\n \{{ .Text }}"
}
}
5 changes: 5 additions & 0 deletions .bruno/bruno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "1",
"name": "GTTP API",
"type": "collection"
}
3 changes: 0 additions & 3 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
func init() {
rootCmd.AddCommand(parseCmd)

// URL flag
parseCmd.Flags().StringP("url", "u", "", "Fetch template from URL")

// File flag
parseCmd.Flags().StringP("file", "f", "", "Fetch template from file")
}

Expand Down
16 changes: 15 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gttp-cli/gttp/pkg/model"
"github.com/gttp-cli/gttp/pkg/parser"
"github.com/gttp-cli/gttp/pkg/utils"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
clip "golang.design/x/clipboard"
"os"
Expand All @@ -16,6 +17,7 @@ func init() {
rootCmd.Flags().StringP("output", "o", "", "Output file")
rootCmd.Flags().BoolP("clipboard", "c", false, "Copy output to clipboard")
rootCmd.Flags().BoolP("silent", "s", false, "Silent mode")
rootCmd.Flags().BoolP("debug", "d", false, "Print debug information")
}

var rootCmd = &cobra.Command{
Expand All @@ -27,6 +29,7 @@ var rootCmd = &cobra.Command{
output, _ := cmd.Flags().GetString("output")
silent, _ := cmd.Flags().GetBool("silent")
clipboard, _ := cmd.Flags().GetBool("clipboard")
debug, _ := cmd.Flags().GetBool("debug")

// Do not allow both URL and file flags to be set
if url != "" && file != "" {
Expand Down Expand Up @@ -56,7 +59,12 @@ var rootCmd = &cobra.Command{
return err
}

result, err := parser.ParseTemplate(tmpl)
tmpl, err = parser.ParseTemplate(tmpl)
if err != nil {
return err
}

result, err := parser.RenderTemplate(tmpl)
if err != nil {
return err
}
Expand All @@ -79,6 +87,12 @@ var rootCmd = &cobra.Command{
fmt.Println(result)
}

if debug {
pterm.DefaultSection.Println("Debug Information")
pterm.DefaultSection.WithLevel(2).Println("Template")
pterm.Printfln("%#v", tmpl)
}

return nil
},
}
Expand Down
84 changes: 84 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cmd

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gttp-cli/gttp/pkg/model"
"github.com/gttp-cli/gttp/pkg/parser"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(serveCmd)

// Add address flag
serveCmd.Flags().StringP("address", "a", "localhost:8080", "Address to listen on")
}

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start API server",
RunE: func(cmd *cobra.Command, args []string) error {
addr, _ := cmd.Flags().GetString("address")
app := fiber.New()

app.Use(logger.New())

app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(map[string]string{
"status": "ok",
"docs": "https://docs.gttp.dev",
})
})

api := app.Group("/api")
v1 := api.Group("/v1")

// /parse accepts YAML and returns the parsed template as JSON
v1.Post("/parse", func(c *fiber.Ctx) error {
// Get template from JSON "template" key
body := struct {
Template string `json:"template"`
}{}

if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(map[string]string{
"error": err.Error(),
})
}

tmpl, err := model.FromYAML(body.Template)
if err != nil {
return c.Status(400).JSON(map[string]string{
"error": err.Error(),
})
}

// Validate template
errs := tmpl.Validate()
if errs != nil {
var errors []string
for _, err := range errs {
errors = append(errors, err.Error())
}
return c.Status(400).JSON(map[string]interface{}{
"errors": errors,
})
}

rendered, err := parser.RenderTemplate(tmpl)
if err != nil {
return c.Status(500).JSON(map[string]string{
"error": err.Error(),
})
}

return c.JSON(map[string]string{
"template": body.Template,
"rendered": rendered,
})
})

return app.Listen(addr)
},
}
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/expr-lang/expr v1.16.0
github.com/goccy/go-yaml v1.11.3
github.com/gofiber/fiber/v2 v2.52.0
github.com/invopop/jsonschema v0.12.0
github.com/pterm/pterm v0.12.76
github.com/spf13/cobra v1.8.0
Expand All @@ -18,15 +19,17 @@ require (
atomicgo.dev/schedule v0.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -40,9 +43,12 @@ require (
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp/shiny v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
Expand Down
19 changes: 16 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
Expand All @@ -44,10 +46,13 @@ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7a
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
Expand All @@ -61,6 +66,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
Expand Down Expand Up @@ -123,6 +130,12 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
Expand All @@ -134,8 +147,8 @@ golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp/shiny v0.0.0-20240119083558-1b970713d09a h1:NZ9mAQhIcCceDZKqQX3JJVIz7nn3QLDuC+nXedsViBM=
Expand Down
Loading

0 comments on commit 6a60db0

Please sign in to comment.