Prefabricated gRPC Server and JSON/REST Gateway
Prefab is a library designed to streamline the setup of gRPC servers with a
gRPC Gateway. It provides sensible defaults to get your server up and running
with minimal boilerplate, while also offering configuration via environment
variables, config files, or programmatic options.
Prefab includes a suite of plugins, adding capabilities such as
authentication, authorization, logging, and templating.
🤖 Agentic Coding Support
Prefab is designed to make agentic coding simple. A Claude Code plugin is included
at .claude-plugin/ with comprehensive skills and resources for building
Prefab servers. The plugin provides topic-specific documentation that is dynamically
loaded based on your task, covering server setup, authentication, authorization,
SSE streaming, configuration, storage, and more.
Install the plugin via: /plugin marketplace add dpup/prefab
✅ Features
- Quick setup: A production ready gRPC server in 6 lines of code.
- Multiplex gRPC and HTTP: Build hybrid monoliths serving both gRPC and HTTP on the same port.
- Serve static files: Serve static files for development or convenience.
- Pluggable: A plugin model provides easy customization and extensibility.
- Configurable: File, env, or functional options.
- Logging & errors: Request scoped field tracking, coded errors, stack traces, public/private error messages.
- Security: CSRF protection built-in and options for configuring CORS.
- Authn: Authenticate users with Google, Magic Links, or Email/Password.
- Authz: Use proto options to define access rules for RPC endpoints.
- OAuth2: Turn your Prefab server into an OAuth2 authorization server.
- Templates: Currently using standard go templates.
💡 Goals
gRPC is a powerful technology for building production-ready backends. Used with
a gRPC Gateway and an entity-oriented RPC design, it can greatly simplify the
construction and maintenance of web-facing REST-ish APIs.
Getting a project off the ground, however, often involves substantial
boilerplate and lacks features common in HTTP frameworks such as
Gin and Echo.
The purpose of prefab, therefore, is to expedite the development of gRPC web
services, which can range from pure gRPC servers to hybrid monoliths that also
serve HTTP. It offers optional plugins for authentication, access control,
logging, email, and more, to further speed up the development of a minimally
viable product, while always being production ready.
🚚 Key dependencies
🚀 Quick Start
Given a GRPC service implementation, the following snippet will start a running
server on localhost:5678 serving both GRPC requests and JSON rest, with the
only constraint that the GRPC path bindings must be prefixed with /api/.
package main
import (
"fmt"
"github.com/dpup/prefab"
)
func main() {
s := prefab.New()
s.RegisterService(
&FooBar_ServiceDesc,
RegisterFooBarHandler,
&foobarImpl{},
)
if err := s.Start(); err != nil {
fmt.Println(err)
}
}
🔌 Plugins
Plugin Model Overview
The base server is intended to have everything need to run a standalone service
that multiplexes across a GRPC interface, a JSON/REST interface via the GRPC
Gateway, and arbitrary HTTP handlers, for non-GRPC functionality. Additional
functionality is exposed as plugins.
Plugins are essentially server scoped singletons which add functionality to the
base server, expose functionality for other plugins, or extend other plugins.
As an example, the Magic Link Plugin extends the Auth Plugin to add
authentication via email. It also depends on an Email Plugin for email sending,
and a Template Plugin for rendering HTML emails.
It is intended that plugins can be have interchangable implementations to allow
customization at various parts of the stack.
Plugin interfaces
Plugins can implement a number of discrete interfaces: