How to use rendered template in creating a pdf - pdf

Ok so I am Go Lang with the Echo framework, to try and build pdf which will load data from a database source - that bit will come later.
So this is how I am rendering my pdf html layout,
func (c *Controller) DataTest(ec echo.Context) error {
return ec.Render(http.StatusOK, "pdf.html", map[string]interface{}{
"name": "TEST",
"msg": "Hello, XXXX!",
})
}
The above function works fine, and renders the html (I built a temp path to the function). Now I want to use that function as my html template to build my pdf's.
So I am using wkhtmltopdf and the lib "github.com/SebastiaanKlippert/go-wkhtmltopdf"
This is how I would render the html in the pdf,
html, err := ioutil.ReadFile("./assets/pdf.html")
if err != nil {
return err
}
But I need to be able to update the template so that is why I am trying to render the page and take that into the pdf.
However, the Echo framework returns an error type and not of type bytes or strings and I am not sure how to update it so my rendered content is returned as bytes?
Thanks,
UPDATE
page := wkhtmltopdf.NewPageReader(bytes.NewReader(c.DataTest(data)))
This is how I am currently doing, the data is just a html string which is then turned into a slice of bytes for the NewReader.
This works fine, but I wanted to turn the DataTest function into a fully rendered html page by Echo. The problem with that is that when you return a rendered page, it is returned as an error type.
So I was trying to work out a why of updating it, so I could return the data as an html string, which would then be put in as a slice of bytes.

if you want rendered html, use echo' custom middleware. I hope it helps you.
main.go
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io"
"net"
"net/http"
"github.com/labstack/echo"
)
type TemplateRegistry struct {
templates map[string]*template.Template
}
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
return tmpl.ExecuteTemplate(w, "base.html", data)
}
func main() {
e := echo.New()
templates := make(map[string]*template.Template)
templates["about.html"] = template.Must(template.ParseFiles("view/about.html", "view/base.html"))
e.Renderer = &TemplateRegistry{
templates: templates,
}
// add custom middleware
// e.Use(PdfMiddleware)
// only AboutHandler for Pdf
e.GET("/about", PdfMiddleware(AboutHandler))
// Start the Echo server
e.Logger.Fatal(e.Start(":8080"))
}
// custom middleware
func PdfMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
resBody := new(bytes.Buffer)
mw := io.MultiWriter(c.Response().Writer, resBody)
writer := &bodyDumpResponseWriter{Writer: mw, ResponseWriter: c.Response().Writer}
c.Response().Writer = writer
if err = next(c); err != nil {
c.Error(err)
}
// or use resBody.Bytes()
fmt.Println(resBody.String())
return
}
}
type bodyDumpResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w *bodyDumpResponseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
}
func (w *bodyDumpResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w *bodyDumpResponseWriter) Flush() {
w.ResponseWriter.(http.Flusher).Flush()
}
func (w *bodyDumpResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.ResponseWriter.(http.Hijacker).Hijack()
}
func AboutHandler(c echo.Context) error {
return c.Render(http.StatusOK, "about.html", map[string]interface{}{
"name": "About",
"msg": "All about Boatswain!",
})
}
view/about.html
{{define "title"}}
Boatswain Blog | {{index . "name"}}
{{end}}
{{define "body"}}
<h1>{{index . "msg"}}</h1>
<h2>This is the about page.</h2>
{{end}}
view/base.html
{{define "base.html"}}
<!DOCTYPE html>
<html>
<head>
<title>{{template "title" .}}</title>
</head>
<body>
{{template "body" .}}
</body>
</html>
{{end}}

So from what I understand you want to:
Render HTML from template
Convert HTML to PDF
Send it as an HTTP response? This part is unclear from your question, but it doesn't matter really.
So, the reason Echo returns error is because it actually not only does the template rendering, but also sending a response to client. If you want to do something else in-between, you can't use that method from echo.
Luckily, echo doesn't do anything magical there. You can just use the standard template package with the same result.
func GetHtml(filename string, data interface{}) (string, error) {
filedata, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
asString := string(filedata)
t, err := template.New("any-name").Parse(asString)
if err != nil {
return "", err
}
var buffer bytes.Buffer
err = t.Execute(&buffer, data)
if err != nil {
return "", err
}
return buffer.String(), nil
}
There you have your function that returns a string. You can use buffer.Bytes() to have a byte array if that's preferrable.
After this you can do whatever you need to, like convert to PDF and write it back to clients using echoCtx.Response().Writer().
Hope that helps, but in future try asking more precise questions, then you are more likely to receive an accurate response.

Related

auth error using gqlgen and gin returns an empty response

