aboutsummaryrefslogtreecommitdiff
path: root/_examples
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-09 08:03:03 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-09 08:03:03 +0300
commit6d99712a301a6225366c1f92cdaeb8b2e680120d (patch)
tree7ea101fc4d7e34235e45925c7c291ba35d983aea /_examples
parentb3c48f806724b82b649c89aa5e0d07a09e414140 (diff)
Added custom renderers
Diffstat (limited to '_examples')
-rw-r--r--_examples/headers/test.http4
-rw-r--r--_examples/html/main.go54
-rw-r--r--_examples/html/test.http13
-rw-r--r--_examples/html/tpl.gohtml5
-rw-r--r--_examples/simple/test.http4
5 files changed, 76 insertions, 4 deletions
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 @@
+<html>
+<body>
+<h1>Message: {{.Message}}</h1>
+</body>
+</html> \ 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