Index

joshstock.in / 9b4a3f1

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7320 Jan 2020 21:499b4a3f1Begin gitpager rewriteJosh Stockin1450N

Blob @ joshstock.in / gitpager / src / server.js

application/javascript1307 bytesdownload raw
1const http = require('http');
2const fs = require('fs');
3const config = require('./config');
4const pathFix = require('./pathFix');
5const template = require('./templates');
6const pages = require('./pages');
7
8const listenerFunction = function(request, response) {
9 if (request.url in config.static) { // static file
10 response.setHeader('Content-Type', config.static[request.url].type);
11 response.writeHead(200);
12 response.write(fs.readFileSync(pathFix(config.static[request.url].path), 'utf-8'));
13 response.end();
14 return;
15 }
16 if (request.url == '/') { // index
17 response.setHeader('Content-Type', 'text/html');
18 response.writeHead(200);
19 response.write(pages.index());
20 response.end();
21 return;
22 }
23 if (request.url.match('^\/.*\/[0-9]*$')) { // /reponame/pagenumber
24 repositoryName = request.url.split('/')[1];
25 if (repositoryName in config.repositories) {
26 pageNumber = request.url.split('/')[2];
27 page = pages.repository(repositoryName, pageNumber);
28 if (page) {
29 response.setHeader('Content-Type', 'text/html');
30 response.writeHead(200);
31 response.write(page);
32 } else {
33 response.writeHead(404);
34 }
35 response.end();
36 return;
37 }
38 }
39 response.writeHead(404);
40 response.end();
41 return;
42}
43
44const server = http.createServer(listenerFunction);
45server.listen(config.port);
46