aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-12-19 20:14:07 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-12-19 20:14:07 +0300
commitd36bd3f0aa6ee962cc1a0d40bfb6a428da4ff93b (patch)
tree8fd9d9ea4f45872cb15fa20f034e0885ede96bc0 /README.md
parent6f5bb85330acd4227d4c233e283f384e6ff834c8 (diff)
Fix readme and example
Diffstat (limited to 'README.md')
-rw-r--r--README.md52
1 files changed, 51 insertions, 1 deletions
diff --git a/README.md b/README.md
index ba31628..7540e8e 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,52 @@
-# api
+# API
Generic api functions
+
+## Usage
+
+### api.Wrap(handler)
+
+Function Wrap wraps API handler and returns standard http.HandlerFunc. It encapsulate body parsing.
+
+#### Example
+
+```go
+package main
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "net/http"
+
+ "github.com/gogeneric/api"
+)
+
+func main() {
+ h := &http.Server{Addr: "0.0.0.0:3000"}
+ mux := http.NewServeMux()
+ h.Handler = mux
+
+ // Here is magic!
+ mux.Handle("/hello", api.Wrap(handleHello))
+
+ if err := h.ListenAndServe(); err != http.ErrServerClosed {
+ log.Fatalln(err)
+ }
+}
+
+// Our API handler with custom request and response types
+func handleHello(ctx context.Context, req *helloRequest) (*helloResponse, error) {
+ return &helloResponse{Message: fmt.Sprintf("Hello, %s!", req.Name)}, nil
+}
+
+// Custom request type
+type helloRequest struct {
+ Name string `json:"name"`
+}
+
+// Custom response type
+type helloResponse struct {
+ Message string `json:"message"`
+}
+
+``` \ No newline at end of file