bungo

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 13 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ¶

BunGo logo

BunGo (Bundler 4 Go)

BunGo is a high-performance, fullstack, and idiomatic Go web framework designed to seamlessly bridge backend Go logic with frontend React (JSX) views. It eliminates the need for Node.js, npm, package.json, and complex external build pipelines by embedding everything you need right into your Go binary.

If you love the simplicity of Go backends but want the rich interactivity of React without the headache of managing a separate frontend ecosystem, BunGo is the framework for you.


📚 Documentation

For complete guides, usage patterns, and best practices, please refer to our new docs directory:

  1. Introduction to BunGo
  2. Installation & Setup
  3. Routing and Pages
  4. Templates and Layouts
  5. React Integration
  6. Security Layers
  7. CLI and Dev Tools
  8. Deployment Options
  9. AI-Assisted Development Guide

📦 Quick Start & Installation

You can instantly scaffold a fresh BunGo application using our dedicated CLI tool.

Important Note: To use the bungo CLI, your Go bin directory must be in your system's PATH. If you encounter a "command not found" error after installation, add $HOME/go/bin to your PATH (e.g., export PATH=$PATH:$HOME/go/bin in your .zshrc or .bashrc). For specific OS instructions, see the Installation Guide.

  1. Install the CLI once:

    go install github.com/piotr-nierobisz/BunGo/cmd/bungo@latest
    
  2. Scaffold a project and run the live-reload dev server:

    bungo init my-project
    cd my-project
    go mod tidy
    
    # Starts your server and continuously hot-reloads on changes!
    bungo dev
    
    # Builds a portable production binary in ./bin/
    bungo build
    

(For a native TypeScript integration, add --typescript to the init command).

For manual project setups without the CLI, refer to the Installation Guide.

Documentation ¶

Index ¶

Constants ¶

View Source
const DevWebSocketPort = 35729

DevWebSocketPort is the fixed port the bungo CLI listens on for live reload; must match injected client.

Variables ¶

This section is empty.

Functions ¶

func OptimizedAssetPath ¶ added in v0.3.0

func OptimizedAssetPath(view string) string

OptimizedAssetPath converts a route view path into the optimized `/_bungo/*.js` route. Inputs: - view: page route view path relative to `views/`. Outputs: - string: optimized JavaScript asset route path.

func RegisterEmbeddedAssetsFS ¶ added in v0.3.0

func RegisterEmbeddedAssetsFS(assets fs.FS)

RegisterEmbeddedAssetsFS registers a process-wide embedded asset filesystem used by BunGo servers. Inputs: - assets: filesystem containing project files generated by `bungo build` embedding. Outputs: - none

func RenderTemplate ¶

func RenderTemplate(
	assetStorage *AssetStorage,
	templatePath string,
	layoutPath string,
	inlineJS string,
	moduleSrc string,
	data map[string]any,
) (string, error)

RenderTemplate renders a page template, optionally composed inside a layout template. Inputs: - assetStorage: server asset storage used to resolve template/layout files. - templatePath: required web-root-relative path to the page-specific .gohtml template file. - layoutPath: optional web-root-relative wrapper .gohtml path defining {{block "content" .}}. - inlineJS: compiled JavaScript source to inject inline as a module script. - moduleSrc: optional static module URL injected when asset optimization is enabled. - data: template data passed to Execute after BunGo script fields are merged in. Outputs: - string: fully rendered HTML with BunGo data/scripts injected into template output. - error: non-nil when template files cannot be read, parsed, or executed.

Types ¶

type APIResponse ¶

type APIResponse struct {
	StatusCode int
	Body       any
}

APIResponse represents an API response

type ApiRoute ¶

type ApiRoute struct {
	Path          string
	Version       string
	Method        string
	SecurityLayer []string
	Handler       func(req *Request) (APIResponse, error)
}

ApiRoute represents a configuration for an API route.

Method must be a standard HTTP verb (GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS, TRACE). Registration normalizes it to uppercase; invalid or empty values panic at Api registration time.

type AssetStorage ¶ added in v0.3.0

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

AssetStorage provides a unified web asset reader with memory-first and disk-fallback lookups.

func (*AssetStorage) Exists ¶ added in v0.3.0

func (s *AssetStorage) Exists(relativePath string) bool

Exists reports whether a relative web asset path exists in memory or on disk. Inputs: - relativePath: slash-style path relative to web root, or empty string for the web root itself. Outputs: - bool: true when the asset exists in embedded memory or disk storage.

func (*AssetStorage) PrepareWebDirForBuild ¶ added in v0.3.0

func (s *AssetStorage) PrepareWebDirForBuild() (string, func(), error)

PrepareWebDirForBuild returns a disk directory suitable for esbuild and a cleanup callback. Inputs: - none Outputs: - string: filesystem directory containing a materialized `layouts`, `views`, and optional `static`. - func(): cleanup callback that removes temporary materialized assets when invoked. - error: non-nil when temporary directory creation or embedded asset extraction fails.

func (*AssetStorage) ReadFile ¶ added in v0.3.0

func (s *AssetStorage) ReadFile(relativePath string) ([]byte, error)

ReadFile reads one relative web asset path preferring memory and then falling back to disk. Inputs: - relativePath: slash-style path relative to web root. Outputs: - []byte: file contents for the resolved asset path. - error: non-nil when the asset is invalid or missing in both memory and disk stores.

func (*AssetStorage) ReadStaticFile ¶ added in v0.3.0

