README
¶
Buffkit Examples
This directory contains a complete example application demonstrating all Buffkit features.
Quick Start
# From the buffkit root directory
make run
# Or directly
cd examples
go run main.go
The application will start at http://localhost:3000
Features Demonstrated
This example application shows how to use:
1. Authentication
- Login form at
/login - Protected routes under
/protected/* - Test credentials:
test@example.com/password - Session-based authentication with encrypted cookies
2. Server-Sent Events (SSE)
- Real-time updates at
/sse-demo - Broadcasting messages to all connected clients
- Automatic reconnection and heartbeats
3. Server-Side Components
- Component showcase at
/components - Examples of all built-in components:
<bk-button>- Styled buttons with variants<bk-card>- Card containers with slots<bk-dropdown>- Dropdown menus<bk-alert>- Alert messages<bk-form>- Forms with CSRF protection<bk-input>- Form inputs with labels
4. Mail Preview (Development)
- View sent emails at
/__mail/preview - Only available when DevMode is true
5. Security Features
- Automatic security headers on all responses
- CSRF protection on forms
- Content Security Policy configured
Project Structure
examples/
├── main.go # Application setup and handlers
└── templates/ # Template files (currently using inline HTML)
└── .keep # Placeholder for embed.FS
Configuration
The example uses an in-memory SQLite database and includes these configurations:
- DevMode: Enabled for development features
- AuthSecret: Test secret (change in production!)
- RedisURL: Optional - jobs work without it
- SMTPAddr: Optional - emails are logged if not configured
- Dialect: SQLite for simplicity
Running with Services
For the full experience with Redis and mail catching:
# Start Redis and MailHog
make docker-services
# Then run the app
make run
- Redis will be available at
localhost:6379 - MailHog SMTP at
localhost:1025 - MailHog UI at
http://localhost:8025
API Endpoints
Public Routes
GET /- Home pageGET /about- About pageGET /components- Component showcaseGET /sse-demo- SSE demonstrationPOST /broadcast- Send SSE broadcastGET /login- Login formPOST /login- Process loginPOST /logout- Logout
Protected Routes
GET /protected/dashboard- User dashboard (requires login)GET /protected/profile- User profile (requires login)
API Routes
GET /api/status- System status JSON
System Routes
GET /events- SSE endpointGET /__mail/preview- Mail preview (dev only)
Customization
This example is designed to be modified. Try:
- Adding New Components: Register custom components in the app setup
- Creating New Protected Routes: Use
buffkit.RequireLoginmiddleware - Broadcasting Custom Events: Use
kit.Broker.Broadcast() - Sending Emails: Use
kit.Mail.Send() - Enqueueing Jobs: Use
kit.Jobs.Enqueue()
Testing Authentication
- Navigate to any protected route (e.g.,
/protected/dashboard) - You'll be redirected to
/login - Enter test credentials:
test@example.com/password - After login, you'll be redirected back
- Click logout to end the session
Testing SSE
- Open
/sse-demoin multiple browser tabs - Send a broadcast message from one tab
- All tabs receive the update instantly
- Check browser console for SSE events
Troubleshooting
App won't start
- Check port 3000 is not in use
- Ensure Go 1.21+ is installed
Login doesn't work
- The test user is created in memory on startup
- Check the console for any database errors
SSE not connecting
- Check browser console for errors
- Ensure
/eventsendpoint is accessible - Some proxies may interfere with SSE
Components not rendering
- View page source to see if
<bk-*>tags are present - Check console for expansion errors
Production Considerations
This example is for demonstration. For production:
- Use a real database (PostgreSQL, MySQL)
- Set a secure
AuthSecretfrom environment - Configure proper SMTP settings
- Set up Redis for job processing
- Disable
DevMode - Use proper templates instead of inline HTML
- Add rate limiting and monitoring
- Configure proper CSP headers
Learning Resources
- See
HOW_IT_WORKS.mdfor architecture details - Check individual package documentation for API details
- Review
PLAN.mdfor the design philosophy
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.