#include "collection_registry.hpp" #include "db_gateway.hpp" #include "errors.hpp" #include "json_http.hpp" #include "file_store.hpp" #include "project_secret_store.hpp" #include "server.hpp" #include namespace svapi { void registerProjectRoutes(ApiServer& s) { auto& svr = s.raw(); ServerDeps* d = &s.deps(); svr.Get("/api/v1/projects", [d](const httplib::Request& req, httplib::Response& res) { ApiKey k = requireKey(*d, req); auto all = d->db.listProjects(); nlohmann::json arr = nlohmann::json::array(); for (const auto& p : all) if (k.canAccess(p)) arr.push_back(p); sendJson(res, 200, {{"projects", arr}}); }); svr.Post("/api/v1/projects", [d](const httplib::Request& req, httplib::Response& res) { ApiKey k = requireKey(*d, req); requireAdmin(k); auto body = bodyJson(req); std::string name = body.value("name", ""); if (name.empty()) throw ApiError(ErrCode::Unprocessable, "validation", "name is required"); if (!d->db.ensureProject(name)) throw ApiError(ErrCode::Unprocessable, "bad_project", "could not create project (check name rules)"); d->registry.ensure(name); sendJson(res, 201, {{"name", name}}); }); svr.Get(R"(/api/v1/projects/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) { ApiKey k = requireKey(*d, req); // NOT widened: the short shape of this route is the literal `/api/v1`, // which would hijack an unrelated path. Project CRUD is an operation on // the tenant rather than within it, so the full form is the only one // that makes sense here anyway - but it still resolves through // projectFor, so a project hostname cannot read another project. std::string project = projectFor(*d, req, 1); requireProjectAccess(k, project); auto all = d->db.listProjects(); if (std::find(all.begin(), all.end(), project) == all.end()) throw ApiError(ErrCode::NotFound, "not_found", "no such project"); sendJson(res, 200, {{"name", project}, {"collections", d->registry.list(project).size()}}); }); svr.Delete(R"(/api/v1/projects/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) { ApiKey k = requireKey(*d, req); requireAdmin(k); // See the GET above for why this pattern stays full-form. The tenancy // check matters more here than anywhere: this route DELETES a project. std::string project = projectFor(*d, req, 1); if (project == "default") throw ApiError(ErrCode::Unprocessable, "protected", "cannot drop the default project"); for (const auto& m : d->registry.list(project)) d->db.client().dropCollection(qualify(project, m.name)); d->db.client().dropCollection(qualify(project, "vectorapi_collections")); // The project's function secrets go with it. dropProject does not // physically purge collection data, so without this a recycled project // name would inherit the previous tenant's live credentials and hand // them to the new tenant's functions as ctx.env. ProjectSecretStore(d->db).dropAll(project); // And its files, for the same reason. Without this a deleted project's // logo and signature images outlive it, and a recycled project name // hands the next tenant the previous one's assets - the same class of // bug as a recycled collection name inheriting its constraints. FileStore(d->db).dropAll(project); if (!d->db.dropProject(project)) throw ApiError(ErrCode::Unavailable, "db_error", "drop failed"); sendJson(res, 200, {{"deleted", project}}); }); } }