diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-01-24 13:07:23 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-01-24 13:07:23 +0100 |
| commit | 08ac33fbd67e115f50855f310f192a4d38fcaf50 (patch) | |
| tree | 63ec4d13306e0b480fe3a21543fd2b97537808dc | |
| parent | 3ce1a44ab308f4c483541944aa777ec8aa91af2b (diff) | |
begin keyword dump endpoint, add search rebuild endpoint
| -rw-r--r-- | bucky/app/api.js | 19 | ||||
| -rw-r--r-- | bucky/app/index.js | 124 | ||||
| -rw-r--r-- | bucky/bin/build-search.js | 5 | ||||
| -rw-r--r-- | bucky/search/lexicon.js | 6 | ||||
| -rw-r--r-- | bucky/search/middleware.js | 10 | ||||
| -rw-r--r-- | index.js | 4 | ||||
| -rw-r--r-- | package-lock.json | 16 | ||||
| -rw-r--r-- | package.json | 2 |
8 files changed, 90 insertions, 96 deletions
diff --git a/bucky/app/api.js b/bucky/app/api.js index 44c4982..7a0e068 100644 --- a/bucky/app/api.js +++ b/bucky/app/api.js @@ -219,6 +219,11 @@ function route (app){ search.logQuery, search.success ) + app.get("/api/search/build", + middleware.ensureAuthenticated, + bucky.checkIsAdmin, + search.rebuild + ) /* keywords */ @@ -254,7 +259,19 @@ function route (app){ threads: res.threads, }) }) - + // app.get("/api/keyword/:keyword/full", + // middleware.ensureAuthenticated, + // bucky.ensureKeyword, + // bucky.ensureThreadsForKeyword, + // bucky.filterPrivateThreads, + // bucky.ensureFilesForThreads, + // bucky.ensureCommentsForThreads, + // function(req, res){ + // res.json({ + // keyword: res.keyword, + // threads: res.threads, + // }) + // }) /* mail */ diff --git a/bucky/app/index.js b/bucky/app/index.js index e8e8cbc..efdaf02 100644 --- a/bucky/app/index.js +++ b/bucky/app/index.js @@ -1,91 +1,41 @@ -require('dotenv').load(); -var fs = require('fs') -var app, express = require('express'); -var http = require('http'); -var https = require('https'); -var bodyParser = require('body-parser') -var cookieParser = require('cookie-parser') -var csurf = require('csurf') -var path = require('path') -var multiparty = require('multiparty') -var ejs = require('ejs') -var favicon = require('serve-favicon') -var passport = require('passport') -var sessionstore = require('sessionstore') -var session = require('express-session') -var MongoStore = require('connect-mongo')(session); -var upload = require('../util/upload') +var api = require('./api') -var app, server +var apis = {} -var federate = require('../util/federate') -var auth = require('../util/auth.js') -var middleware = require('../util/middleware.js') -var api = require('./api.js') -var pages = require('./pages.js') - -var site = module.exports = {} -site.init = function(){ - app = express() - app.enable('trust proxy'); - app.set('port', process.env.PORT || 5000) - app.use(favicon(__dirname + '../../../public/favicon.ico')) - app.use(bodyParser.json({limit: '50mb'})) - app.use(cookieParser()) - app.use(session({ - secret: 'argonauts', - proxy: true, - key: 'bucky.sid', - cookie: { - secure: process.env.NODE_ENV === 'production', - domain: '.' + process.env.HOST_NAME, - maxAge: 43200000000, - }, - store: new MongoStore({ - url: 'mongodb://localhost/buckySessionDb' -// type: 'mongodb', -// host: 'localhost', -// port: 27017, -// dbName: 'buckySessionDb', -// collectionName: 'sessions', -// timeout: 10000, - }), - resave: true, - saveUninitialized: false, - })) - - upload.init() - // federate.route(app) - -// app.use(csurf({ -// cookie: true, -// value: (req) => { req.headers['csrf-token'] } -// })) - app.disable('x-powered-by') - - auth.init() - app.use(express.query()) - app.use(passport.initialize()) - app.use(passport.session()) - - server = http.createServer(app).listen(process.env.PORT || 5000, function () { - console.log('Bucky listening at http://' + process.env.HOST_NAME + ':%s', server.address().port) - }) - - app.all('*', middleware.ensureLocals) - - api.route(app) - pages.route(app) - auth.route(app) +var xpress = { + get: () => { this.match('get', arguments) }, + post: () => { this.match('post', arguments) }, + put: () => { this.match('put', arguments) }, + delete: () => { this.match('delete', arguments) }, + match: (type, args) => { + var uri = args[0] + var cbs = args.slice(1) + apis[type] = apis[type] || {} + apis[type][uri] = cbs + }, +} - app.set('view engine', 'ejs') - app.set('views', path.join(__dirname, '../../views')) - app.use(express.static(path.join(__dirname, '../../public'))) - - // rebuild javascript after restarting server - if (process.env.NODE_ENV === 'production') { - require('../bin/build-scripts') - } +var router = module.exports = { + get: (uri) => { router.match('get', uri) }, + put: (uri) => { router.match('put', uri) }, + post: (uri) => { router.match('post', uri) }, + delete: (uri) => { router.match('delete', uri) }, + match: (type, uri) => { + return (req, res, next) => { + var fns = apis[type][uri] + req.isAuthenticated = () => true + res.json = (data) => { next && next(data) } + var promises = fns.map(fn => { + return new Promise((resolve, reject) => { + newRes = { ...res } + fn(req, newRes, () => { + res = newRes + resolve(req, res) + }) + }) + }) + return Promise.all(promises) + } + }, } -site.api = require('./api') -site.pages = require('./pages') + diff --git a/bucky/bin/build-search.js b/bucky/bin/build-search.js index d417087..23657b3 100644 --- a/bucky/bin/build-search.js +++ b/bucky/bin/build-search.js @@ -1,3 +1,4 @@ -var search = require('../search/lexicon') -search.build() +var lexicon = require('../search/lexicon') + +lexicon.build().then(() => process.exit()) diff --git a/bucky/search/lexicon.js b/bucky/search/lexicon.js index 2415e81..dc1d7ab 100644 --- a/bucky/search/lexicon.js +++ b/bucky/search/lexicon.js @@ -12,9 +12,9 @@ var total = 0 module.exports = { build: build_index } -function build_index() { +function build_index(cb) { console.log("building index") - parse_threads() + return parse_threads() .then(parse_comments) .then(parse_files) .then( () => { @@ -23,7 +23,7 @@ function build_index() { console.log( "--- UNIQUE WORDS: ", unique ); lexicon_store(); console.log( "Done!") - process.exit() + return { total, unique } }) } function parse_threads() { diff --git a/bucky/search/middleware.js b/bucky/search/middleware.js index 314afbc..0cca05c 100644 --- a/bucky/search/middleware.js +++ b/bucky/search/middleware.js @@ -1,6 +1,8 @@ +var db = require('../db') + var search = require('./search') var snippet = require('./snippet') -var db = require('../db') +var lexicon = require('./lexicon') module.exports = { @@ -100,4 +102,10 @@ module.exports = { }) }, + rebuild: function(req, res, next){ + lexicon.build().then( (data) => { + res.json(data) + }) + }, + } @@ -1,2 +1,2 @@ -var app = require('./bucky/app/index') -app.init() +var site = require('./bucky/app/site') +site.init() diff --git a/package-lock.json b/package-lock.json index 16d42a4..473273b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1233,6 +1233,22 @@ } } }, + "mock-express": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/mock-express/-/mock-express-1.2.0.tgz", + "integrity": "sha512-VYRcjrz62MMKDGHH+zUt3AEuVDXruxA22s0U1UAQlmFKQAJpXW3WBJ6ld4AJeJoKB3OU0pqJni/bTL6lBxXfXA==", + "requires": { + "express": "4.16.2", + "extend": "1.3.0" + }, + "dependencies": { + "extend": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/extend/-/extend-1.3.0.tgz", + "integrity": "sha1-0VFvsP9WJNLr+RI+odrFoZlABPg=" + } + } + }, "mongodb": { "version": "2.2.33", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.33.tgz", diff --git a/package.json b/package.json index ab3390e..1e076f0 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "start": "node index", "build:search": "node bucky/bin/build-search", "build:scripts": "node bucky/bin/build-scripts", + "export:keyword": "node bucky/bin/export-keyword", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -31,6 +32,7 @@ "knex": "^0.8.6", "knox": "^0.9.2", "lodash": "^3.10.1", + "mock-express": "^1.2.0", "mongodb": "^2.2.33", "multer": "^1.0.3", "multiparty": "^4.1.2", |
