2023-08-26 06:31:51 +00:00
|
|
|
import { Application, path } 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-05-23 18:53:53 +00:00
|
|
|
import { AssetBundlePlugSpacePrimitives } from "../common/spaces/asset_bundle_space_primitives.ts";
|
|
|
|
import { DiskSpacePrimitives } from "../common/spaces/disk_space_primitives.ts";
|
|
|
|
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
|
|
|
import { S3SpacePrimitives } from "../server/spaces/s3_space_primitives.ts";
|
2023-06-13 18:47:05 +00:00
|
|
|
import { Authenticator } from "../server/auth.ts";
|
|
|
|
import { JSONKVStore } from "../plugos/lib/kv_store.json_file.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { ServerSystem } from "../server/server_system.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
import { sleep } from "$sb/lib/async.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { SilverBulletHooks } from "../common/manifest.ts";
|
|
|
|
import { System } from "../plugos/system.ts";
|
2023-08-30 15:25:54 +00:00
|
|
|
import { silverBulletDbFile } from "./constants.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;
|
|
|
|
db?: string;
|
2023-08-30 15:25:54 +00:00
|
|
|
storeOnly?: 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-08-30 15:25:54 +00:00
|
|
|
const storeOnly = options.storeOnly || Deno.env.get("SB_STORE_ONLY");
|
|
|
|
let dbFile = options.db || Deno.env.get("SB_DB_FILE") || silverBulletDbFile;
|
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) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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") {
|
|
|
|
console.log(
|
2023-05-23 18:53:53 +00:00
|
|
|
`NOTE: SilverBullet will only be available locally (via http://localhost:${port}).
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
let spacePrimitives: SpacePrimitives | undefined;
|
|
|
|
if (folder === "s3://") {
|
2023-05-29 07:53:49 +00:00
|
|
|
spacePrimitives = new S3SpacePrimitives({
|
|
|
|
accessKey: Deno.env.get("AWS_ACCESS_KEY_ID")!,
|
|
|
|
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
|
|
|
|
endPoint: Deno.env.get("AWS_ENDPOINT")!,
|
|
|
|
region: Deno.env.get("AWS_REGION")!,
|
|
|
|
bucket: Deno.env.get("AWS_BUCKET")!,
|
|
|
|
});
|
2023-05-23 18:53:53 +00:00
|
|
|
console.log("Running in S3 mode");
|
2023-08-26 06:31:51 +00:00
|
|
|
folder = Deno.cwd();
|
2023-05-23 18:53:53 +00:00
|
|
|
} else {
|
2023-05-29 07:53:49 +00:00
|
|
|
// Regular disk mode
|
2023-05-23 18:53:53 +00:00
|
|
|
folder = path.resolve(Deno.cwd(), folder);
|
2023-05-29 07:53:49 +00:00
|
|
|
spacePrimitives = new DiskSpacePrimitives(folder);
|
2023-05-23 18:53:53 +00:00
|
|
|
}
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2023-05-29 07:53:49 +00:00
|
|
|
spacePrimitives = new AssetBundlePlugSpacePrimitives(
|
|
|
|
spacePrimitives,
|
|
|
|
new AssetBundle(plugAssetBundle as AssetJson),
|
|
|
|
);
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
let system: System<SilverBulletHooks> | undefined;
|
2023-08-30 15:25:54 +00:00
|
|
|
// system = undefined in storeOnly mode (no PlugOS instance on the server)
|
|
|
|
if (!storeOnly) {
|
2023-08-29 19:17:29 +00:00
|
|
|
// Enable server-side processing
|
2023-08-26 06:31:51 +00:00
|
|
|
dbFile = path.resolve(folder, dbFile);
|
2023-08-29 19:17:29 +00:00
|
|
|
console.log(
|
|
|
|
`Running in server-processing mode, keeping state in ${dbFile}`,
|
|
|
|
);
|
2023-08-26 06:31:51 +00:00
|
|
|
const serverSystem = new ServerSystem(spacePrimitives, dbFile, app);
|
|
|
|
await serverSystem.init();
|
|
|
|
spacePrimitives = serverSystem.spacePrimitives;
|
|
|
|
system = serverSystem.system;
|
|
|
|
if (options.reindex) {
|
|
|
|
console.log("Reindexing space (requested via --reindex flag)");
|
2023-08-30 20:36:27 +00:00
|
|
|
serverSystem.system.loadedPlugs.get("index")!.invoke(
|
2023-08-26 06:31:51 +00:00
|
|
|
"reindexSpace",
|
|
|
|
[],
|
2023-08-30 20:36:27 +00:00
|
|
|
).catch(console.error);
|
2023-08-26 06:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 18:47:05 +00:00
|
|
|
const authStore = new JSONKVStore();
|
|
|
|
const authenticator = new Authenticator(authStore);
|
|
|
|
|
|
|
|
const flagUser = options.user ?? Deno.env.get("SB_USER");
|
|
|
|
if (flagUser) {
|
|
|
|
// If explicitly added via env/parameter, add on the fly
|
|
|
|
const [username, password] = flagUser.split(":");
|
|
|
|
await authenticator.register(username, password, ["admin"], "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.auth) {
|
|
|
|
// Load auth file
|
|
|
|
const authFile: string = options.auth;
|
|
|
|
console.log("Loading authentication credentials from", authFile);
|
|
|
|
await authStore.load(authFile);
|
|
|
|
(async () => {
|
|
|
|
// Asynchronously kick off file watcher
|
2023-08-26 06:31:51 +00:00
|
|
|
for await (const _event of Deno.watchFs(options.auth!)) {
|
2023-06-13 18:47:05 +00:00
|
|
|
console.log("Authentication file changed, reloading...");
|
|
|
|
await authStore.load(authFile);
|
|
|
|
}
|
|
|
|
})().catch(console.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const envAuth = Deno.env.get("SB_AUTH");
|
|
|
|
if (envAuth) {
|
|
|
|
console.log("Loading authentication from SB_AUTH");
|
|
|
|
authStore.loadString(envAuth);
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:17:29 +00:00
|
|
|
const httpServer = new HttpServer(
|
|
|
|
spacePrimitives!,
|
|
|
|
app,
|
|
|
|
system,
|
|
|
|
{
|
|
|
|
hostname,
|
|
|
|
port: port,
|
|
|
|
pagesPath: folder!,
|
|
|
|
clientAssetBundle: new AssetBundle(clientAssetBundle as AssetJson),
|
|
|
|
authenticator,
|
|
|
|
keyFile: options.key,
|
|
|
|
certFile: options.cert,
|
|
|
|
},
|
|
|
|
);
|
2023-08-21 12:08:45 +00:00
|
|
|
await 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
|
|
|
}
|