feat(domains) implement domain routing; Logger

This commit is contained in:
gh0sTedBuddy
2024-03-20 03:43:27 +00:00
parent f303b67409
commit b5e8ce3144
15 changed files with 907 additions and 187 deletions

View File

@@ -1,5 +1,8 @@
import { LayServer } from '../src';
import { google } from 'googleapis';
import {resolve} from 'path';
import Html from '../src/Views/Html';
const APP_PORT = Bun.env.APP_PORT ?? Bun.env.PORT ?? 3000;
class InfoController {
static namespace = 'info';
@@ -10,7 +13,10 @@ class InfoController {
}
index(req, res) {
res.text('I think you are right here');
return res.json({
headers: req.headers
})
res.text('I think you are right here <a href="/country/spain">now go here</a>');
}
countryInfo(req, res) {
@@ -28,54 +34,16 @@ class InfoController {
}
}
class oAuthController {
static namespace = 'oauth';
constructor(server) {
server.get('/:provider', this.auth.bind(this));
server.get('/callback/:provider', this.callback.bind(this));
}
async auth(req, res) {
if (req.params.get('provider') != 'google') {
return {
messages: 'invalid provider',
data: [...req.params.entries()],
};
}
const scopes = 'https://www.googleapis.com/auth/youtube';
const url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
// If you only need one scope you can pass it as a string
scope: scopes,
});
console.log(url);
return Response.redirect(url, 301);
}
callback(req, res) {
if (!req.query.get('code')) {
return new Response('/', { status: 302 });
}
return new Response(`/${req.query.get('code')}`);
}
}
const server = new LayServer({
viewsPath: resolve(__dirname, 'views'),
loaders: new Map([[
'html', new Html()
]]),
controllers: [
function Main(server) {
server.get('/', async (req, res) => {
return {
message: 'Hi mom, i am on the internet!',
batman: req.query.get('batman'),
body: req.query,
};
});
return res.json({message: 'hello world'})
}, { domain: 'api.tipedi.local' });
server.all('/data', (req, res) => {
let respBody = `your ${req.method} request was well recieved`;
switch (req.method) {
@@ -110,9 +78,14 @@ const server = new LayServer({
});
});
},
oAuthController,
InfoController,
],
});
server.listen(Bun.env.APP_PORT ?? Bun.env.PORT ?? 42169);
server.get('/', async (req, res) => {
return res.render('index');
});
server.listen(APP_PORT, () => {
console.log(`server listen on ${APP_PORT}`);
});

29
examples/views/index.html Normal file
View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width,initial-scale=1.0'>
<link rel='stylesheet' href='app.css'>
<title>{{ title }}</title>
</head>
<body>
<header>
<div class="logo">
<a href="/">layc</a>
</div>
<nav class="nav">
<a href="/features">features</a>
<a href="/blog">Blog</a>
</nav>
</header>
<main>
{{ content }}
</main>
<footer>
<nav class="nav">
<a href="/imprint">Imprint</a>
<a href="/tos">Terms</a>
</nav>
</footer>
</body>
</html>