fnRequest, pongo2-addons added
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
17
helper/template_filters.go
Normal file
17
helper/template_filters.go
Normal 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
|
||||
}
|
||||
51
helper/template_functions.go
Normal file
51
helper/template_functions.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user