2023-12-10 12:23:42 +00:00
|
|
|
import { Application } from "../server/deps.ts";
|
2022-10-24 11:51:26 +00:00
|
|
|
import { HttpServer } from "../server/http_server.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import clientAssetBundle from "../dist/client_asset_bundle.json" assert {
|
|
|
|
type: "json",
|
|
|
|
};
|
|
|
|
import plugAssetBundle from "../dist/plug_asset_bundle.json" assert {
|
|
|
|
type: "json",
|
|
|
|
};
|
2022-10-24 11:51:26 +00:00
|
|
|
import { AssetBundle, AssetJson } from "../plugos/asset_bundle/bundle.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
import { sleep } from "$sb/lib/async.ts";
|
2023-12-10 12:23:42 +00:00
|
|
|
|
|
|
|
import { determineDatabaseBackend } from "../server/db_backend.ts";
|
|
|
|
import { SpaceServerConfig } from "../server/instance.ts";
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-06-13 18:47:05 +00:00
|
|
|
export async function serveCommand(
|
2023-08-26 06:31:51 +00:00
|
|
|
options: {
|
|
|
|
hostname?: string;
|
|
|
|
port?: number;
|
|
|
|
user?: string;
|
|
|
|
auth?: string;
|
|
|
|
cert?: string;
|
|
|
|
key?: string;
|
|
|
|
reindex?: boolean;
|
2023-12-13 16:52:56 +00:00
|
|
|
syncOnly?: boolean;
|
2023-12-17 10:46:18 +00:00
|
|
|
clientEncryption?: boolean;
|
2023-08-26 06:31:51 +00:00
|
|
|
},
|
2023-05-24 03:42:02 +00:00
|
|
|
folder?: string,
|
|
|
|
) {
|
2023-08-11 18:37:13 +00:00
|
|
|
const hostname = options.hostname || Deno.env.get("SB_HOSTNAME") ||
|
|
|
|
"127.0.0.1";
|
2023-05-24 03:42:02 +00:00
|
|
|
const port = options.port ||
|
|
|
|
(Deno.env.get("SB_PORT") && +Deno.env.get("SB_PORT")!) || 3000;
|
2023-12-17 10:46:18 +00:00
|
|
|
|
|
|
|
const clientEncryption = options.clientEncryption ||
|
|
|
|
!!Deno.env.get("SB_CLIENT_ENCRYPTION");
|
|
|
|
|
|
|
|
if (clientEncryption) {
|
|
|
|
console.log(
|
|
|
|
"Running in client encryption mode, this will implicitly enable sync-only mode",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-13 16:52:56 +00:00
|
|
|
const syncOnly = options.syncOnly || !!Deno.env.get("SB_SYNC_ONLY");
|
2023-12-17 10:46:18 +00:00
|
|
|
|
|
|
|
if (syncOnly) {
|
|
|
|
console.log("Running in sync-only mode (no backend processing)");
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
const app = new Application();
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-05-24 03:42:02 +00:00
|
|
|
if (!folder) {
|
2023-12-10 12:23:42 +00:00
|
|
|
// Didn't get a folder as an argument, check if we got it as an environment variable
|
2023-05-24 03:42:02 +00:00
|
|
|
folder = Deno.env.get("SB_FOLDER");
|
|
|
|
if (!folder) {
|
|
|
|
console.error(
|
|
|
|
"No folder specified. Please pass a folder as an argument or set SB_FOLDER environment variable.",
|
|
|
|
);
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-12-10 12:23:42 +00:00
|
|
|
|
|
|
|
const baseKvPrimitives = await determineDatabaseBackend(folder);
|
2023-05-24 03:42:02 +00:00
|
|
|
|
2022-10-24 11:51:26 +00:00
|
|
|
console.log(
|
2023-01-16 15:45:55 +00:00
|
|
|
"Going to start SilverBullet binding to",
|
2022-12-04 05:24:06 +00:00
|
|
|
`${hostname}:${port}`,
|
2022-10-24 11:51:26 +00:00
|
|
|
);
|
2022-12-16 12:00:06 +00:00
|
|
|
if (hostname === "127.0.0.1") {
|
2023-12-10 12:23:42 +00:00
|
|
|
console.info(
|
|
|
|
`SilverBullet will only be available locally (via http://localhost:${port}).
|
2023-05-23 18:53:53 +00:00
|
|
|
To allow outside connections, pass -L 0.0.0.0 as a flag, and put a TLS terminator on top.`,
|
2022-12-16 12:00:06 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-12-10 12:23:42 +00:00
|
|
|
const userAuth = options.user ?? Deno.env.get("SB_USER");
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2023-12-13 16:52:56 +00:00
|
|
|
let userCredentials: { user: string; pass: string } | undefined;
|
|
|
|
if (userAuth) {
|
|
|
|
const [user, pass] = userAuth.split(":");
|
|
|
|
userCredentials = { user, pass };
|
|
|
|
}
|
2023-12-10 12:23:42 +00:00
|
|
|
const configs = new Map<string, SpaceServerConfig>();
|
|
|
|
configs.set("*", {
|
|
|
|
hostname,
|
|
|
|
namespace: "*",
|
2023-12-13 16:52:56 +00:00
|
|
|
auth: userCredentials,
|
|
|
|
authToken: Deno.env.get("SB_AUTH_TOKEN"),
|
2023-12-17 10:46:18 +00:00
|
|
|
syncOnly,
|
|
|
|
clientEncryption,
|
2023-12-10 12:23:42 +00:00
|
|
|
pagesPath: folder,
|
|
|
|
});
|
2023-06-13 18:47:05 +00:00
|
|
|
|
2023-12-10 12:23:42 +00:00
|
|
|
const httpServer = new HttpServer({
|
2023-08-29 19:17:29 +00:00
|
|
|
app,
|
2023-12-10 12:23:42 +00:00
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
clientAssetBundle: new AssetBundle(clientAssetBundle as AssetJson),
|
|
|
|
plugAssetBundle: new AssetBundle(plugAssetBundle as AssetJson),
|
|
|
|
baseKvPrimitives,
|
|
|
|
keyFile: options.key,
|
|
|
|
certFile: options.cert,
|
|
|
|
configs,
|
|
|
|
});
|
|
|
|
httpServer.start();
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2023-08-21 12:08:45 +00:00
|
|
|
// Wait in an infinite loop (to keep the HTTP server running, only cancelable via Ctrl+C or other signal)
|
|
|
|
while (true) {
|
2023-08-26 06:31:51 +00:00
|
|
|
await sleep(10000);
|
2023-08-21 12:08:45 +00:00
|
|
|
}
|
2022-10-24 11:51:26 +00:00
|
|
|
}
|