@cocalc/backend
Version:
CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.
94 lines (92 loc) • 4.37 kB
JavaScript
"use strict";
/*
Where Data is Stored:
We centralize here determination of all directories on the filesystem
where data is stored for any of the components of CoCalc, run in any way.
All information here must be determinable when this module is initialized,
e.g., from environment variables or heuristics involving the filesystem.
In particular, nothing here can be impacted by command line flags
or content of a database.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.setApi = exports.apiBasePath = exports.apiServer = exports.apiKey = exports.blobstore = exports.logs = exports.secrets = exports.projects = exports.pgdatabase = exports.pghost = exports.pgdata = exports.pguser = exports.data = exports.root = void 0;
const DEFINITION = `CoCalc Environment Variables:
- root -- if COCALC_ROOT is set then it; otherwise use [cocalc-source]/src/.
- data -- if the environment variable DATA is set, use that. Otherwise, use {root}/data
- pgdata -- if env var PGDATA is set, use that; otherwise, it is {data}/postgres: where data data is stored (if running locally)
- pghost - if env var PGHOST is set, use that; otherwise, it is {data}/postgres/socket: what database connects to
- projects -- If env var PROJECTS is set, use that; otherwise, it is {data}"/projects/[project_id]";
This is where project home directories are (or shared files for share server), and it MUST
contain the string "[project_id]".
- secrets -- if env var SECRETS is set, use that; otherwise, it is {data}/secrets: where to store secrets
- logs -- if env var LOGS is set, use that; otherwise, {data}/logs: directory in which to store logs
`;
const path_1 = require("path");
function determineRootFromPath() {
const cur = __dirname;
const search = "/src/";
const i = cur.lastIndexOf(search);
const root = (0, path_1.resolve)(cur.slice(0, i + search.length - 1));
process.env.COCALC_ROOT = root;
return root;
}
exports.root = process.env.COCALC_ROOT ?? determineRootFromPath();
exports.data = process.env.DATA ?? (0, path_1.join)(exports.root, "data");
exports.pguser = process.env.PGUSER ?? "smc";
exports.pgdata = process.env.PGDATA ?? (0, path_1.join)(exports.data, "postgres");
exports.pghost = process.env.PGHOST ?? (0, path_1.join)(exports.pgdata, "socket");
exports.pgdatabase = process.env.SMC_DB ?? process.env.PGDATABASE ?? "smc";
exports.projects = process.env.PROJECTS ?? (0, path_1.join)(exports.data, "projects", "[project_id]");
exports.secrets = process.env.SECRETS ?? (0, path_1.join)(exports.data, "secrets");
exports.logs = process.env.LOGS ?? (0, path_1.join)(exports.data, "logs");
exports.blobstore = process.env.COCALC_JUPYTER_BLOBSTORE_IMPL ?? "sqlite";
exports.apiKey = process.env.API_KEY ?? "";
exports.apiServer = process.env.API_SERVER ?? "";
exports.apiBasePath = process.env.API_BASE_PATH
? process.env.API_BASE_PATH
: "/";
delete process.env.API_KEY; // reduce chances of it leaking.
function setApi({ key, server, basePath, }) {
if (key != null) {
exports.apiKey = key;
}
if (server != null) {
checkApiServer(server);
exports.apiServer = server;
}
if (basePath != null) {
checkBasePath(basePath);
exports.apiBasePath = basePath ? basePath : "/";
}
}
exports.setApi = setApi;
function sanityChecks() {
// Do a sanity check on projects:
if (!exports.projects.includes("[project_id]")) {
throw Error(`${DEFINITION}\n\nenv variable PROJECTS must contain "[project_id]" but it is "${process.env.PROJECTS}"`);
}
if (exports.blobstore != "sqlite" && exports.blobstore != "disk") {
throw Error("If set, COCALC_JUPYTER_BLOBSTORE_IMPL must be 'sqlite' or 'disk'");
}
checkApiServer(exports.apiServer);
checkBasePath(exports.apiBasePath);
}
function checkApiServer(server) {
if (!server)
return;
if (server.endsWith("/")) {
throw Error("API_SERVER must not end in /");
}
if (!server.startsWith("http://") && !server.startsWith("https://")) {
throw Error("API_SERVER must start with http:// or https://");
}
}
function checkBasePath(basePath) {
if (!basePath)
return;
if (!basePath.startsWith("/")) {
throw Error("base path must start with a slash");
}
}
sanityChecks();
//# sourceMappingURL=data.js.map