2022-10-24 11:51:26 +00:00
|
|
|
import { path } from "../server/deps.ts";
|
|
|
|
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";
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-05-24 03:42:02 +00:00
|
|
|
export function serveCommand(
|
|
|
|
options: any,
|
|
|
|
folder?: string,
|
|
|
|
) {
|
2022-12-16 12:00:06 +00:00
|
|
|
const hostname = options.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-05-23 18:53:53 +00:00
|
|
|
const maxFileSizeMB = options.maxFileSizeMB || 20;
|
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://") {
|
|
|
|
spacePrimitives = new AssetBundlePlugSpacePrimitives(
|
|
|
|
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")!,
|
|
|
|
}),
|
|
|
|
new AssetBundle(plugAssetBundle as AssetJson),
|
|
|
|
);
|
|
|
|
console.log("Running in S3 mode");
|
|
|
|
} else {
|
|
|
|
folder = path.resolve(Deno.cwd(), folder);
|
|
|
|
spacePrimitives = new AssetBundlePlugSpacePrimitives(
|
|
|
|
new DiskSpacePrimitives(folder, {
|
|
|
|
maxFileSizeMB: options.maxFileSizeMB,
|
|
|
|
}),
|
|
|
|
new AssetBundle(plugAssetBundle as AssetJson),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
console.log("Serving pages from", folder);
|
2022-10-24 11:51:26 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const httpServer = new HttpServer(spacePrimitives, {
|
2022-12-04 05:24:06 +00:00
|
|
|
hostname,
|
2022-10-24 11:51:26 +00:00
|
|
|
port: port,
|
2023-05-23 18:53:53 +00:00
|
|
|
pagesPath: folder,
|
|
|
|
clientAssetBundle: new AssetBundle(clientAssetBundle as AssetJson),
|
2023-05-24 03:42:02 +00:00
|
|
|
user: options.user ?? Deno.env.get("SB_USER"),
|
2023-05-23 18:53:53 +00:00
|
|
|
keyFile: options.key,
|
|
|
|
certFile: options.cert,
|
|
|
|
maxFileSizeMB: +maxFileSizeMB,
|
2022-10-24 11:51:26 +00:00
|
|
|
});
|
2023-05-23 18:53:53 +00:00
|
|
|
httpServer.start().catch(console.error);
|
2022-10-24 11:51:26 +00:00
|
|
|
}
|