http

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package http provides adapters to render gomponents in http handlers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

func Adapt(h Handler) http.HandlerFunc

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.

Example
package main

import (
	"net/http"

	g "github.com/maragudk/gomponents"
	ghttp "github.com/maragudk/gomponents/http"
)

func main() {
	h := ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
		return g.El("div"), nil
	})
	mux := http.NewServeMux()
	mux.Handle("/", h)
}
Output:

Types

type Handler

type Handler = func(http.ResponseWriter, *http.Request) (g.Node, error)

Handler is like http.Handler but returns a g.Node and an error. See Adapt for how errors are translated to HTTP responses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL