static/example_test.go

36 lines
756 B
Go
Raw Permalink Normal View History

2025-01-12 11:01:29 +00:00
package static_test
import (
"embed"
"fmt"
"io"
"net/http"
"net/http/httptest"
"git.bivouac.wiki/use/static"
)
//go:embed example_static
var static_files embed.FS
func ExampleStatic() {
s := static.NewStatic("example_static/", static_files)
mux := http.NewServeMux()
// for files outside a static directory:
mux.HandleFunc("/robots.txt", s.WrapStatic("/robots.txt", "robots.txt"))
// for an entire subdirectory:
mux.HandleFunc("/static/{filename}", s.StaticRoute)
w := httptest.NewRecorder()
ts := httptest.NewServer(mux)
ts.URL = "robots.txt"
ts.Start()
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
fmt.Errorf("expected error to be nil got %v", err)
}
fmt.Println(string(data))
}