There are no recipes for this which is surprising as it seems like a pretty basic use case.
If a request does not have a valid token, we want to return a 403 to the user. But trying to do that results in an empty response and "missing response context" error.
So this is currently the middleware:
func (auth \*Auth) Middleware() gin.HandlerFunc {
return func(c \*gin.Context) {
_, err := auth.Validate(c.Request.Context(), c.Request)
if (err != nil) {
c.AbortWithError(http.StatusForbidden, err)
return
}
c.Next()
}
}
Using it here in server.go
func Register(config \*viper.Viper, broker \*broker.Broker, metrics \*metrics.Metrics, clients *clients.Clients, auth \*auth.Auth) {
//...
r.Use(auth.Middleware())
//...
r.POST("/query", graphqlHandler(broker, clients, metrics))
r.GET("/query", graphqlHandler(broker, clients, metrics))
r.GET("/", playgroundHandler())
_ = r.Run()
}
There is no response returned in the event of an invalid token and you get:
missing response context
I was thinking I could use graphql.WithResponseContext but since that's immutable and we are aborting so there is no "next", there is no place for that context to go.

How to implement new media types in swagger backend

I have created a swagger specification which produces "application/zip"
/config:
get:
produces:
- application/zip
responses:
200: # OK
description: All config files
schema:
type: string
format: binary
I have implemented the handlers for this endpoint but I get this error
http: panic serving 127.0.0.1:20366: applicationZip producer has not yet been implemented
This error is coming from this code
func NewSampleAPI(spec *loads.Document) *SampleAPI {
return &SampleAPI{
...
ApplicationZipProducer: runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
return errors.NotImplemented("applicationZip producer has not yet been implemented")
}),
After investigating this error my findings are that we need to implement something like this
api := operations.NewSampleAPI(swaggerSpec)
api.ApplicationZipProducer = func(w io.Writer, data interface{}) error {
...
}
So my question is that
what should we put in this Producer and why is it necessary to implement this because there is no implementation for "application/json" ?
Is "application/json" Producer is implemented by default and we need to implement other producers?
Note: I am using swagger "2.0" spec
Since you have used "application/zip" as response content then you might have implemented the backend code which might be returning io.ReadCloser.
Then your Producer will look like this
api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
if w == nil {
return errors.New("ApplicationZipProducer requires a writer") // early exit
}
if data == nil {
return errors.New("no data given to produce zip from")
}
if zp, ok := data.(io.ReadCloser); ok {
b, err := ioutil.ReadAll(zp)
if err != nil {
return fmt.Errorf("application zip producer: %v", err)
}
_, err = w.Write(b)
return nil
}
return fmt.Errorf("%v (%T) is not supported by the ApplicationZipProducer, %s", data, data)
})
This will parse the data interface into io.ReadCloser and read data from it and then it will fill into the io.Writer
Note: If your main purpose is just send file as attachment then you should use "application/octet-stream" and its producer is implemented by default

Go Unmarshalling Response from API

I have been working with Go and the Bitmex API.
I have the following Bitmex api endpoint:
https://www.bitmex.com/api/v1/leaderboard
This returns an array of JSON objects structured as follows.
[
{
"name": "string",
"isRealName": true,
"profit": 0
}
]
However I get the following incorrect representation of the JSON when I marshal it.
[{0 false } {0 false } ... ]
I know my HTTP request is going through, as when I print the response.Body I get the following
[{"profit":256915996199,"isRealName":true,"name":"angelobtc"} ... ]
Here is the struct I am using to store the marshaled data.
type LeaderboardResponse struct {
profit float64 `json:"profit"`
isRealName bool `json:"isRealName"`
name string `json:"name"`
}
And my main method
func main() {
response, errorResponse := http.Get("https://www.bitmex.com/api/v1/leaderboard")
if response.Status == "200 OK"{
if errorResponse != nil {
fmt.Printf("The HTTP request failed with error %s\n",errorResponse)
} else {
body, errorBody := ioutil.ReadAll(response.Body)
if errorBody != nil {
fmt.Println("There was an error retrieving the body", errorBody)
} else {
leaderboard := []LeaderboardResponse{}
json.Unmarshal([]byte(body),&leaderboard)
if leaderboard != nil {
fmt.Println(leaderboard);
//The result of the statement above and the one below are different
fmt.Println(string(body))
} else {
fmt.Println("leaderboard array is undefined")
}
defer response.Body.Close()
}
}
} else {
fmt.Println("Response received with status ", string(response.Status))
}
}
It appears that the values of struct have not been modifies despite it being assigned the marshaled JSON body
How do I fix this issue?
Furthermore,
How do I add my API credentials to the HTTP request?
How do I access the response header and marshal it?

Golang Api Only Matches Last Route