func (s *AssetStorage) ReadStaticFile(requestPath string) ([]byte, error)

ReadStaticFile reads a static asset by URL-relative path and enforces path traversal safety. Inputs: - requestPath: URL-relative path under `/static/`, for example `css/app.css`. Outputs: - []byte: resolved static file bytes from memory-first, disk-fallback storage. - error: non-nil when requestPath is invalid or the static file is not found.

type Engine ¶

type Engine interface {
	Start(address string, srv *Server) error
}

Invoker represents the execution environment starting the server. We define it locally to avoid import cycle, since engine pkg depends on bungo

type PageRoute ¶

type PageRoute struct {
	Path          string
	Template      string
	Layout        string
	View          string
	SecurityLayer []string
	Handler       func(req *Request) (map[string]any, error)
}

PageRoute configures a single page route.

Template is required: the page-specific .gohtml file in layouts/ that holds the page content (and optionally {{define "content"}} when using a Layout).

Layout is optional: a wrapper .gohtml in layouts/ that defines {{block "content" .}}. When set, the Template is rendered inside that block, so you avoid repeating <html>, <head>, and <body> in every template. If empty, the Template is rendered as a standalone page.

View is optional: the corresponding .jsx/.tsx (or .js/.ts) entry in views/ to be compiled and injected into the page as a module script.

type Request ¶

type Request struct {
	Context  context.Context
	Headers  map[string]string
	Params   map[string]string // URL and Query parameters
	Body     []byte
	Internal map[string]any // For passing context between Security Layers and Handlers
}

Request represents a framework-agnostic HTTP request

type SecurityLayer ¶

type SecurityLayer struct {
	Name    string
	Handler func(req *Request) bool
}

SecurityLayer represents a reusable security layer that can be applied to routes.

type Server ¶

type Server struct {
	Pages          map[string]PageRoute
	APIs           map[string]ApiRoute
	SecurityLayers map[string]SecurityLayer
	Engine         Engine
	WebDir         string
	DefaultLayout  string
	// contains filtered or unexported fields
}

Server is the central registry for the BunGo application.

func NewServer ¶

func NewServer(engine Engine, webDir string) *Server

NewServer creates a Server and validates required web directory structure at startup. Inputs: - engine: runtime engine implementation responsible for serving incoming requests. - webDir: base web directory containing required layouts/ and views/ subdirectories. Outputs: - *Server: initialized server registry with empty route and security maps.

func (*Server) Api ¶

func (s *Server) Api(route ApiRoute)

Api registers an API route in the server route registry. Inputs: - route: API route configuration keyed by version, method, and path. Outputs: - none

func (*Server) AssetOptimizationEnabled ¶ added in v0.2.0

func (s *Server) AssetOptimizationEnabled() bool

AssetOptimizationEnabled reports whether static module bundle delivery is enabled. Inputs: - none Outputs: - bool: true when SetAssetOptimization enabled external /_bungo bundle serving.

func (*Server) AssetStorage ¶ added in v0.3.0

func (s *Server) AssetStorage() *AssetStorage

AssetStorage returns the server storage abstraction for memory-first and disk-fallback web asset access. Inputs: - none Outputs: - *AssetStorage: server asset storage used by engines, template rendering, and builders.

func (*Server) Page ¶

func (s *Server) Page(route PageRoute)

Page registers a page route and validates referenced template, layout, and view files. Inputs: - route: page route configuration to store in the server route registry. Outputs: - none

func (*Server) ResolvePageScriptAssets ¶ added in v0.3.0

func (s *Server) ResolvePageScriptAssets(route *PageRoute, compiledViews map[string]string) (string, string)

ResolvePageScriptAssets resolves inline/module script values for one page route render. Inputs: - route: page route whose optional view determines script asset injection values. - compiledViews: map of compiled view source keyed by original route View value. Outputs: - string: inline JavaScript payload when asset optimization is disabled. - string: module source URL when asset optimization is enabled.

func (*Server) ResolvePageTemplatePaths ¶ added in v0.3.0

func (s *Server) ResolvePageTemplatePaths(route *PageRoute) (string, string)

ResolvePageTemplatePaths returns template and layout asset paths relative to the configured web root. Inputs: - route: page route whose template and optional layout should be resolved. Outputs: - string: required template asset path relative to web root. - string: optional layout asset path relative to web root, or empty when no layout applies.

func (*Server) Security ¶

func (s *Server) Security(layer SecurityLayer)

Security registers a named security layer in the server security registry. Inputs: - layer: reusable security layer definition with name and handler function. Outputs: - none

func (*Server) Serve ¶

func (s *Server) Serve(port int) error

Serve starts server execution on the provided port using the configured engine. Inputs: - port: TCP port number used to build the engine listen address. Outputs: - error: non-nil when the engine fails to start or returns a runtime error.

func (*Server) SetAssetOptimization ¶ added in v0.2.0

func (s *Server) SetAssetOptimization(enabled bool)

SetAssetOptimization toggles static module delivery for compiled page view bundles. Inputs: - enabled: true to serve view bundles via /_bungo/*.js, false to inline module code. Outputs: - none

func (*Server) SetDefaultLayout ¶

func (s *Server) SetDefaultLayout(path string)

SetDefaultLayout sets the default layout file used when a page route omits Layout. Inputs: - path: layout filename in webDir/layouts, or empty string to clear the default. Outputs: - none

Directories ¶

Path Synopsis
cmd
bungo command
aws module
gcp module
internal
dev

Jump to

Keyboard shortcuts

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