119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
package helper
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"gitbase.de/apairon/mark2web/config"
|
|
"github.com/flosch/pongo2"
|
|
)
|
|
|
|
func jsonWebRequest(url string) config.MapString {
|
|
Log.Noticef("requesting url via GET %s", url)
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
Log.Panicf("could not get url '%s': %s", url, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
Log.Panicf("could not read body from url '%s': %s", url, err)
|
|
}
|
|
|
|
Log.Debugf("output from url '%s':\n%s", url, string(body))
|
|
|
|
if resp.StatusCode >= 400 {
|
|
Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, url)
|
|
}
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
if strings.Contains(contentType, "json") {
|
|
|
|
} else {
|
|
Log.Panicf("is not json '%s' from url '%s'", contentType, url)
|
|
}
|
|
|
|
jsonMap := make(config.MapString)
|
|
err = json.Unmarshal(body, &jsonMap)
|
|
if err != nil {
|
|
Log.Panicf("could not read json from '%s': %s", url, err)
|
|
}
|
|
|
|
return jsonMap
|
|
}
|
|
|
|
// RequestFn will make a web request and returns map[string]interface form pongo2
|
|
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
|
|
u := url.String()
|
|
return pongo2.AsValue(jsonWebRequest(u))
|
|
}
|
|
|
|
func add2Nav(currentNode *config.PathConfigTree, pathConfig *config.PathConfig, tplFilename, outDir string, navname string, ctx interface{}, dataMapKey string, body string, hidden bool) {
|
|
newNodeConfig := new(config.PathConfigTree)
|
|
fillNodeConfig(
|
|
newNodeConfig,
|
|
currentNode.InputPath,
|
|
currentNode.OutputPath,
|
|
outDir,
|
|
pathConfig,
|
|
)
|
|
if navname != "" {
|
|
newNodeConfig.Config.This = config.ThisPathConfig{
|
|
Navname: &navname,
|
|
}
|
|
}
|
|
if dataMapKey != "" {
|
|
if newNodeConfig.Config.Data == nil {
|
|
newNodeConfig.Config.Data = make(config.MapString)
|
|
}
|
|
// as submap in Data
|
|
newNodeConfig.Config.Data[dataMapKey] = ctx
|
|
} else if m, ok := ctx.(map[string]interface{}); ok {
|
|
// direct set data
|
|
newNodeConfig.Config.Data = m
|
|
}
|
|
|
|
// fake via normal file behavior
|
|
newNodeConfig.Config.Template = &tplFilename
|
|
newNodeConfig.InputFiles = []string{""} // empty file is special for use InputString
|
|
indexInFile := ""
|
|
indexOutFile := "index.html"
|
|
if idx := newNodeConfig.Config.Index; idx != nil {
|
|
if idx.OutputFile != nil && *idx.OutputFile != "" {
|
|
indexOutFile = *idx.OutputFile
|
|
}
|
|
}
|
|
newNodeConfig.Config.Index = &config.IndexConfig{
|
|
InputFile: &indexInFile,
|
|
OutputFile: &indexOutFile,
|
|
InputString: &body,
|
|
}
|
|
newNodeConfig.Hidden = hidden
|
|
|
|
currentNode.Sub = append(currentNode.Sub, newNodeConfig)
|
|
}
|
|
|
|
// RenderFn renders a pongo2 template with additional context
|
|
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
|
|
dataMapKey := ""
|
|
body := ""
|
|
|
|
for i, p := range param {
|
|
switch i {
|
|
case 0:
|
|
dataMapKey = p.String()
|
|
case 1:
|
|
body = p.String()
|
|
}
|
|
}
|
|
|
|
add2Nav(currentTreeNodeConfig, currentPathConfig, templateFilename.String(), outDir.String(), "", ctx.Interface(), dataMapKey, body, true)
|
|
|
|
return pongo2.AsValue(nil)
|
|
}
|