I have a golang api application. I've defined a set of routes and handlers. However, the mux router only ever returns the last route.
When I request /api/info I get this in my logging:
9:0:38 app | 2018/02/05 09:00:38 GET /api/info Users Create 308.132µs
Why is that routing to the wrong route?
routing package:
// NewRouter establishes the root application router
func NewRouter(context *config.ApplicationContext, routes Routes, notFoundHandler http.HandlerFunc) *mux.Router {
router := mux.NewRouter()
router.NotFoundHandler = notFoundHandler
for _, route := range routes {
router.
PathPrefix("/api").
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
// TODO: fix HandlerFunc. Right now, it is overriding previous routes and setting a single handler for all
// this means that the last route is the only router with a handler
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logRoute(setJSONHeader(route.HandlerFunc), route.Name)(context, w, r)
})
}
return router
}
func logRoute(inner ContextHandlerFunc, name string) ContextHandlerFunc {
return func(c *config.ApplicationContext, w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner(c, w, r)
log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
}
}
func setJSONHeader(inner ContextHandlerFunc) ContextHandlerFunc {
return func(c *config.ApplicationContext, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
inner(c, w, r)
}
}
main package:
var context = config.ApplicationContext{
Database: database.NewDatabase().Store,
}
var routes = router.Routes{
router.Route{"Info", "GET", "/info", handlers.InfoShow},
router.Route{"Users Create", "POST", "/users/create", handlers.UsersCreate},
}
func main() {
notFoundHandler := handlers.Errors404
router := router.NewRouter(&context, routes, notFoundHandler)
port := os.Getenv("PORT")
log.Fatal(http.ListenAndServe(":"+port, router))
}
If I visit /api/info it will attempt to call a POST to /users/create. However, if I remove the second route, it will correctly route to the InfoShow handler.
Why is mux overriding the first route? I'm fairly certain there's something wrong with
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logRoute(setJSONHeader(route.HandlerFunc), route.Name)(context, w, r)
})
but I'm not sure why that would cause it to map over the first route.
Ideas?
Reading through your code and gorilla/mux, I think I know the issue. You're using the for loop variable route, and specifically its field HanderFunc, in the function literal, but because of how function literals work, the value of that field is not evaluated until the that function literal is called. In Go, the second variable in a range loop is reused on each iteration, rather than created anew, and so after the for loop, if it's still in scope of anything (like your function literal), it will contain the value of the last loop iteration. Here's an example of what I mean:
https://play.golang.org/p/Xx62tuwhtgG
package main
import (
"fmt"
)
func main() {
var funcs []func()
ints := []int{1, 2, 3, 4, 5}
// How you're doing it
for i, a := range ints {
fmt.Printf("Loop i: %v, a: %v\n", i, a)
funcs = append(funcs, func() {
fmt.Printf("Lambda i: %v, a: %v\n", i, a)
})
}
for _, f := range funcs {
f()
}
fmt.Println("-------------")
// How you *should* do it
funcs = nil
for i, a := range ints {
i := i
a := a
fmt.Printf("Loop i: %v, a: %v\n", i, a)
funcs = append(funcs, func() {
fmt.Printf("Lambda i: %v, a: %v\n", i, a)
})
}
for _, f := range funcs {
f()
}
}
In the first example, i and a are being reused on each loop iteration, and aren't evaluated for their values in the lambda (function literal) until that lambda is actually called (by the funcs loop). To fix that, you can shadow a and i by redeclaring them inside the scope of the loop iteration (but outside the lambda's scope). This makes a separate copy for each iteration, to avoid issues with reuse of the same variable.
Specifically for your code, if you change your code to the following, it should work:
for _, route := range routes {
route := route // make a copy of the route for use in the lambda
// or alternatively, make scoped vars for the name and handler func
router.
PathPrefix("/api").
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
// TODO: fix HandlerFunc. Right now, it is overriding previous routes and setting a single handler for all
// this means that the last route is the only router with a handler
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logRoute(setJSONHeader(route.HandlerFunc), route.Name)(context, w, r)
})
}

how to handle New Server func in unit testing

for me , unit testing has workload. so i use gotests to generate Boilerplate testing code case.
server.go
func NewServer(cfg *Config, l net.Listener, driver Driver, db store.Store) *Server {
s := &Server{
cfg: cfg,
listener: l,
leader: "",
driver: driver,
db: db,
}
s.server = &http.Server{
Handler: s.createMux(),
}
return s
}
gotests generate server_test.go:
func TestNewServer(t *testing.T) {
fakeCfg := &Config{
Listen: "hello",
LogLevel: "debug",
}
type args struct {
cfg *Config
l net.Listener
leader string
driver Driver
db store.Store
}
tests := []struct {
name string
args args
want *Server
}{
{
name: "test",
args: args{
cfg: fakeCfg,
},
want: &Server{
cfg: fakeCfg,
server: &http.Server{
Handler: nil,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewServer(tt.args.cfg, tt.args.l, tt.args.driver, tt.args.db); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewServer() = %v, want %v", got, tt.want)
}
})
}
}
unit test result:
$ go test -v -run TestNewServer
=== RUN TestNewServer
=== RUN TestNewServer/test
--- FAIL: TestNewServer (0.00s)
--- FAIL: TestNewServer/test (0.00s)
server_test.go:47: NewServer() = &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6840 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}, want &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6790 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}
FAIL
exit status 1
FAIL github.com/Dataman-Cloud/swan/api 0.017s
because the initial server struct is not one step. i can't correct get server attribute in &Server{} section.
anyone can do me a favor, give me a hint?howto write test on this situation?
You are comparing the pointers and not the values.
You should change your test to:
!reflect.DeepEqual(*got, *tt.want)
Then you are comparing the content of the structs.
finally i do a trick test, i manually compose every element of the object. it works like a charm.