Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The current list for features for this CLI tool is as follows:
- upload a local directory to your account
- download a remote directory (from your account) to your machine
- develop HTML templates
- develop JSON templates

To start local development of templates, run:

Expand Down Expand Up @@ -60,9 +61,24 @@ afosto render -p 8888 -f afosto.config.yml

Where `-p` is the port number for the local webserver and `-f` points to the afosto.config.yml file in the root of the template repository.

### Develop JSON templates
By default, the render process assumes you're rendering HTML templates.
If you want to render JSON templates instead, you have to add an extra key in your config file:

```yaml
config:
- category: templates
routes:
- path: jsonTemplate
output: json # The required key for JSON templates
templatePath: path/to/your/template.tpl
queryPath: path/to/your/query.graphql
```

### Templating language

Within the templates that are rendered there a some (hidden) powerful features / filters that might be of use.
[Pongo2](https://github.com/flosch/pongo2) is used as the templating languages.
Within the templates there are a few (hidden) powerful features / filters that might be useful.

#### Sort

Expand All @@ -73,5 +89,3 @@ Sort takes in a slice or a map and sort this based on a key. Start with `-` in o
{{slice|sort:"-filters.key"}}
```



1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/afosto/cli
go 1.13

require (
github.com/dghubble/sling v1.3.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/flosch/pongo2/v4 v4.0.2
github.com/gen2brain/dlgs v0.0.0-20210406143744-f512297a108e
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -67,8 +65,6 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ func (ac *AfostoClient) Query(query string, parameters interface{}) (*QueryResul
Variables: parameters,
}))

req.Header.Set("content-type", "application/json")

var result QueryResult
b, _, err := handle(ac.client.Do(req))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/data/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Route struct {
Category string `yaml:"-"`
InlineCss bool `yaml:"inlineCss"`
TemplatePath string `yaml:"templatePath"`
Output string `yaml:"output"`
QueryPath *string `yaml:"queryPath"`
Vars map[string]interface{}
ResolvedQuery *string
Expand Down
28 changes: 25 additions & 3 deletions pkg/render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,43 @@ func (ds *developmentServer) render(w http.ResponseWriter, req *http.Request) {
}
}

logging.Log.WithField("input", entry.TemplatePath).Debug("rendering template entry")
outputType := "html"

w.Header().Set("content-type", "text/html")
if entry.Output != "" {
outputType = entry.Output
}
logging.Log.WithField("input", entry.TemplatePath).Debug("rendering template entry")

defer func() {
if r := recover(); r != nil {
w.Write([]byte(fmt.Sprintf("template error: %v", r)))
}
}()

err = tpl.ExecuteWriter(templateData, w)
content, err := tpl.Execute(templateData)
if err != nil {
logging.Log.Error(err)
return
}

if outputType == "json" {
var stub interface{}

if err := json.Unmarshal([]byte(content), &stub); err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
w.Write([]byte("\n ------------------------------------------ \n"))
w.Write([]byte("Content: \n\n"))
w.Write([]byte(content))
return
}

w.Header().Set("Content-Type", "application/json")
} else {
w.Header().Set("content-type", "text/html")
}

w.Write([]byte(content))
} else {
logging.Log.WithField("entry", p).Error("could not find entrypoint")
w.WriteHeader(404)
Expand Down