static

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

README

http/static

http/static serves project-relative static directories and SPA assets on top of chi. Import path: github.com/Ithildur/EiluneKit/http/static; package name: static.

Quick Start

handler, err := static.SPAHandler("dist", static.Options{
	AppDir: appdir.Options{EnvVar: "APP_HOME"},
})
if err != nil {
	return err
}

r.Handle("/*", handler)

For explicit mounting:

if _, err := static.MountSPA(r, "/app", "dist", static.Options{
	Development: true,
}); err != nil {
	return err
}

Resolution Rules

  • relPath must be a clean project-relative path such as dist or web/dist
  • absolute paths, . and .., duplicate separators, and dirty paths are rejected with ErrInvalidProjectPath
  • SPA resolution requires index.html
  • when Options.Development is false, discovery uses app-home env/executable sources only
  • when Options.Development is true, discovery also allows the working directory

Options

  • AppDir: passed through to appdir.DiscoverHome
  • Development: enables working-directory fallback for local development

Contracts

  • SPAHandler, MountSPA, Mount, ResolveDir, and ResolveSPADir each take one Options struct; use static.Options{} for defaults
  • when Options.AppDir.Markers is empty, the package derives markers from relPath
  • invalid app-home env overrides fail fast with appdir.ErrEnvInvalid; they do not fall back to the working directory

Documentation

Overview

Package static provides static file and SPA helpers. Package static 提供静态文件与 SPA 辅助函数。

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidProjectPath = errors.New("static: invalid project path")

Functions

func Mount

func Mount(router chi.Router, prefix string, relPath string, opts Options) error

Mount mounts a file server at prefix. Call Mount(router, prefix, relPath, opts). Mount 在 prefix 挂载文件服务器。 调用 Mount(router, prefix, relPath, opts)。

func MountSPA

func MountSPA(router chi.Router, prefix string, relPath string, opts Options) (http.Handler, error)

MountSPA mounts a SPA handler at prefix. Call MountSPA(router, prefix, relPath, opts). MountSPA 在 prefix 挂载 SPA handler。 调用 MountSPA(router, prefix, relPath, opts)。

func ResolveDir

func ResolveDir(relPath string, opts Options) (string, error)

ResolveDir returns the directory for relPath. relPath must be project-relative. ResolveDir 返回 relPath 对应目录。 relPath 必须是项目相对路径。

func ResolveSPADir

func ResolveSPADir(relPath string, opts Options) (string, error)

ResolveSPADir returns the SPA directory for relPath. index.html must exist. ResolveSPADir 返回 relPath 对应的 SPA 目录。 必须存在 index.html。

func SPAHandler

func SPAHandler(relPath string, opts Options) (http.Handler, error)

SPAHandler returns a handler with SPA history fallback. relPath must be project-relative, for example "dist" or "web/dist". Call handler, err := SPAHandler(relPath, opts). SPAHandler 返回带 SPA history 回退的 handler。 relPath 必须是项目相对路径,例如 "dist" 或 "web/dist"。 调用 handler, err := SPAHandler(relPath, opts)。 Example / 示例:

handler, _ := static.SPAHandler("dist", static.Options{})
r.Handle("/*", handler)

Types

type Options

type Options struct {
	AppDir      appdir.Options
	Development bool
}

Options configures static asset discovery. Options 配置静态资源发现。

Jump to

Keyboard shortcuts

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