This commit is contained in:
Zef Hemel
2022-12-05 12:14:21 +01:00
parent b24c2996be
commit e780a838b6
8 changed files with 38 additions and 41 deletions
+17 -9
View File
@@ -11,7 +11,8 @@ export type ServerOptions = {
pagesPath: string;
dbPath: string;
assetBundle: AssetBundle;
password?: string;
user?: string;
pass?: string;
};
const staticLastModified = new Date().toUTCString();
@@ -20,14 +21,14 @@ export class HttpServer {
app: Application;
systemBoot: SpaceSystem;
private port: number;
password?: string;
user?: string;
settings: { [key: string]: any } = {};
abortController?: AbortController;
constructor(options: ServerOptions) {
this.port = options.port;
this.app = new Application(); //{ serverConstructor: FlashServer });
this.password = options.password;
this.user = options.user;
this.systemBoot = new SpaceSystem(
options.assetBundle,
options.pagesPath,
@@ -63,6 +64,7 @@ export class HttpServer {
await this.systemBoot.start();
await this.systemBoot.ensureSpaceIndex();
await this.ensureAndLoadSettings();
// Serve static files (javascript, css, html)
this.app.use(async ({ request, response }, next) => {
if (request.url.pathname === "/") {
@@ -106,6 +108,8 @@ export class HttpServer {
}
});
this.addPasswordAuth(this.app);
// Pages API
const fsRouter = this.buildFsRouter(this.systemBoot.spacePrimitives);
this.app.use(fsRouter.routes());
@@ -163,15 +167,20 @@ export class HttpServer {
}
}
private addPasswordAuth(r: Router) {
if (this.password) {
r.use(async ({ request, response }, next) => {
private addPasswordAuth(app: Application) {
if (this.user) {
app.use(async ({ request, response }, next) => {
if (
request.headers.get("Authorization") === `Bearer ${this.password}`
request.headers.get("Authorization") ===
`Basic ${btoa(this.user!)}`
) {
await next();
} else {
response.status = 401;
response.headers.set(
"WWW-Authenticate",
`Basic realm="Please enter your username and password"`,
);
response.body = "Unauthorized";
}
});
@@ -180,7 +189,6 @@ export class HttpServer {
private buildFsRouter(spacePrimitives: SpacePrimitives): Router {
const fsRouter = new Router();
this.addPasswordAuth(fsRouter);
// File list
fsRouter.get("/", async ({ response }) => {
response.headers.set("Content-type", "application/json");
@@ -276,7 +284,7 @@ export class HttpServer {
private buildPlugRouter(): Router {
const plugRouter = new Router();
this.addPasswordAuth(plugRouter);
// this.addPasswordAuth(plugRouter);
const system = this.systemBoot.system;
plugRouter.post(