mirror of https://github.com/JoeBiellik/paste
parent
fd4be2e53b
commit
c3e8534157
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"port": 80,
|
"port": 80,
|
||||||
"db": "db/paste"
|
"db": "mongodb://db/paste"
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,62 @@
|
|||||||
var config = require('config');
|
const config = require('config');
|
||||||
var Paste = require('../models/paste');
|
const Paste = require('../models/paste');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
*view() {
|
async view(ctx) {
|
||||||
try {
|
try {
|
||||||
let paste = yield Paste.findById(this.params.id).exec();
|
let paste = await Paste.findById(ctx.params.id).exec();
|
||||||
let lang = Object.keys(this.query)[0];
|
let lang = Object.keys(ctx.query)[0];
|
||||||
|
|
||||||
if (lang) {
|
if (lang) {
|
||||||
yield this.render('highlight', {
|
await ctx.render('highlight', {
|
||||||
pretty: config.prettyHtml,
|
pretty: config.prettyHtml,
|
||||||
title: 'Paste ' + paste.id,
|
title: config.name + ' ' + paste.id,
|
||||||
paste: paste.paste,
|
paste: paste.paste,
|
||||||
lang: lang
|
lang: lang
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.type = 'text/plain';
|
ctx.type = 'text/plain';
|
||||||
this.body = paste.paste;
|
ctx.body = paste.paste;
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
this.throw('Paste Not Found', 404);
|
ctx.throw('Paste Not Found', 404);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
*create() {
|
async create(ctx) {
|
||||||
if (this.request.body.fields) {
|
if (ctx.request.body.fields) {
|
||||||
if (this.request.body.fields.paste) {
|
if (ctx.request.body.fields.paste) {
|
||||||
this.request.body.paste = this.request.body.fields.paste;
|
ctx.request.body.paste = ctx.request.body.fields.paste;
|
||||||
}
|
}
|
||||||
if (this.request.body.fields.highlight) {
|
if (ctx.request.body.fields.highlight) {
|
||||||
this.request.body.highlight = this.request.body.fields.highlight;
|
ctx.request.body.highlight = ctx.request.body.fields.highlight;
|
||||||
}
|
}
|
||||||
if (this.request.body.fields.expire) {
|
if (ctx.request.body.fields.expire) {
|
||||||
this.request.body.expire = this.request.body.fields.expire;
|
ctx.request.body.expire = ctx.request.body.fields.expire;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.request.body.expire) {
|
if (!ctx.request.body.expire) {
|
||||||
this.request.body.expire = config.expiresDefault;
|
ctx.request.body.expire = config.expiresDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
let paste = new Paste({
|
let paste = new Paste({
|
||||||
paste: this.request.body.paste,
|
paste: ctx.request.body.paste,
|
||||||
expiresAt: new Date(Date.now() + this.request.body.expire * 1000)
|
expiresAt: new Date(Date.now() + ctx.request.body.expire * 1000)
|
||||||
});
|
});
|
||||||
|
|
||||||
yield paste.save();
|
await paste.save();
|
||||||
|
|
||||||
let link = paste.id;
|
let link = paste.id;
|
||||||
|
|
||||||
if (this.request.body.highlight) {
|
if (ctx.request.body.highlight) {
|
||||||
link += '?' + this.request.body.highlight;
|
link += '?' + ctx.request.body.highlight;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(this.query).includes('redirect')) {
|
if (Object.keys(ctx.query).includes('redirect')) {
|
||||||
this.redirect(link);
|
ctx.redirect(link);
|
||||||
} else {
|
} else {
|
||||||
this.body = this.request.origin + '/' + link + '\n';
|
ctx.body = ctx.request.origin + '/' + link + '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
var config = require('config');
|
const config = require('config');
|
||||||
var util = require('util');
|
const util = require('util');
|
||||||
var mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = () => {
|
||||||
mongoose.Promise = global.Promise;
|
mongoose.Promise = global.Promise;
|
||||||
mongoose.connect(config.db);
|
|
||||||
|
|
||||||
mongoose.connection.once('open', function() {
|
|
||||||
util.log('MongoDB connection open');
|
|
||||||
});
|
|
||||||
|
|
||||||
|
mongoose.connection.once('open', util.log.bind(util, 'MongoDB connection open'));
|
||||||
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
|
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
|
||||||
|
|
||||||
|
mongoose.connect(config.db, {
|
||||||
|
useMongoClient: true
|
||||||
|
});
|
||||||
|
|
||||||
return mongoose.connection;
|
return mongoose.connection;
|
||||||
};
|
};
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,18 +1,18 @@
|
|||||||
var router = require('koa-router')();
|
const router = require('koa-router')();
|
||||||
var config = require('config');
|
const config = require('config');
|
||||||
var pastes = require('./controllers/pastes');
|
const pastes = require('./controllers/pastes');
|
||||||
|
|
||||||
router.get('/', function *() {
|
router
|
||||||
yield this.render('index', {
|
.get('/', async (ctx) => {
|
||||||
pretty: config.prettyHtml,
|
await ctx.render('index', {
|
||||||
title: 'Paste',
|
pretty: config.prettyHtml,
|
||||||
url: this.request.origin,
|
title: config.name,
|
||||||
expires: config.expires,
|
url: ctx.request.origin,
|
||||||
highlights: config.highlights
|
expires: config.expires,
|
||||||
});
|
highlights: config.highlights
|
||||||
});
|
});
|
||||||
|
})
|
||||||
router.post('/', pastes.create);
|
.post('/', pastes.create)
|
||||||
router.get('/:id', pastes.view);
|
.get('/:id', pastes.view);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
Loading…
Reference in New Issue