frontend

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package frontend provides helpers for serving Vite-built frontend assets embedded in the Go binary via go:embed, plus a development proxy for hot-module reload during development.

It also contains two TypeScript code generators:

  • GenerateTypes emits TS interfaces for Go structs with json tags.
  • GenerateClient emits a typed, dependency-free fetch client from the router's named routes (see the documentation in client.go), reusing the same Go→TS type mapping.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateClient

func GenerateClient(routes []*routing.Route, outFile string, opts ...ClientOption) error

GenerateClient generates a typed, dependency-free TypeScript fetch client for the given routes (typically router.Routes()) and writes it to outFile. See the package documentation above for the emission rules.

func GenerateTypes

func GenerateTypes(srcDir, outFile string) error

GenerateTypes generates TypeScript type declarations for Go structs annotated with `json` tags and writes the output to a .ts file. This is a lightweight, reflection-free source code generator.

frontend.GenerateTypes("app/models", "resources/ts/types.generated.ts")

Types

type ClientOption

type ClientOption func(*clientConfig)

ClientOption configures GenerateClient.

func WithRouteTypes

func WithRouteTypes(routeName, requestGoType, responseGoType string) ClientOption

WithRouteTypes attaches request/response types to the named route. requestGoType and responseGoType are Go type expressions ("CreateUserRequest", "[]User", "map[string]bool", ...) converted with the same Go→TS mapping used by GenerateTypes. Pass "" to leave either side as `unknown`.

func WithTypesImport

func WithTypesImport(module string) ClientOption

WithTypesImport sets the module specifier the generated client imports named interface types from, e.g. WithTypesImport("./types.generated"). Only the named (non-primitive) types actually referenced by WithRouteTypes are imported. Without this option, referenced named types are assumed to be declared elsewhere in the consuming project.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles serving frontend assets in both dev and production modes.

func New

func New(mode string, opts ...Option) *Manager

New creates a frontend Manager.

  • In dev mode, requests are proxied to the Vite dev server.
  • In production mode, assets are served from the embedded pubFS.

func (*Manager) Handler

func (m *Manager) Handler() http.Handler

Handler returns an http.Handler that serves frontend assets. In dev mode it proxies to Vite; in prod it serves from the embedded FS.

func (*Manager) LoadManifest

func (m *Manager) LoadManifest(path string) error

LoadManifest parses the Vite manifest.json from the given path. Call this during app startup in production.

func (*Manager) ViteTag

func (m *Manager) ViteTag(entry string) string

ViteTag returns the HTML <script> and <link> tags for the given entry point. In dev mode it points to Vite's dev server; in prod it uses the manifest.

{{.Frontend.ViteTag "resources/ts/app.ts"}}

type Option

type Option func(*Manager)

Option configures the Manager.

func WithDevURL

func WithDevURL(u string) Option

WithDevURL sets the Vite dev server URL (default: http://localhost:5173).

func WithFS

func WithFS(fsys fs.FS) Option

WithFS sets the embedded fs.FS for production assets. Typically this is the go:embed public/build directory.

Jump to

Keyboard shortcuts

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