From 6d99712a301a6225366c1f92cdaeb8b2e680120d Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sun, 9 Jan 2022 08:03:03 +0300 Subject: Added custom renderers --- _examples/headers/test.http | 4 ++-- _examples/html/main.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ _examples/html/test.http | 13 +++++++++++ _examples/html/tpl.gohtml | 5 +++++ _examples/simple/test.http | 4 ++-- 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 _examples/html/main.go create mode 100644 _examples/html/test.http create mode 100644 _examples/html/tpl.gohtml (limited to '_examples') diff --git a/_examples/headers/test.http b/_examples/headers/test.http index 6efd342..0f3152e 100644 --- a/_examples/headers/test.http +++ b/_examples/headers/test.http @@ -1,5 +1,5 @@ ### Request JSON: -http://localhost:3000/hello +POST http://localhost:3000/hello Content-Type: application/json { @@ -12,7 +12,7 @@ Content-Type: application/json #{"message":"Hello, Alex!"} ### Request Form: -http://localhost:3000/hello +POST http://localhost:3000/hello Content-Type: application/x-www-form-urlencoded name=Alex diff --git a/_examples/html/main.go b/_examples/html/main.go new file mode 100644 index 0000000..abd0440 --- /dev/null +++ b/_examples/html/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "bytes" + "context" + "html/template" + "log" + "net/http" + + "github.com/gogeneric/api" +) + +var tpl *template.Template + +func main() { + h := &http.Server{Addr: "0.0.0.0:3000"} + mux := http.NewServeMux() + h.Handler = mux + var err error + tpl, err = template.ParseGlob("./*.gohtml") + if err != nil { + log.Fatalln(err) + } + // Here is magic! + mux.Handle("/hello", api.Wrap(handleHello)) + + if err := h.ListenAndServe(); err != http.ErrServerClosed { + log.Fatalln(err) + } +} + +func handleHello(ctx context.Context, req *helloRequest) (*helloResponse, error) { + return &helloResponse{ + Message: "Hello, " + req.Name, + template: "tpl.gohtml", + }, nil +} + +type helloRequest struct { + Name string `json:"name"` +} + +type helloResponse struct { + template string + Message string +} + +func (r *helloResponse) Render() ([]byte, error) { + buf := bytes.NewBuffer([]byte{}) + if err := tpl.ExecuteTemplate(buf, r.template, r); err != nil { + return nil, err + } + return buf.Bytes(), nil +} diff --git a/_examples/html/test.http b/_examples/html/test.http new file mode 100644 index 0000000..a20e65e --- /dev/null +++ b/_examples/html/test.http @@ -0,0 +1,13 @@ +### Request HTML: +POST http://localhost:3000/hello +Content-Type: application/json + +{ + "name": "Alex" +} + +### Response: +# http://localhost:3000/hello +# +#{"message":"Hello, Alex!"} + diff --git a/_examples/html/tpl.gohtml b/_examples/html/tpl.gohtml new file mode 100644 index 0000000..5cc65f4 --- /dev/null +++ b/_examples/html/tpl.gohtml @@ -0,0 +1,5 @@ + + +

Message: {{.Message}}

+ + \ No newline at end of file diff --git a/_examples/simple/test.http b/_examples/simple/test.http index 6efd342..0f3152e 100644 --- a/_examples/simple/test.http +++ b/_examples/simple/test.http @@ -1,5 +1,5 @@ ### Request JSON: -http://localhost:3000/hello +POST http://localhost:3000/hello Content-Type: application/json { @@ -12,7 +12,7 @@ Content-Type: application/json #{"message":"Hello, Alex!"} ### Request Form: -http://localhost:3000/hello +POST http://localhost:3000/hello Content-Type: application/x-www-form-urlencoded name=Alex -- cgit v1.2.3