feat: add support for domain hosting
This commit is contained in:
parent
5270e95869
commit
812881ca52
@ -1,9 +1,10 @@
|
||||
import { MongoClient } from "https://deno.land/x/mongo@v0.31.2/mod.ts";
|
||||
import { RootRouter } from "./routers/RootRouter.ts";
|
||||
import { APIRouter } from "./routers/APIRouter.ts";
|
||||
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
|
||||
import AppDatabase from "./AppDatabase.ts";
|
||||
import { StaticRouter } from "./routers/StaticRouter.ts";
|
||||
|
||||
export default async function clonegur() {
|
||||
export default async function clonegur(apiUrl: string) {
|
||||
let salt;
|
||||
|
||||
try {
|
||||
@ -19,6 +20,9 @@ export default async function clonegur() {
|
||||
db: 'clonegur',
|
||||
servers: [ { host: '127.0.0.1', port: 27017 } ]
|
||||
});
|
||||
return new RootRouter(salt, new AppDatabase(db));
|
||||
return {
|
||||
static: new StaticRouter(apiUrl),
|
||||
api: new APIRouter(salt, new AppDatabase(db)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,41 @@
|
||||
import clonegur from "./clonegur.ts";
|
||||
import { APIRouter } from "./routers/APIRouter.ts";
|
||||
import AppRouter from "./routers/AppRouter.ts";
|
||||
import { StaticRouter } from "./routers/StaticRouter.ts";
|
||||
import { route } from "./server/decorators.ts";
|
||||
import RestRequest from "./server/RestRequest.ts";
|
||||
|
||||
const app = await clonegur();
|
||||
const server = Deno.listen({ port: 80, hostname: '127.0.0.1' });
|
||||
app.attach(server);
|
||||
if (Deno.args[0] === "devel") {
|
||||
class DevelRouter extends AppRouter {
|
||||
@route('api/*') api;
|
||||
@route('*') static;
|
||||
|
||||
public constructor(api: APIRouter, _static: StaticRouter) {
|
||||
super();
|
||||
this.api = api;
|
||||
this.static = _static;
|
||||
}
|
||||
}
|
||||
|
||||
const app = await clonegur('http://localhost:80/');
|
||||
const server = Deno.listen({ port: 80, hostname: '127.0.0.1' });
|
||||
new DevelRouter(app.api, app.static).attach(server);
|
||||
}
|
||||
else if (Deno.args[0] === "prod") {
|
||||
const api = Deno.args[1] ?? 'api.clonegur.topcheto.eu';
|
||||
const stt = Deno.args[2] ?? 'clonegur.topcheto.eu';
|
||||
const app = await clonegur('https://:80/');
|
||||
|
||||
const server = Deno.listenTls({ port: 80, hostname: '127.0.0.1' });
|
||||
|
||||
server.accept().then(async conn => {
|
||||
for await (const req of Deno.serveHttp(conn)) {
|
||||
const url = new URL(req.request.url);
|
||||
if (url.hostname === api) app.api.handle(RestRequest.fromMessage(req));
|
||||
else if (url.hostname === stt) app.static.handle(RestRequest.fromMessage(req));
|
||||
else req.respondWith(new Response("nope :/", {
|
||||
status: 404
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
17
backend/routers/APIRouter.ts
Normal file
17
backend/routers/APIRouter.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import UserRouter from "../routers/UserRouter.ts";
|
||||
import ImageRouter from "./ImageRouter.ts";
|
||||
import AppRouter from "./AppRouter.ts";
|
||||
import AppDatabase from "../AppDatabase.ts";
|
||||
import { route } from "../server/decorators.ts";
|
||||
|
||||
export class APIRouter extends AppRouter {
|
||||
@route('users/*') users;
|
||||
@route('images/*') images;
|
||||
|
||||
constructor(salt: string, db: AppDatabase) {
|
||||
super();
|
||||
|
||||
this.users = new UserRouter(salt, db);
|
||||
this.images = new ImageRouter(db);
|
||||
}
|
||||
}
|
@ -1,11 +1,22 @@
|
||||
import { rest } from "../server/decorators.ts";
|
||||
import HttpError from "../server/HttpError.ts";
|
||||
import RestRequest from "../server/RestRequest.ts";
|
||||
import RestResponse from "../server/RestResponse.ts";
|
||||
import Router from "../server/Router.ts";
|
||||
import { stream } from "../utils/utils.ts";
|
||||
|
||||
export default class AppRouter extends Router {
|
||||
public onError(_req: RestRequest, error: unknown): RestResponse | HttpError | Promise<RestResponse | HttpError> {
|
||||
if (error instanceof HttpError) return new HttpError({ error: error.body }, error.status);
|
||||
return super.onError(_req, error);
|
||||
}
|
||||
|
||||
@rest('OPTIONS', '*')
|
||||
options() {
|
||||
return new RestResponse();
|
||||
}
|
||||
@rest('*', '*')
|
||||
default() {
|
||||
return new RestResponse().body(stream('Page not found :/')).status(404);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
import UserRouter from "../routers/UserRouter.ts";
|
||||
import ImageRouter from "./ImageRouter.ts";
|
||||
import AppRouter from "./AppRouter.ts";
|
||||
import RestResponse from "../server/RestResponse.ts";
|
||||
import { rest, route } from "../server/decorators.ts";
|
||||
import { stream } from "../utils/utils.ts";
|
||||
import AppDatabase from "../AppDatabase.ts";
|
||||
import staticHandler from "../server/staticHandler.ts";
|
||||
|
||||
export class RootRouter extends AppRouter {
|
||||
@route('api/users/*') users;
|
||||
@route('api/images/*') images;
|
||||
|
||||
@rest('GET', '/')
|
||||
async index() {
|
||||
return new RestResponse()
|
||||
.body((await Deno.open('static/index.html')).readable)
|
||||
.contentType('html');
|
||||
}
|
||||
@route('/*') static;
|
||||
|
||||
@rest('OPTIONS', '*')
|
||||
options() {
|
||||
return new RestResponse();
|
||||
}
|
||||
@rest('*', '*')
|
||||
default() {
|
||||
return new RestResponse().body(stream('Page not found :/')).status(404);
|
||||
}
|
||||
|
||||
constructor(salt: string, db: AppDatabase) {
|
||||
super();
|
||||
|
||||
this.static = staticHandler('static');
|
||||
this.users = new UserRouter(salt, db);
|
||||
this.images = new ImageRouter(db);
|
||||
}
|
||||
}
|
23
backend/routers/StaticRouter.ts
Normal file
23
backend/routers/StaticRouter.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { rest, route } from "../server/decorators.ts";
|
||||
import RestResponse from "../server/RestResponse.ts";
|
||||
import staticHandler from "../server/staticHandler.ts";
|
||||
import { stream } from "../utils/utils.ts";
|
||||
import AppRouter from "./AppRouter.ts";
|
||||
|
||||
export class StaticRouter extends AppRouter {
|
||||
@rest('GET', '/http-url.js')
|
||||
getHttpUrl() {
|
||||
return stream(`var $_url = ${this.apiUrl};`);
|
||||
}
|
||||
@rest('GET', '/')
|
||||
async index() {
|
||||
return new RestResponse()
|
||||
.body((await Deno.open('static/index.html')).readable)
|
||||
.contentType('html');
|
||||
}
|
||||
@route('/*') static = staticHandler('static');
|
||||
|
||||
public constructor(private apiUrl: string) {
|
||||
super();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user