Fabrica Blog

Simple guides introducing Fabrica with hands-on examples.

View the Project on GitHub OpenCHAMI/fabrica

Fabrica Migrations

Fabrica migrations update existing project source files when generator behavior changes. Run migrations from the project root before regenerating with a newer Fabrica version.

Available Migrations

Migration Command What it does When to run
YAML tags fabrica migrate yaml-tags Adds missing yaml struct tags matching existing json tags in resource type files. Existing services created before resource templates emitted YAML tags.

YAML Tags

The YAML tags migration updates files under apis/<group>/<version>/*_types.go so YAML data can be unmarshaled into resource structs the same way JSON data can.

For a field like this:

Spec DeviceSpec `json:"spec" validate:"required"`

the migration writes:

Spec DeviceSpec `json:"spec" yaml:"spec" validate:"required"`

The migration:

How to Migrate

Preview the changes first:

fabrica migrate yaml-tags --dry-run

Apply the migration:

fabrica migrate yaml-tags

Regenerate Fabrica-managed code so generated request models, clients, and storage helpers pick up updated templates:

fabrica generate

For a project outside the current directory, pass --dir:

fabrica migrate yaml-tags --dir /path/to/service --dry-run
fabrica migrate yaml-tags --dir /path/to/service

Troubleshooting

If the migration reports a parse error, fix the Go syntax in the referenced *_types.go file and rerun the command. The migration intentionally refuses to rewrite files that cannot be parsed safely.

If the dry-run reports no changes, the resource files already have YAML tags, do not have JSON tags to mirror, or are not listed under the configured API groups and versions in apis.yaml.

API Discovery Catalog

Generated services now support public GET and HEAD requests at /.well-known/api-catalog. The catalog always lists the standard /service/status API endpoint, followed by generated resource endpoints, with their documentation and status links in RFC 9727 JSON Linkset format. Services without generated resources can still discover their OpenAPI specification through the status entry.

  1. Set generation.openapi_server_url in .fabrica.yaml to the public service URL, including any deployment prefix, then run fabrica generate using the matching Fabrica CLI. This updates both routes and OpenAPI output.
  2. Ensure project-owned cmd/server/main.go calls RegisterGeneratedPublicRoutes(public) in its public router group, outside authentication and authorization middleware. Newly initialized services already include this call. If present, no startup change is needed. Do not also register /service/status separately when adding this helper.
  3. Build and test the service, then verify:

    curl -i http://localhost:8080/.well-known/api-catalog
    curl -I http://localhost:8080/.well-known/api-catalog
    

    Expect HTTP 200 and Content-Type: application/linkset+json with the RFC 9727 profile. GET returns catalog links; HEAD returns a Link header with rel="api-catalog" and no body. Verify both requests without credentials.

When using a reverse proxy with a deployment prefix, route the origin’s /.well-known/api-catalog to the service’s catalog endpoint as well, so clients can discover the catalog at the standard well-known location.

Public Service Status Route

Fabrica v0.4.12 generates a public /service/status handler and registration function. Newly initialized services call that function automatically. Existing services adopting the generated endpoint must update their project-owned HTTP setup; fabrica generate does not rewrite cmd/server/main.go.

Use this checklist:

  1. Update the service’s Fabrica dependency and use the matching Fabrica CLI, then regenerate the service.

    fabrica generate
    
  2. In cmd/server/main.go, add the registration call to the public router group, alongside the existing public routes:

    r.Group(func(public chi.Router) {
        public.Get("/health", healthHandler)
        RegisterGeneratedPublicRoutes(public)
        public.Get("/openapi.json", ServeOpenAPISpec)
        public.Get("/docs", ServeSwaggerUI)
        // Preserve other existing public routes.
    })
    

    Keep this group outside authentication and authorization middleware, as in the new-service template. Apply those middleware to resource routes separately.

  3. If the service already registers /service/status, replace that registration when adopting the generated endpoint. Review any custom health logic and response fields before removing its handler. The generated provider initially returns ok, and its response contains only status and service.

  4. Review the changes, run focused tests, and build the affected service.

    git diff
    go mod tidy  # If generated imports changed.
    go test ./cmd/server ./internal/...
    go build ./cmd/server
    git diff --check
    
  5. Start the service and verify an anonymous request, using its configured address:

    curl -i http://localhost:8080/service/status
    

    With the default provider, expect HTTP 200 and {"status":"ok","service":"<service-name>"}. Repeat this check with authentication enabled to confirm the endpoint remains public.

New services do not need this manual registration step. They get the generated public status route from the start.