-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (78 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express');
const app = express();
const log = require('tracer').colorConsole();
const github = require('./github');
const compression = require('compression');
const showdown = require('showdown');
const converter = new showdown.Converter();
converter.setFlavor('github');
converter.setOption('simpleLineBreaks', false);
const _ = require('lodash');
const fs = require('fs');
const {PORT} = require('./config');
const postcssMiddleware = require('postcss-middleware');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const path = require('path');
app.set('view engine', 'pug');
app.use('/websockets', express.static('public'));
app.use(compression());
app.use('/websockets/css', postcssMiddleware({
src: req => path.join(__dirname, 'public', req.path),
plugins: [autoprefixer(), cssnano()],
}));
let repos = [];
github(r => {
repos = r;
fs.writeFile("cache.json", JSON.stringify(r), error => {
if (error) {
return log.error(error);
}
log.info("Cache has been saved");
});
});
try {
repos = require('./cache.json');
} catch (e) {
log.debug('No cache set, waiting for requests to finish');
}
app.get('/websockets', function(req, res) {
const data = _.chain(repos)
.groupBy('language')
.mapValues(repos => _.orderBy(repos, ['stars'], ['desc']))
.value();
res.render('index', {data});
});
app.get('/websockets/:owner/:name', function(req, res) {
const owner = req.params.owner.toLowerCase();
const name = req.params.name.toLowerCase();
for (const repo of repos) {
if (
repo.name.toLowerCase() === name &&
repo.owner.login.toLowerCase() === owner
) {
return res.render('repository', {
repo,
readme: converter.makeHtml(repo.readme)
});
}
}
res.status(404).send();
});
app.get('/websockets/:language', function(req, res) {
const language = req.params.language;
const languageRepos = [];
for (const repo of repos) {
if (repo.language.toLowerCase() === language.toLowerCase()) {
languageRepos.push(repo);
}
}
if (languageRepos.length > 0) {
return res.render('language', {
repos: languageRepos, language: languageRepos[0].language
});
}
res.status(404).send();
});
app.listen(PORT);
log.info(`Server is running on port ${PORT}. Visit http://localhost:${PORT}/websockets`);