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