static-files

command
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 7 Imported by: 0

README

Static files and an API route

This example serves a selected asset directory at / and keeps a normal JSON handler at /api/status. HyperServe opens the asset directory through os.Root; startup fails if the configured root is unavailable.

Run

The paths are relative to this example directory:

cd examples/static-files
go run .

Try the page, the API, and one response header:

curl http://localhost:8080/
curl http://localhost:8080/api/status
curl -I http://localhost:8080/index.html | grep X-Content-Type-Options

Server setup

app, err := hyperserve.New(hyperserve.WithStaticDir("./static"))
if err != nil {
    log.Fatal(err)
}

app.Use(hyperserve.HeadersMiddleware(app.Options()))

if err := app.HandleStatic("/"); err != nil {
    log.Fatal(err)
}

WithStaticDir selects the filesystem capability. HandleStatic decides where that capability appears in the URL space. The middleware is a separate request pipeline decision and therefore uses Use after construction.

The complete program also registers /api/status. Go's ServeMux selects that more-specific route before the / static fallback.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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