package api import ( "github.com/go-chi/chi/v5" "git.avsdev.uk/Go/job-search/internal/events" "git.avsdev.uk/Go/job-search/internal/store" ) // API is the REST API, mounted under /api. Create it with New, giving it what // its resources need as options, and register its routes with Routes. The // server applies the API's middleware (CSRF, NoStore, RequireJSON, rate // limits); the API only registers handlers. type API struct { store *store.Store // nil: no opportunity routes events *events.Bus // nil: nothing is published } // Option configures the API. type Option func(*API) // WithStore makes the API serve the opportunities in opportunityStore under // /opportunities, and their classifications under /classifications. Without // it there are neither. func WithStore(opportunityStore *store.Store) Option { return func(a *API) { a.store = opportunityStore } } // WithEvents makes the API publish its changes to bus, such as an // events.OpportunityMarksUpdated event when an opportunity's marks change. func WithEvents(bus *events.Bus) Option { return func(a *API) { a.events = bus } } // New returns the API. func New(opts ...Option) *API { api := &API{} for _, option := range opts { option(api) } return api } // Routes registers the API's routes on router (mounted under /api): its // description (see openapi.go), and the opportunities and classifications if // it has a store. func (a *API) Routes(router chi.Router) { docsRoutes(router) if a.store != nil { newOpportunityRoutes(a.store, a.events).Routes(router) newClassificationRoutes(a.store).Routes(router) } }