Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit c09ded8

Browse files
committed
Revert "clean"
This reverts commit c6992ac.
1 parent c6992ac commit c09ded8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+16420
-0
lines changed

app.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const config = require('config');
2+
3+
const createError = require('http-errors');
4+
const express = require('express');
5+
const path = require('path');
6+
const cookieParser = require('cookie-parser');
7+
const logger = require('morgan');
8+
const cors = require('cors');
9+
10+
// connect to DB
11+
const mongoose = require('mongoose');
12+
mongoose.connect(config.get('DB'), {
13+
useNewUrlParser: true,
14+
useUnifiedTopology: true
15+
}, (err, db) => {
16+
if(err) return console.error(err);
17+
console.log('DB connect');
18+
});
19+
20+
const app = express();
21+
22+
// view engine setup
23+
app.set('views', path.join(__dirname, 'views'));
24+
app.set('view engine', 'ejs');
25+
26+
// functions
27+
const ip = (req) => (req.headers['x-forwarded-for'] || req.connection.remoteAddress).replace('::ffff:', '');
28+
29+
// favicon setup
30+
31+
// app.use(logger('dev', {
32+
// skip: function (req, res) { return res.statusCode < }
33+
// }));
34+
app.use(logger('dev'));
35+
app.use(express.json());
36+
app.use(express.urlencoded({ extended: false }));
37+
app.use(cookieParser());
38+
app.use(cors());
39+
app.use(express.static(path.join(__dirname, 'public')));
40+
41+
// locals
42+
app.use((req, res, next) =>{
43+
res.locals = {
44+
...res.locals,
45+
ip: ip(req),
46+
appName: config.get('app.appName'),
47+
title: config.get('app.title'),
48+
subtitle: config.get('app.subtitle'),
49+
email: config.get('app.email'),
50+
hcaptcha: config.get('other.hcaptcha.sitekey'),
51+
GAid: config.get('other.GAid')
52+
}
53+
next();
54+
});
55+
56+
// app.use((req, res, next) => {
57+
// console.log(req.body);
58+
// next();
59+
// })
60+
//
61+
// app.use('/verify', require('./routes/verify.js'));
62+
app.use('/admin', require('./routes/admin.js'));
63+
app.use('/api', require('./routes/api.js'));
64+
app.use('/error', require('./routes/error.js'));
65+
app.use('/', require('./routes/index.js'));
66+
67+
// catch 404 and forward to error handler
68+
app.use(function(req, res, next) {
69+
next(createError(404));
70+
});
71+
72+
// error handler
73+
app.use(function(err, req, res, next) {
74+
// set locals, only providing error in development
75+
res.locals.message = err.message;
76+
res.locals.error = req.app.get('env') === 'development' ? err : {};
77+
78+
// render the error page
79+
res.status(err.status || 500);
80+
res.render('error', err);
81+
});
82+
83+
module.exports = app;

bin/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
const config = require('config');
3+
const fs = require('fs');
4+
5+
// update notice
6+
const updateNotifier = require('update-notifier');
7+
const pkg = require('../package.json');
8+
const notifier = updateNotifier({pkg});
9+
notifier.notify();
10+
notifier.update && console.log(notifier.update);
11+
12+
// config
13+
if(!fs.readdirSync('config').some(i => i.includes('local'))){
14+
console.log('tips: you can custom some variables by adding your `config/local.json`');
15+
}
16+
17+
console.group('Config:');
18+
console.table({ DB: config.get('DB')});
19+
console.table(config.get('app'));
20+
console.table({
21+
hcaptcha: !! (config.get('other.hcaptcha.sitekey') && config.get('other.hcaptcha.sitekey') ),
22+
GAid: !!config.get('other.GAid')
23+
});
24+
console.groupEnd();
25+
26+
require('./www');

bin/www

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const config = require('config');
8+
const app = require('../app');
9+
const debug = require('debug')('url-shortener:server');
10+
const http = require('http');
11+
12+
/**
13+
* Get port from environment and store in Express.
14+
*/
15+
16+
const port = normalizePort(config.PORT || '3000');
17+
app.set('port', port);
18+
19+
/**
20+
* Create HTTP server.
21+
*/
22+
23+
const server = http.createServer(app);
24+
25+
/**
26+
* Listen on provided port, on all network interfaces.
27+
*/
28+
29+
server.listen(port);
30+
server.on('error', onError);
31+
server.on('listening', onListening);
32+
33+
/**
34+
* Normalize a port into a number, string, or false.
35+
*/
36+
37+
function normalizePort(val) {
38+
const port = parseInt(val, 10);
39+
40+
if (isNaN(port)) {
41+
// named pipe
42+
return val;
43+
}
44+
45+
if (port >= 0) {
46+
// port number
47+
return port;
48+
}
49+
50+
return false;
51+
}
52+
53+
/**
54+
* Event listener for HTTP server "error" event.
55+
*/
56+
57+
function onError(error) {
58+
if (error.syscall !== 'listen') {
59+
throw error;
60+
}
61+
62+
const bind = typeof port === 'string'
63+
? 'Pipe ' + port
64+
: 'Port ' + port;
65+
66+
// handle specific listen errors with friendly messages
67+
switch (error.code) {
68+
case 'EACCES':
69+
console.error(bind + ' requires elevated privileges');
70+
process.exit(1);
71+
break;
72+
case 'EADDRINUSE':
73+
console.error(bind + ' is already in use');
74+
process.exit(1);
75+
break;
76+
default:
77+
throw error;
78+
}
79+
}
80+
81+
/**
82+
* Event listener for HTTP server "listening" event.
83+
*/
84+
85+
function onListening() {
86+
const addr = server.address();
87+
const bind = typeof addr === 'string'
88+
? 'pipe ' + addr
89+
: 'port ' + addr.port;
90+
debug('Listening on ' + bind);
91+
}

config/default.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"DB": "mongodb://127.0.0.1:27017",
3+
"PORT": 3000,
4+
"app": {
5+
"BASEURL": "http://localhost:3000",
6+
"email": "support@localhost",
7+
"appName": "AURL",
8+
"title": "URL shortenrt",
9+
"subtitle": "Powered by AURL",
10+
"backdoor": "backdoor",
11+
"admin": "admin"
12+
},
13+
"other":{
14+
"hcaptcha": {
15+
"sitekey": "",
16+
"secret": ""
17+
},
18+
"GAid": ""
19+
}
20+
}

github-social-image.png

140 KB
Loading

0 commit comments

Comments
 (0)