slot

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 7 Imported by: 1

README

Slot

Go Reference

Compose a response from nested HTTP handlers. A generic version of Svelte Slots. Used by mux.

Supports handlers running in series or concurrently (like Remix).

Install

go get -u github.com/livebud/slot

Example

See ExampleBatch in slot_test.go.

Contributors

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Batch

func Batch(handlers ...http.Handler) http.Handler
Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
	"strings"

	"github.com/livebud/slot"
)

func main() {
	view := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slots := slot.Open(w, r)
		w.Header().Set("Content-Type", "text/html")
		script := slots.Slot("script")
		script.WriteString(`<script src='/index.js'></script>`)
		slot, err := slots.ReadString()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		fmt.Fprintf(w, "<h1>%s</h1>", slot)
	})
	frame := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slots := slot.Open(w, r)
		slot, err := slots.ReadString()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		slots.Slot("style").WriteString(`<link href='/frame.css'/>`)
		fmt.Fprintf(w, "<main>\n\t\t\t%s\n\t\t</main>", slot)
	})
	layout := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slots := slot.Open(w, r)
		slot, err := slots.ReadString()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		script, err := slots.Slot("script").ReadString()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		style, err := slots.Slot("style").ReadString()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		fmt.Fprintf(w, "<html>\n\t<head>\n\t\t%s\n\t\t%s\n\t</head>\n\t<body>\n\t\t%s\n\t</body>\n</html>", script, style, slot)
	})
	handler := slot.Batch(view, frame, layout)
	req := httptest.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	handler.ServeHTTP(rec, req)
	res := rec.Result()
	response, err := httputil.DumpResponse(res, true)
	if err != nil {
		panic(err)
	}
	fmt.Println(strings.ReplaceAll(string(response), "\r\n", "\n"))
}
Output:

HTTP/1.1 200 OK
Connection: close
Content-Type: text/html

<html>
	<head>
		<script src='/index.js'></script>
		<link href='/frame.css'/>
	</head>
	<body>
		<main>
			<h1></h1>
		</main>
	</body>
</html>

func Chain

func Chain(handlers ...http.Handler) http.Handler

func Open

func Open(w http.ResponseWriter, r *http.Request) *slots

Open slots for reading and writing

Types

type Mock

type Mock struct {
	Data  string            // Slot data
	Named map[string]string // Named slots
}

Mock slots

func (*Mock) ReadString

func (m *Mock) ReadString() (string, error)

func (*Mock) Slot

func (m *Mock) Slot(slot string) NamedSlot

type NamedSlot

type NamedSlot interface {
	ReadString() (string, error)
	WriteString(string) error
}

NamedSlot is a single read-writable slot

type Slots

type Slots interface {
	ReadString() (string, error)
	Slot(slot string) NamedSlot
}

Slots contains a single readable default slot and a map of named slots

func Empty

func Empty() Slots

Empty slots

Jump to

Keyboard shortcuts

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