Services and method contracts in Goat
Declaring methods, binding Go implementations, Web and MCP publication, and handling writes.
A service exposes domain operations such as reading notifications or changing project settings. A contract specifies the method name, input and output types, operation kind and publication policy. The Go implementation uses existing application services, repositories and transactions. The generator uses that contract to build Go interfaces, Web clients and publication metadata.
Where each part lives
| Location | Responsibility |
|---|---|
herd/services/*.service.goat, herd/services/*.catalog.goat | Operations, types, destinations, adapters and other domain metadata. |
herd/repositories/*.repository.goat | Contracts for persistent data access, including internal methods. |
herd/views/portable.catalog.goat | Explicit contracts:include declarations connect the project contracts. |
child/app/boards/, child/app/modelcontracts/ and other domain packages in child/app/ | Implementations, object-level authorization and result projection. |
child/app/servicecontracts/generated/ and child/web/projects/panel/src/views/generated/ | Derived interfaces and bindings for method contracts and foundation integration. |
repository:define --model=... extends an entity’s repository. A repository without --model can bind to an existing persistence implementation. dependency:use references a known dependency in an acyclic graph. The existing application container supplies implementations and manages their lifecycle. Shared modules can export contracts through model scripts. Include a declaration through one mechanism to avoid duplicate identities.
Initializing Go services
The GoatCMS entry point, child/cmd/goatcms/main.go, passes services.GenInstances to appcore.RunApp. It supplies database, layout and mail implementations alongside application information and shop extensions. herd/gen/templates/go/services_gen.go.tmpl defines the shape of the project container. Project code imports foundation packages by their full Go prefixes.
After initialization, child/app/bootstrap/body.go registers application commands. At server startup, child/app/routes/routes.go builds domain services with dependencies from the foundation, such as the database, IAM and model repositories. It registers routes, methods and extensions. The serve action starts background workers within the container lifecycle, which cancels and joins them on shutdown.
In child/app/boards/contract_services.go, ContractMethods() populates the generated Registry with adapters that use existing services. For example, notifications.list uses a notification adapter backed by boards.Service. Application routes extend the registry for domains such as mockups and editorial. Bind an implementation to the appropriate registry field and verify registration at startup. Tests can supply an interface implementation with controlled results and separately exercise actual publication rules at the endpoint.
Reading a declaration
This shortened excerpt from herd/services/pilots.service.goat describes an inbox read.
service:define --name=notifications --doc="The current account's inbox." --body=<<SERVICE
implementation:use --name=boards --go-package=code.pozoga.eu/spozoga/goatcms.com/child/app/boards --go-name=Service --web-module=../../portable-pilot-services --web-name=createNotificationServices --mock-module=../../portable-pilot-services --mock-name=createMockNotificationServices
method:add --name=list --kind=read --output=pilots.notification-page --body=<<METHOD
web:publish --permission=pilots.read --adapter=notification-inbox
mcp:publish --name=view_notifications_list --scopes="account:read tasks:read"
METHOD
SERVICEThe view refers to this method as notifications.list. --input identifies an input object type. Omit it when the client supplies no arguments. --output names the DTO type returned. --doc explains behavior to technical consumers. error:add --code=... declares a known domain error, and example:add supplies a validated input, result or error example. Successful examples can supply mock data. Use JSON for example values and Goat subterminals for the contract structure.
Publication and delivery
- Internal. Methods without a publication serve application code. Project client DTOs explicitly and apply authorization and visibility filters before projection and pagination.
- Web.
web:publishdefaults to authenticated access. A public method must declare--access=public. The host applies session and CSRF rules. The project’s native handler serves/api/panel/v1/methods/{name}. - MCP.
mcp:publishis a separate declaration. The tool name, profile, scopes and resource restrictions define access through a connection. The application still authorizes the current account, project and object. After a publication change, refresh the client catalogue and verify discovery and invocation through/api/mcp/v1/all. - Transport. Delivery is independent of
read|write. JSON is the default. Upload, download and subscription need a compatible provider with live and mock factories, selected bytransport:use. A subscription also specifies an event type. The provider handles binary payloads. An invalid or missing provider blocks the operation. - Preview. The mock factory may keep state within a view instance. Disposing the preview ends that state’s lifetime. A missing mock fails the call.
Writes, conflicts and unknown outcomes
For a durable Web write, declare --idempotency=required and a resolver in web:publish --resolve=.... Use a durable write receipt or a method-specific postcondition check where the operation is naturally idempotent. For example, a task move commits the change and receipt in one transaction. After losing a response, the client checks the original attempt using the same key. A missing receipt leaves the outcome unknown. Recheck authorization according to the contract when reading a receipt. Versioned updates should report an explicit conflict so the caller can reload and decide whether to write again.
web:publish --attempts=memory is available for Web operations without durable receipts, such as native actions on credentials. These methods cannot use MCP publication, idempotency keys or resolvers. The runtime sends one attempt. On an unknown outcome, the view can display a message and read the current state. Keep secrets out of contract examples and durable receipts.
From contract to working method
- Define the name, types, data scope, errors and write policy. Include the declaration in the catalogue and add an example.
- Bind application code in
child/app/. Use the authenticated operation context and the existing transaction. Register live and mock factories and any required transport provider. - Regenerate.
goat rerefreshes modules and derived sources. Inspect the generated types andcontractSetId, which reflects signatures, registrations and metadata, among other inputs. Historical previews use contracts from their own revision. - Check the successful result, denied access, validation errors and mock behavior. For writes, test conflicts and receipt resolution after a lost response. For Web and MCP publications, exercise the real boundaries with permissions and resource restrictions.
goat run:script --path=child/ci/go.goat -- --packages="./child/app/panelviews/... ./child/app/boards/..."
goat run:script --path=child/ci/panel.goat -- --suite=viewsSee herd/specifications/service-contracts/ in the repository for the full syntax and examples of Go consumers with PostgreSQL. Select tests for the domain package that implements the method.