| 1 | const http = require('http'); |
| 2 | const fs = require('fs'); |
| 3 | const config = require('./config'); |
| 4 | const pathFix = require('./pathFix'); |
| 5 | const template = require('./templates'); |
| 6 | const pages = require('./pages'); |
| 7 |
|
| 8 | const 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 |
|
| 44 | const server = http.createServer(listenerFunction); |
| 45 | server.listen(config.port); |
| 46 |
|