Skip to main content
The contract template separates your business logic from transport concerns. β€œTransport” means how data travels - HTTP, WebSocket, or JSON-RPC. By separating these, you write your core logic once and expose it through multiple protocols automatically.

Directory Layout

The service/ Directory

This is where your business logic lives. Services are plain Go structs.

service/todo/todo.go

Key points:
  • Pure Go - no HTTP or transport concerns
  • Context as first parameter
  • Strongly typed inputs and outputs
  • Returns errors for proper handling

Method Signatures

The contract system recognizes these patterns:

The app/ Directory

Sets up the server and mounts transports.

app/server/server.go

What happens:
  1. Registry - Collects all services
  2. Register - Adds a service under a namespace (β€œtodo”)
  3. REST transport - Maps methods to HTTP endpoints
  4. JSON-RPC transport - Handles JSON-RPC calls
  5. OpenAPI - Generates documentation

app/server/config.go

The cmd/ Directory

Entry point for your service.

cmd/api/main.go

How Transports Map Methods

REST Mapping

Methods are mapped to HTTP endpoints automatically: The mapping follows conventions:
  • Create β†’ POST without ID
  • List β†’ GET without ID
  • Get β†’ GET with ID
  • Update β†’ PUT with ID
  • Delete β†’ DELETE with ID

JSON-RPC Mapping

All methods are available via JSON-RPC:
Response:

Adding a New Service

1. Create the Service

Create service/users/users.go:

2. Register the Service

Update app/server/server.go:

3. Test It

Type Documentation

Add documentation with struct tags:
This appears in the generated OpenAPI spec.

Next Steps

Tutorial

Build a complete contract service

Contract Concepts

Deep dive into contracts