-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
serve
command that exposes an API
- Loading branch information
1 parent
d8e1fb8
commit 6a60db0
Showing
8 changed files
with
222 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": "1", | ||
"name": "GTTP API", | ||
"type": "collection" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.