Structure services with Encore Go.
In Encore.go, each package with an API endpoint is automatically a service. No special configuration needed.
Simply create a package with at least one //encore:api endpoint:
// user/user.go
package user
import "context"
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
// This makes "user" a service
}
user/
โโโ user.go # API endpoints
โโโ db.go # Database (if needed)
โโโ migrations/ # SQL migrations
โโโ 1_create_users.up.sql
An application can define its APIs in one root service package:
my-app/
โโโ encore.app
โโโ go.mod
โโโ api.go # All endpoints
โโโ db.go # Database
โโโ migrations/
โโโ 1_initial.up.sql
Each service lives in its own package:
my-app/
โโโ encore.app
โโโ go.mod
โโโ user/
โ โโโ user.go
โ โโโ db.go
โ โโโ migrations/
โโโ order/
โ โโโ order.go
โ โโโ db.go
โ โโโ migrations/
โโโ notification/
โโโ notification.go
Group related services into systems:
my-app/
โโโ encore.app
โโโ go.mod
โโโ commerce/
โ โโโ order/
โ โ โโโ order.go
โ โโโ cart/
โ โ โโโ cart.go
โ โโโ payment/
โ โโโ payment.go
โโโ identity/
โ โโโ user/
โ โ โโโ user.go
โ โโโ auth/
โ โโโ auth.go
โโโ comms/
โโโ email/
โ โโโ email.go
โโโ push/
โโโ push.go
Just import and call the function directly - Encore handles the RPC:
package order
import (
"context"
"encore.app/user" // Import the user service
)
//encore:api auth method=GET path=/orders/:id
func GetOrderWithUser(ctx context.Context, params *GetOrderParams) (*OrderWithUser, error) {
order, err := getOrder(ctx, params.ID)
if err != nil {
return nil, err
}
// This becomes an RPC call - Encore handles it
orderUser, err := user.GetUser(ctx, &user.GetUserParams{ID: order.UserID})
if err != nil {
return nil, err
}
return &OrderWithUser{Order: order, User: orderUser}, nil
}
Create packages without //encore:api endpoints for shared code:
my-app/
โโโ user/
โ โโโ user.go # Service (has API)
โโโ order/
โ โโโ order.go # Service (has API)
โโโ internal/
โโโ util/
โ โโโ util.go # Not a service (no API)
โโโ validation/
โโโ validate.go
//encore:api endpointsencore-architecture when the service boundaries have not been decided