Skip to content

Commit

Permalink
basic modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Apr 10, 2018
0 parents commit 5794ac8
Show file tree
Hide file tree
Showing 13 changed files with 1,827 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-extra-parens": 1,
"block-scoped-var": 1,
"curly": 1,
"eqeqeq": 1,
"no-global-assign": 1,
"no-implicit-globals": 1,
"no-labels": 1,
"no-multi-str": 1,
"comma-spacing": 1,
"comma-style": 1,
"func-call-spacing": 1,
"indent": 1,
"keyword-spacing": 1,
"linebreak-style": 1,
"lines-around-comment": 1,
"no-multiple-empty-lines": 1,
"space-infix-ops": 1,
"arrow-spacing": 1,
"no-var": 1,
"prefer-const": 1,
"no-unsafe-negation": 1,
"array-callback-return": 1,
"dot-location": 1,
"dot-notation": 1,
"no-eval": 1,
"no-extend-native": 1,
"no-extra-label": 1,
"semi": 1,
"space-before-blocks": 1,
"space-before-function-paren": 1,
"space-in-parens": 1,
"space-unary-ops": 1,
"spaced-comment": 1,
"arrow-body-style": 1,
"arrow-parens": 1,
"no-restricted-imports": 1,
"no-duplicate-imports": 1,
"no-useless-computed-key": 1,
"no-useless-rename": 1,
"rest-spread-spacing": 1,
"no-trailing-spaces": 1,
"quotes": [1, "single"]
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
error.log
combined.log
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RSSHub

> Make RSS Great Again!
## Author

**RSSHub** © [DIYgod](https://github.com/DIYgod), Released under the [MIT](./LICENSE) License.<br>
Authored and maintained by DIYgod with help from contributors ([list](https://github.com/DIYgod/DPlayer/contributors)).

> Blog [@Anotherhome](https://www.anotherhome.net) · GitHub [@DIYgod](https://github.com/DIYgod) · Twitter [@DIYgod](https://twitter.com/DIYgod) · Telegram Channel [@awesomeDIYgod](https://t.me/awesomeDIYgod)
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const logger = require('./utils/logger');

logger.info('🍻 RSSHub start! Cheers!');

const app = express();
app.engine('art', require('express-art-template'));

app.all('*', require('./routes/all'));

app.listen(1200);
Empty file added logs/.gitkeep
Empty file.
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "RSSHub",
"version": "0.0.1",
"description": "Make RSS Great Again!",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/DIYgod/RSSHub.git"
},
"keywords": [
"RSS"
],
"author": "DIYgod",
"license": "MIT",
"bugs": {
"url": "https://github.com/DIYgod/RSSHub/issues"
},
"homepage": "https://github.com/DIYgod/RSSHub#readme",
"devDependencies": {},
"dependencies": {
"art-template": "4.12.2",
"eslint": "4.19.1",
"express": "^4.16.3",
"express-art-template": "1.0.1",
"redis": "2.8.0",
"request": "^2.85.0",
"winston": "3.0.0-rc3"
}
}
13 changes: 13 additions & 0 deletions routes/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
res.header('Content-Type', 'application/xml; charset=utf-8');

if (req.method === 'OPTIONS') {
res.send(200);
}
else {
next();
}
};
19 changes: 19 additions & 0 deletions routes/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const logger = require('../utils/logger');
const redis = require('../utils/redis');
const mix = require('../utils/mix');

module.exports = (options) => {
logger.info(`${options.req.url}, user IP: ${mix.getIp(options.req)}`);

redis.get(options.req.url, (reply) => {
if (reply) {
options.res.send(reply);
}
else {
options.getHTML((html) => {
redis.set(options.req.url, html);
options.res.send(html);
});
}
});
};
24 changes: 24 additions & 0 deletions utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const winston = require('winston');

const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));

module.exports = logger;
7 changes: 7 additions & 0 deletions utils/mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
getIp: (req) => req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress,
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
};
29 changes: 29 additions & 0 deletions utils/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const logger = require('./logger');
const redis = require('redis');
let client;
if (process.env.REDIS_PORT_6379_TCP_ADDR && process.env.REDIS_PORT_6379_TCP_PORT) {
client = redis.createClient({
host: process.env.REDIS_PORT_6379_TCP_ADDR,
port: process.env.REDIS_PORT_6379_TCP_PORT
});
}
else {
client = redis.createClient();
}

client.on('error', (err) => {
logger.error('Redis Error ' + err);
});

module.exports = {
set: (key, value, time = 5 * 60) => {
client.set(key, value, 'EX', time);
logger.info('Redis Set: ' + key);
},
get: (key, callback) => {
client.get(key, (err, reply) => {
callback(reply);
});
},
client: client
};
22 changes: 22 additions & 0 deletions views/rss.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title><![CDATA[{{ title || 'RSSHub' }}]]></title>
<link>{{ link }}</link>
<description><![CDATA[{{ description }}]]></description>
<generator>RSSHub</generator>
<webMaster>[email protected]</webMaster>
<language>zh-cn</language>
<lastBuildDate>{{ lastBuildDate }}</lastBuildDate>
<ttl>300</ttl>
{{ each item }}
<item>
<title><![CDATA[{{ $value.title }}]]></title>
<description><![CDATA[{{@ $value.description }}]]></description>
{{ if $value.pubDate }}<pubDate>{{ $value.pubDate }}</pubDate>{{ /if }}
<guid>{{ $value.link }}</guid>
<link>{{ $value.link }}</link>
</item>
{{ /each }}
</channel>
</rss>
Loading

0 comments on commit 5794ac8

Please sign in to comment.