Prefab Development Guide
Prefab is a Go server framework that simplifies building production-ready gRPC and HTTP services with a plugin-based architecture.
Core Concepts
- Server: Central component that manages gRPC/HTTP services and plugins
- Plugins: Modular components for auth, storage, templates, etc.
- Services: gRPC service implementations with automatic HTTP gateway
- Handlers: Custom HTTP handlers for non-gRPC endpoints
Prefab servers commonly compose multiple gRPC services into a single process. This enables a service-oriented monolith architecture where related services share infrastructure (auth, storage, logging) while maintaining clear boundaries. As utilization requirements become known, individual services can be extracted into separate deployments without changing their interfaces.
Quick Start Pattern
import "github.com/dpup/prefab"
func main() {
s := prefab.New(
prefab.WithPort(8080),
prefab.WithPlugin(auth.Plugin()),
// Add more options...
)
s.RegisterService(
&myservice.MyService_ServiceDesc,
myservice.RegisterMyServiceHandler,
&myServiceImpl{},
)
if err := s.Start(); err != nil {
log.Fatal(err)
}
}
Resources
Load these resources based on the specific task:
Server & Services
- Project Setup - Proto files, Makefile, project structure
- Server Setup - Server creation, initialization, and basic configuration
- gRPC & HTTP - Registering services, HTTP handlers, static files
Authentication & Authorization
- Authentication - OAuth, password auth, magic links, fake auth for testing
- OAuth Server - Build an OAuth2 authorization server for third-party apps
- Authorization - Declarative access control with proto annotations, policies, role describers
Features
- SSE Streaming - Server-Sent Events for real-time updates
- Configuration - YAML, environment variables, functional options
- Storage - Storage plugins (memory, SQLite)
- File Uploads - File upload/download with authorization
- Email - SMTP email sending
- Templates - Go HTML template rendering
- Event Bus - Publish/subscribe inter-plugin communication
Development
- Custom Plugins - Creating plugins with dependencies and lifecycle
- Error Handling - Stack traces, gRPC codes, log fields
- Logging - Structured context-aware logging
- Security - CSRF, headers, HTTPS, token security
Code Style
- Use
errors.New(), errors.NewC(), errors.Wrap() for errors with stack traces
- Standard library imports first, then third-party
- Document public APIs with GoDoc comments
- Follow plugin interface patterns with
Plugin() function and PluginName constant
When to Load Resources
| Task |
Resources to Load |
| Starting a new project |
project-setup.md, server-setup.md |
| Creating a new server |
server-setup.md, configuration.md, logging.md |
| Adding authentication |
auth.md, server-setup.md |
| Building an OAuth server |
oauth-server.md, auth.md, storage.md |
| Setting up access control |
authz.md, auth.md |
| Adding real-time features |
sse.md |
| Creating a custom plugin |
plugins.md, eventbus.md |
| Handling errors properly |
errors.md, logging.md |
| Security review |
security.md |
| Adding storage |
storage.md |
| Adding HTTP/gRPC endpoints |
grpc-http.md |
| Adding file uploads |
uploads.md, authz.md |
| Sending emails |
email.md, templates.md |
| Rendering templates |
templates.md |
| Inter-plugin communication |
eventbus.md |
| Setting up logging |
logging.md |
1---2name: prefab-dev3description: Use this skill when developing Go applications with the Prefab server framework. This includes creating servers, adding gRPC/HTTP handlers, configuring authentication and authorization, setting up SSE streams, managing configuration, creating custom plugins, and following Prefab error handling and security patterns.4---5
6# Prefab Development Guide
7
8Prefab is a Go server framework that simplifies building production-ready gRPC and HTTP services with a plugin-based architecture.
9
10## Core Concepts
11
12- **Server**: Central component that manages gRPC/HTTP services and plugins
13- **Plugins**: Modular components for auth, storage, templates, etc.
14- **Services**: gRPC service implementations with automatic HTTP gateway
15- **Handlers**: Custom HTTP handlers for non-gRPC endpoints
16
17Prefab servers commonly compose multiple gRPC services into a single process. This enables a service-oriented monolith architecture where related services share infrastructure (auth, storage, logging) while maintaining clear boundaries. As utilization requirements become known, individual services can be extracted into separate deployments without changing their interfaces.
18
19## Quick Start Pattern
20
21```go
22import "github.com/dpup/prefab"
23
24func main() {
25 s := prefab.New(
26 prefab.WithPort(8080),
27 prefab.WithPlugin(auth.Plugin()),
28 // Add more options...
29 )
30
31 s.RegisterService(
32 &myservice.MyService_ServiceDesc,
33 myservice.RegisterMyServiceHandler,
34 &myServiceImpl{},
35 )
36
37 if err := s.Start(); err != nil {
38 log.Fatal(err)
39 }
40}
41```
42
43## Resources
44
45Load these resources based on the specific task:
46
47### Server & Services
48- **[Project Setup](resources/project-setup.md)** - Proto files, Makefile, project structure
49- **[Server Setup](resources/server-setup.md)** - Server creation, initialization, and basic configuration
50- **[gRPC & HTTP](resources/grpc-http.md)** - Registering services, HTTP handlers, static files
51
52### Authentication & Authorization
53- **[Authentication](resources/auth.md)** - OAuth, password auth, magic links, fake auth for testing
54- **[OAuth Server](resources/oauth-server.md)** - Build an OAuth2 authorization server for third-party apps
55- **[Authorization](resources/authz.md)** - Declarative access control with proto annotations, policies, role describers
56
57### Features
58- **[SSE Streaming](resources/sse.md)** - Server-Sent Events for real-time updates
59- **[Configuration](resources/configuration.md)** - YAML, environment variables, functional options
60- **[Storage](resources/storage.md)** - Storage plugins (memory, SQLite)
61- **[File Uploads](resources/uploads.md)** - File upload/download with authorization
62- **[Email](resources/email.md)** - SMTP email sending
63- **[Templates](resources/templates.md)** - Go HTML template rendering
64- **[Event Bus](resources/eventbus.md)** - Publish/subscribe inter-plugin communication
65
66### Development
67- **[Custom Plugins](resources/plugins.md)** - Creating plugins with dependencies and lifecycle
68- **[Error Handling](resources/errors.md)** - Stack traces, gRPC codes, log fields
69- **[Logging](resources/logging.md)** - Structured context-aware logging
70- **[Security](resources/security.md)** - CSRF, headers, HTTPS, token security
71
72## Code Style
73
74- Use `errors.New()`, `errors.NewC()`, `errors.Wrap()` for errors with stack traces
75- Standard library imports first, then third-party
76- Document public APIs with GoDoc comments
77- Follow plugin interface patterns with `Plugin()` function and `PluginName` constant
78
79## When to Load Resources
80
81| Task | Resources to Load |
82|------|-------------------|
83| Starting a new project | project-setup.md, server-setup.md |
84| Creating a new server | server-setup.md, configuration.md, logging.md |
85| Adding authentication | auth.md, server-setup.md |
86| Building an OAuth server | oauth-server.md, auth.md, storage.md |
87| Setting up access control | authz.md, auth.md |
88| Adding real-time features | sse.md |
89| Creating a custom plugin | plugins.md, eventbus.md |
90| Handling errors properly | errors.md, logging.md |
91| Security review | security.md |
92| Adding storage | storage.md |
93| Adding HTTP/gRPC endpoints | grpc-http.md |
94| Adding file uploads | uploads.md, authz.md |
95| Sending emails | email.md, templates.md |
96| Rendering templates | templates.md |
97| Inter-plugin communication | eventbus.md |
98| Setting up logging | logging.md |