From def1be88038a623514276a2acdbdd33341105ff4 Mon Sep 17 00:00:00 2001 From: les Date: Fri, 9 Aug 2019 01:58:11 +0200 Subject: [PATCH] cleaning and use nuxt-express --- assets/README.md | 2 -- config/production.js | 2 +- nuxt.config.js | 6 ++--- package.json | 1 + plugins/README.md | 2 -- server/dbconfig.js | 2 +- server/federation/helpers.js | 6 ++--- server/index.js | 47 ++++++++++++++---------------------- server/routes.js | 17 +++++++++++++ store/README.md | 2 -- yarn.lock | 7 ++++++ 11 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 server/routes.js diff --git a/assets/README.md b/assets/README.md index 34766f93..24ba5cde 100644 --- a/assets/README.md +++ b/assets/README.md @@ -1,7 +1,5 @@ # ASSETS -**This directory is not required, you can delete it if you don't want to use it.** - This directory contains your un-compiled assets such as LESS, SASS, or JavaScript. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#webpacked). diff --git a/config/production.js b/config/production.js index 8448c644..2e498350 100644 --- a/config/production.js +++ b/config/production.js @@ -1,5 +1,5 @@ +// DO NOT TOUCH THIS FILE const fs = require('fs') - const config_path = process.env.config_path let config = {} diff --git a/nuxt.config.js b/nuxt.config.js index 5ea13383..d5605353 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -13,9 +13,9 @@ module.exports = { link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }] }, dev: (process.env.NODE_ENV !== 'production'), - serverMiddleware: [ - { path: '/api', handler: '~/server/api/index.js' } - ], + //serverMiddleware: [ + //{ path: '/api', handler: '~/server/api/index.js' } + //], server: conf.server, diff --git a/package.json b/package.json index 721b24a4..3cd39c03 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "multer": "^1.4.2", "node-fetch": "^2.6.0", "nuxt": "^2.8.1", + "nuxt-express-module": "^0.0.11", "pg": "^7.11.0", "sass-loader": "^7.1.0", "sequelize": "^5.12.3", diff --git a/plugins/README.md b/plugins/README.md index ca1f9d8a..f4f309ca 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -1,7 +1,5 @@ # PLUGINS -**This directory is not required, you can delete it if you don't want to use it.** - This directory contains Javascript plugins that you want to run before mounting the root Vue.js application. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/plugins). diff --git a/server/dbconfig.js b/server/dbconfig.js index e307ec3c..69ec6c05 100644 --- a/server/dbconfig.js +++ b/server/dbconfig.js @@ -1,3 +1,3 @@ +// needed by sequelize const config = require('config') - module.exports = config.db diff --git a/server/federation/helpers.js b/server/federation/helpers.js index 5fadc63e..9b14ef99 100644 --- a/server/federation/helpers.js +++ b/server/federation/helpers.js @@ -67,7 +67,7 @@ const Helpers = { // TODO: cache async getActor(url, force=false) { - // try with cache first if not forced + // try with cache first if (!force && actorCache[url]) return actorCache[url] const user = await fetch(url, { headers: {'Accept': 'application/jrd+json, application/json'} }) .then(res => { @@ -84,7 +84,7 @@ const Helpers = { // ref: https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/ async verifySignature(req, res, next) { let user = await Helpers.getActor(req.body.actor) - if (!user) return res.send('Actor not found', 401) + if (!user) return res.status(401).send('Actor not found') // little hack -> https://github.com/joyent/node-http-signature/pull/83 req.headers.authorization = 'Signature ' + req.headers.signature @@ -97,7 +97,7 @@ const Helpers = { // signature not valid, try without cache user = await Helpers.getActor(req.body.actor, true) - if (!user) return res.send('Actor not found', 401) + if (!user) return res.status(401).send('Actor not found') if (httpSignature.verifySignature(parsed, user.publicKey.publicKeyPem)) return next() // still not valid diff --git a/server/index.js b/server/index.js index 2158c70b..9e89b6e3 100644 --- a/server/index.js +++ b/server/index.js @@ -1,19 +1,18 @@ -#!/bin/env node -const path = require('path') -const express = require('express') -const consola = require('consola') -const morgan = require('morgan') +//#!/bin/env node +//const path = require('path') +//const express = require('express') +//const consola = require('consola') +//const morgan = require('morgan') const { Nuxt, Builder } = require('nuxt') -const api = require('./api') -const federation = require('./federation') -const webfinger = require('./federation/webfinger') +//const api = require('./api') +//const federation = require('./federation') +//const webfinger = require('./federation/webfinger') // Import and Set Nuxt.js options const nuxt_config = require('../nuxt.config.js') const config = require('config') -const app = express() async function start() { nuxt_config.server = config.server // Init Nuxt.js @@ -26,30 +25,31 @@ async function start() { } else { await nuxt.ready() } + nuxt.listen() // configurable favicon && logo - app.use('/favicon.ico', express.static(path.resolve(config.favicon || 'assets/favicon.ico'))) + //app.use('/favicon.ico', express.static(path.resolve(config.favicon || 'assets/favicon.ico'))) - app.use(morgan('dev')) - app.use('/media/', express.static(config.upload_path)) + //app.use(morgan('dev')) + //app.use('/media/', express.static(config.upload_path)) // gancio standard api - app.use('/api', api) + //app.use('/api', api) // federation api / activitypub / webfinger / nodeinfo - app.use('/.well-known', webfinger) - app.use('/federation', federation) + //app.use('/.well-known', webfinger) + //app.use('/federation', federation) // Give nuxt middleware to express - app.use(nuxt.render) + //app.use(nuxt.render) // Listen - const server = app.listen(nuxt_config.server) + //const server = app.listen(nuxt_config.server) // close connections/port/unix socket function shutdown() { consola.info(`Closing connections..`) - server.close(() => { + nuxt.close(() => { // TODO, close db connection? process.exit() }) @@ -57,17 +57,6 @@ async function start() { process.on('SIGTERM', shutdown) process.on('SIGINT', shutdown) - server.on('error', e => { - consola.error(e) - }) - - server.on('listening', () => { - const address = server.address() - consola.ready({ - message: `Server listening on ${(typeof address) === 'object' ? `${address.address}:${address.port}` : address}`, - badge: true - }) - }) } start() diff --git a/server/routes.js b/server/routes.js new file mode 100644 index 00000000..74a48fd0 --- /dev/null +++ b/server/routes.js @@ -0,0 +1,17 @@ +const path = require('path') +const config = require('config') +const express = require('express') +const api = require('./api') +const federation = require('./federation') +const webfinger = require('./federation/webfinger') + +const router = express.Router() +router.use('/favicon.ico', express.static(path.resolve(config.favicon || 'assets/favicon.ico'))) +router.use('/media/', express.static(config.upload_path)) +router.use('/api', api) + +// federation api / activitypub / webfinger / nodeinfo +router.use('/.well-known', webfinger) +router.use('/federation', federation) + +module.exports = router diff --git a/store/README.md b/store/README.md index 1972d277..36db9a7b 100644 --- a/store/README.md +++ b/store/README.md @@ -1,7 +1,5 @@ # STORE -**This directory is not required, you can delete it if you don't want to use it.** - This directory contains your Vuex Store files. Vuex Store option is implemented in the Nuxt.js framework. diff --git a/yarn.lock b/yarn.lock index 6d10850f..945ac84d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6787,6 +6787,13 @@ number-is-nan@^1.0.0: resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d" integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0= +nuxt-express-module@^0.0.11: + version "0.0.11" + resolved "https://registry.yarnpkg.com/nuxt-express-module/-/nuxt-express-module-0.0.11.tgz#d78375b52b8a7529bc06b110d3c81b30416f09b5" + integrity sha512-ugLUMqKhOFVt55lbpBTLrfVHxCeKCq6jqsyQcZ1+nh5eQnb8hm5T4xF+Ud0g2jzuMXLUgKM9dyIU1rv5VdMuKg== + dependencies: + express "^4.16.3" + nuxt@^2.8.1: version "2.8.1" resolved "https://registry.yarnpkg.com/nuxt/-/nuxt-2.8.1.tgz#10a307e51973fa7b55abfeb41b84c76cbdd6f2f6"