Files
archie/internal/app/archie/api.go
T

90 lines
3.0 KiB
Go

package archie
import (
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/internal/platform/hub"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/chat"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/dashboard"
"github.com/samcharles93/archie-core/modules/deliveries"
"github.com/samcharles93/archie-core/modules/drafts"
"github.com/samcharles93/archie-core/modules/events"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/memory"
"github.com/samcharles93/archie-core/modules/runs"
"github.com/samcharles93/archie-core/modules/system"
)
// apiModules are the modules that serve the HTTP API.
type apiModules struct {
settings *governance.Module
conns *connectors.Module
system *system.Module
tell *deliveries.Module
allow *approvals.Module
classes *judgement.Classes
policies *judgement.Policies
hear *events.Module
judge *judgement.Module
engine *runs.Engine
chat *chat.Module
drafts *drafts.Module
notes *memory.Module
live *hub.Hub
}
func (m apiModules) register(api huma.API) {
m.settings.Register(api)
m.settings.RegisterAccounts(api)
m.settings.RegisterOrganisations(api)
m.settings.RegisterAccess(api)
m.conns.Register(api)
m.system.Register(api)
m.tell.Register(api)
m.allow.Register(api)
m.classes.Register(api, m.allow)
m.policies.Register(api)
m.hear.Register(api)
m.judge.Register(api)
m.engine.Register(api, orgID, m.hear, m.judge, m.allow)
m.drafts.Register(api)
m.chat.Register(api)
// The memory routes are served once the root wires the module, which it
// does after the modules that read it are built.
if m.notes != nil {
m.notes.Register(api)
}
dashboard.Register(api, m.live)
}
// Document returns the API's OpenAPI document as YAML. It makes the same
// registrations the server does, on modules with nothing behind them, so it
// needs no database.
func Document() ([]byte, error) {
settings := &governance.Module{}
_, api := emptyAPI(settings.Authenticator(""), settings)
return api.OpenAPI().YAML()
}
// emptyAPI registers every route on modules with nothing behind them.
func emptyAPI(auth httpapi.Authenticator, authz httpapi.Authorizer) (*http.ServeMux, huma.API) {
mux := http.NewServeMux()
settings := &governance.Module{}
api := httpapi.New(mux, buildinfo.Version, auth, authz)
apiModules{
settings: settings, conns: &connectors.Module{}, system: &system.Module{},
tell: &deliveries.Module{}, allow: &approvals.Module{}, classes: &judgement.Classes{},
policies: &judgement.Policies{}, hear: &events.Module{}, judge: &judgement.Module{},
engine: &runs.Engine{}, chat: &chat.Module{}, drafts: &drafts.Module{},
notes: &memory.Module{}, live: hub.New(),
}.register(api)
return mux, api
}