fnRequest, pongo2-addons added
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sebastian Frank
2019-02-27 15:58:10 +01:00
parent 4c2a13d6b5
commit 8f1345d4aa
13 changed files with 132 additions and 7 deletions

View File

@@ -0,0 +1,17 @@
package helper
import (
"github.com/flosch/pongo2"
_ "github.com/flosch/pongo2-addons"
"gopkg.in/russross/blackfriday.v2"
)
func init() {
pongo2.ReplaceFilter("markdown", MarkdownFilter)
}
// MarkdownFilter is a pongo2 filter, which converts markdown to html
func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
html := blackfriday.Run([]byte(in.String()))
return pongo2.AsSafeValue(string(html)), nil
}

View File

@@ -0,0 +1,51 @@
package helper
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/flosch/pongo2"
)
// Request will make a web request and returns map[string]interface form pongo2
func Request(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
u := url.String()
fmt.Printf("request GET %s\n", u)
resp, err := http.Get(u)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
spew.Dump(string(body))
if resp.StatusCode >= 400 {
panic(resp.Status)
}
contentType := resp.Header.Get("Content-Type")
if strings.Contains(contentType, "json") {
} else {
panic("invalid content-type")
}
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
panic(err)
}
return pongo2.AsValue(jsonMap)
}