Documentation
¶
Overview ¶
Package buffkit provides an opinionated SSR-first stack for Buffalo applications. It brings Rails-like batteries to server-rendered Go applications without the bloat.
Buffkit is designed around several core principles:
- Server-side rendering first (HTML rendered on server, htmx for interactions)
- Zero JavaScript bundler (uses import maps instead)
- Batteries included (auth, jobs, mail, components out of the box)
- Database agnostic (uses database/sql, no ORM lock-in)
- Everything is shadowable (your app can override any template or asset)
The main entry point is the Wire() function which installs all Buffkit packages into your Buffalo application with a single call.
Index ¶
- func NewMigrationRunner(db *sql.DB, migrationFS embed.FS, dialect string) *migrations.Runner
- func RenderPartial(c buffalo.Context, name string, data map[string]interface{}) ([]byte, error)
- func RequireLogin(next buffalo.Handler) buffalo.Handler
- func SetGlobalKit(kit *Kit)
- func Version() string
- type Config
- type Kit
- type MigrationRunner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMigrationRunner ¶
NewMigrationRunner creates a new migration runner. It uses the new migrations package implementation.
func RenderPartial ¶
RenderPartial renders a partial template with data. This is a helper for rendering fragments that can be used for both htmx responses AND SSE broadcasts - ensuring single source of truth for HTML fragments:
html, err := buffkit.RenderPartial(c, "partials/item", map[string]interface{}{
"item": item,
})
// Send as htmx response
c.Render(200, r.HTML(html))
// AND broadcast via SSE
kit.Broker.Broadcast("item-update", html)
func RequireLogin ¶
RequireLogin is middleware that ensures user is authenticated. Add this to routes or groups that need protection:
app.GET("/admin", buffkit.RequireLogin(AdminHandler))
// or
protected := app.Group("/admin")
protected.Use(buffkit.RequireLogin)
If the user is not logged in, they are redirected to /login. If authenticated, the user object is added to context as "current_user".
func SetGlobalKit ¶
func SetGlobalKit(kit *Kit)
SetGlobalKit sets the global Kit instance for Grift tasks This is called automatically by Wire()
Types ¶
type Config ¶
type Config struct {
// DevMode enables development features like mail preview at /__mail/preview
// and relaxes certain security restrictions. Should be false in production.
DevMode bool
// AuthSecret is used for session encryption. This MUST be set to a secure
// random value in production. The session cookies are encrypted with this key.
// Required field - Wire() will error if not provided.
AuthSecret []byte
// RedisURL for background job processing via Asynq. If empty, job enqueuing
// becomes a no-op (useful for development without Redis). Format:
// "redis://username:password@localhost:6379/0"
RedisURL string
// SMTP configuration for mail sending. If SMTPAddr is empty, a development
// mail sender is used that logs emails instead of sending them.
SMTPAddr string // Host:port (e.g., "smtp.sendgrid.net:587")
SMTPUser string // SMTP username for authentication
SMTPPass string // SMTP password for authentication
// Database dialect: "postgres" | "sqlite" | "mysql"
// This is used for dialect-specific SQL in migrations and stores.
Dialect string
// Optional database connection. If not provided, Buffkit will attempt to
// connect using the DATABASE_URL environment variable. This allows you to
// either manage the connection yourself or let Buffkit handle it.
DB *sql.DB
}
Config holds all configuration for Buffkit packages. This is the main configuration struct that controls how Buffkit behaves. Each field maps to a specific subsystem's configuration needs.
type Kit ¶
type Kit struct {
// SSR broker for server-sent events. Use this to broadcast real-time
// updates to connected clients: kit.Broker.Broadcast("event", htmlBytes)
Broker *ssr.Broker
// Jobs runtime for background processing. Access the Asynq client to
// enqueue jobs: kit.Jobs.Client.Enqueue(task)
Jobs *jobs.Runtime
// Mail sender interface. Can be used directly to send emails:
// kit.Mail.Send(ctx, message)
Mail mail.Sender
// Auth store for user management. Useful if you need to directly
// query users: kit.AuthStore.ByEmail(ctx, email)
AuthStore auth.UserStore
// Import map manager for JavaScript dependencies. Can be used to
// dynamically add pins: kit.ImportMap.Pin("name", "url")
ImportMap *importmap.Manager
// Component registry for server-side components. Register custom
// components: kit.Components.Register("my-component", renderer)
Components *components.Registry
// Configuration that was used to initialize Buffkit. Useful for
// checking settings at runtime.
Config Config
}
Kit holds references to all Buffkit subsystems after wiring. This is returned from Wire() and provides access to all the initialized components. You can use these references to interact with Buffkit systems directly when needed (e.g., broadcasting SSE events, enqueuing jobs).
func Wire ¶
Wire installs all Buffkit packages into a Buffalo application. This is the main integration point - call this once in your app.go:
app := buffalo.New(buffalo.Options{...})
kit, err := buffkit.Wire(app, buffkit.Config{
DevMode: ENV == "development",
AuthSecret: []byte(envy.Get("SESSION_SECRET", "change-me")),
RedisURL: envy.Get("REDIS_URL", ""),
})
Wire performs the following setup:
- Validates configuration (ensures required fields are set)
- Initializes SSR broker and mounts /events endpoint
- Sets up authentication with login/logout routes
- Configures background job processing (if Redis available)
- Initializes mail sending (SMTP or dev mode)
- Sets up import maps with default JavaScript libraries
- Adds security middleware to the request chain
- Registers default server-side components
- Adds helper functions to Buffalo context
The order of initialization matters as some systems depend on others. Wire handles this ordering correctly.
type MigrationRunner ¶
type MigrationRunner struct {
// Database connection to run migrations against
DB *sql.DB
// Dialect for database-specific SQL ("postgres", "sqlite", "mysql")
Dialect string
// Table name for tracking migrations (defaults to "buffkit_migrations")
Table string
}
MigrationRunner handles database migrations for Buffkit. It manages the buffkit_migrations table that tracks which migrations have been applied. Migrations are simple SQL files that are run in lexical order.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
grift
command
|
|
|
Package components provides server-side custom elements for Buffalo applications.
|
Package components provides server-side custom elements for Buffalo applications. |
|
Package ssr provides server-sent events (SSE) functionality for real-time updates.
|
Package ssr provides server-sent events (SSE) functionality for real-time updates. |