about summary refs log tree commit diff stats
path: root/http
diff options
context:
space:
mode:
authorMarkus Wüstenberg2024-06-26 10:32:46 +0200
committerGitHub2024-06-26 08:32:46 +0000
commit8b43a908820fb8c6b8a0f6d68d11bb2596a9e033 (patch)
treedfa2f4903ee64a225d35f5e7c7a3f0ba193a5bb7 /http
parenta79e6d9d10099c418846280ae4a4997e82f6efee (diff)
downloadgomponents-8b43a908820fb8c6b8a0f6d68d11bb2596a9e033.tar.lz
gomponents-8b43a908820fb8c6b8a0f6d68d11bb2596a9e033.tar.zst
gomponents-8b43a908820fb8c6b8a0f6d68d11bb2596a9e033.zip
Add links to godoc (#183)
Also add an example for `http.Adapt`.

Fixes #182
Diffstat (limited to 'http')
-rw-r--r--http/handler.go12
-rw-r--r--http/handler_test.go8
2 files changed, 14 insertions, 6 deletions
diff --git a/http/handler.go b/http/handler.go
index 9f47bff..c53655b 100644
--- a/http/handler.go
+++ b/http/handler.go
@@ -7,18 +7,18 @@ import (
 	g "github.com/maragudk/gomponents"
 )
 
-// Handler is like http.Handler but returns a Node and an error.
-// See Adapt for how errors are translated to HTTP responses.
+// Handler is like [http.Handler] but returns a [g.Node] and an error.
+// See [Adapt] for how errors are translated to HTTP responses.
 type Handler = func(http.ResponseWriter, *http.Request) (g.Node, error)
 
 type errorWithStatusCode interface {
 	StatusCode() int
 }
 
-// Adapt a Handler to a http.Handlerfunc.
-// The returned Node is rendered to the ResponseWriter, in both normal and error cases.
-// If the Handler returns an error, and it implements a "StatusCode() int" method, that HTTP status code is sent
-// in the response header. Otherwise, the status code http.StatusInternalServerError (500) is used.
+// Adapt a [Handler] to a [http.HandlerFunc].
+// The returned [g.Node] is rendered to the [http.ResponseWriter], in both normal and error cases.
+// If the [Handler] returns an error, and it implements a "StatusCode() int" method, that HTTP status code is sent
+// in the response header. Otherwise, the status code [http.StatusInternalServerError] (500) is used.
 func Adapt(h Handler) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 		n, err := h(w, r)
diff --git a/http/handler_test.go b/http/handler_test.go
index 822d69a..706e9eb 100644
--- a/http/handler_test.go
+++ b/http/handler_test.go
@@ -112,3 +112,11 @@ func get(t *testing.T, h http.Handler) (int, string) {
 	}
 	return result.StatusCode, string(body)
 }
+
+func ExampleAdapt() {
+	h := ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
+		return g.El("div"), nil
+	})
+	mux := http.NewServeMux()
+	mux.Handle("/", h)
+}