Skip to content
quarry Artifact [818a065762]
Login

Artifact [818a065762]

Artifact 818a0657629e5c7c1b5acd0ac5a85b11d45b13a0a090bcfe0fea191dbf1240c3:


// Package api owns RegisterAll โ€” the one explicit wiring point where every
// op joins the registry (0003 ยง2: no init() magic; the wiring stays
// greppable) โ€” plus the HTTP router that serves the registered ops. This
// is the sanctioned place Run closures over the data planes are built;
// the arch gate keeps every other consumer on the op side of the seam.
package api

import (
	"context"
	"fmt"

	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/ops"
)

// Deps carries what op implementations close over. Index may be nil (no
// store configured โ€” `quarry spec` runs this way); ops that need it
// answer with an error at run time, and the spec stays complete either
// way.
type Deps struct {
	Version string
	Index   *index.Index
}

type versionIn struct{}

type versionOut struct {
	Version string `json:"version"`
}

type timelineIn struct {
	Limit int `json:"limit" validate:"min=0,max=200"`
}

type timelineOut struct {
	Entries []index.TimelineEntry `json:"entries"`
	Count   int                   `json:"count"`
}

// RegisterAll wires every op.
func RegisterAll(r *ops.Registry, d Deps) error {
	if err := ops.Register(r, ops.Op[versionIn, versionOut]{
		Name:  "meta.version",
		Doc:   "Report the quarry build version.",
		Kind:  ops.Query,
		Auth:  ops.Anonymous,
		Route: "GET /api/v1/version",
		Run: func(_ context.Context, _ versionIn) (versionOut, error) {
			return versionOut{Version: d.Version}, nil
		},
	}); err != nil {
		return err
	}
	return ops.Register(r, ops.Op[timelineIn, timelineOut]{
		Name:  "timeline.list",
		Doc:   "List check-ins, newest first.",
		Kind:  ops.Query,
		Auth:  ops.Anonymous,
		Route: "GET /api/v1/timeline",
		Run: func(_ context.Context, in timelineIn) (timelineOut, error) {
			if d.Index == nil {
				return timelineOut{}, fmt.Errorf("no store is being served")
			}
			limit := in.Limit
			if limit == 0 {
				limit = 50
			}
			entries, err := d.Index.Timeline(limit)
			if err != nil {
				return timelineOut{}, err
			}
			return timelineOut{Entries: entries, Count: len(entries)}, nil
		},
	})
}