-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored serveHttp to use serve and updated Eta to latest version
- Loading branch information
1 parent
f69a737
commit 8f9e49f
Showing
9 changed files
with
130 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,46 @@ | ||
import { configure, renderFile } from "https://deno.land/x/eta@v1.11.0/mod.ts"; | ||
import { parse } from "https://deno.land/[email protected]/flags/mod.ts"; | ||
import { Eta } from "https://deno.land/x/eta@v3.0.3/src/index.ts"; | ||
import { parseArgs } from "jsr:@std/cli/parse-args"; | ||
|
||
const __dirname = new URL(".", import.meta.url).pathname; | ||
|
||
const viewPath = [ | ||
`${__dirname}/public`, | ||
`${__dirname}/public/partials`, | ||
`${__dirname}/public/layouts`, | ||
]; | ||
const viewpath = Deno.cwd() + "/src/public/"; | ||
const eta = new Eta({ views: viewpath, cache: true }); | ||
|
||
configure({ views: viewPath }); | ||
const flags = parse(Deno.args, { | ||
string: [ "port" ], | ||
default: { port: 80 }, | ||
const flags = parseArgs(Deno.args, { | ||
string: ["port"], | ||
default: { port: "3000" }, | ||
}); | ||
|
||
const server = Deno.listen({ port: flags.port }); | ||
|
||
console.log("File server running on http://localhost:" + flags.port + "/"); | ||
|
||
for await (const conn of server) { | ||
handleHttp(conn).catch(console.error); | ||
} | ||
|
||
async function handleHttp(conn: Deno.Conn) { | ||
const httpConn = Deno.serveHttp(conn); | ||
for await (const requestEvent of httpConn) { | ||
const url = new URL(requestEvent.request.url); | ||
let filepath = decodeURIComponent(url.pathname); | ||
if (filepath === "/") { | ||
filepath = "index.eta"; | ||
} else if (filepath.toLocaleLowerCase().indexOf(".") <= 0) { | ||
filepath = `${filepath}.eta`; | ||
} | ||
async function handler(request: Request) { | ||
const url = new URL(request.url); | ||
let filepath = decodeURIComponent(url.pathname); | ||
if (filepath === "/") { | ||
filepath = "/index.eta"; | ||
} else if (filepath.toLocaleLowerCase().indexOf(".") <= 0) { | ||
filepath = `${filepath}.eta`; | ||
} | ||
|
||
let file; | ||
let response; | ||
try { | ||
console.log(filepath); | ||
if (filepath.indexOf(".eta") > 0) { | ||
response = new Response(await renderFile(filepath, {}), { | ||
headers: { "content-type": "text/html" }, | ||
}); | ||
} else { | ||
file = await Deno.open(__dirname + "public" + filepath, { | ||
read: true, | ||
}); | ||
const readableStream = file.readable; | ||
response = new Response(readableStream); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
response = new Response("404 Not Found", { status: 404 }); | ||
return; | ||
let file; | ||
let response; | ||
try { | ||
console.log(filepath); | ||
if (filepath.indexOf(".eta") > 0) { | ||
response = new Response(await eta.render(filepath, {}), { | ||
headers: { "content-type": "text/html" }, | ||
}); | ||
} else { | ||
file = await Deno.open(__dirname + "public" + filepath, { | ||
read: true, | ||
}); | ||
const readableStream = file.readable; | ||
response = new Response(readableStream); | ||
} | ||
|
||
await requestEvent.respondWith(response); | ||
} catch (e) { | ||
console.error(e); | ||
response = new Response("404 Not Found", { status: 404 }); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
Deno.serve({ port: parseInt(flags.port) }, handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,16 @@ | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="shortcut icon" type="image/x-icon" href="images/favicon.png"> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" | ||
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> | ||
<link rel="stylesheet" href="index.css"> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
|
||
if(localStorage.getItem('consentMode') === null){ | ||
|
||
function gtag() { | ||
dataLayer.push(arguments); | ||
} | ||
|
||
if (localStorage.getItem('consentMode') === null) { | ||
gtag('consent', 'default', { | ||
'ad_storage': 'denied', | ||
'analytics_storage': 'denied', | ||
|
@@ -25,37 +27,45 @@ | |
gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode'))); | ||
} | ||
|
||
if(localStorage.getItem('userId') != null) { | ||
window.dataLayer.push({'user_id': localStorage.getItem('userId')}); | ||
if (localStorage.getItem('userId') != null) { | ||
window.dataLayer.push({ | ||
'user_id': localStorage.getItem('userId') | ||
}); | ||
} | ||
</script> | ||
<!-- Google Tag Manager --> | ||
<script>(function (w, d, s, l, i) { | ||
w[l] = w[l] || []; w[l].push({ | ||
'gtm.start': | ||
new Date().getTime(), event: 'gtm.js' | ||
}); var f = d.getElementsByTagName(s)[0], | ||
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src = | ||
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); | ||
})(window, document, 'script', 'dataLayer', 'GTM-MHJNBK6');</script> | ||
<script> | ||
(function(w, d, s, l, i) { | ||
w[l] = w[l] || []; | ||
w[l].push({ | ||
'gtm.start': new Date().getTime(), | ||
event: 'gtm.js' | ||
}); | ||
var f = d.getElementsByTagName(s)[0], | ||
j = d.createElement(s), | ||
dl = l != 'dataLayer' ? '&l=' + l : ''; | ||
j.async = true; | ||
j.src = | ||
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; | ||
f.parentNode.insertBefore(j, f); | ||
})(window, document, 'script', 'dataLayer', 'GTM-MHJNBK6'); | ||
</script> | ||
<!-- End Google Tag Manager --> | ||
<title><%= it.title %></title> | ||
</head> | ||
|
||
<body> | ||
<!-- Google Tag Manager (noscript) --> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MHJNBK6" height="0" width="0" | ||
style="display:none;visibility:hidden"></iframe></noscript> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MHJNBK6" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | ||
<!-- End Google Tag Manager (noscript) --> | ||
|
||
<%~ it.body %> | ||
|
||
<%~ includeFile("debug") %> | ||
<%~ include("../partials/debug") %> | ||
|
||
<%~ includeFile("consent") %> | ||
<%~ include("../partials/consent") %> | ||
|
||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" | ||
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" | ||
crossorigin="anonymous"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script> | ||
</body> | ||
</html> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.