Commit f516cccf authored by Aral Balkan's avatar Aral Balkan

Merge branch 'nuxt'

Integrate Nuxt
parents aaca61c6 a014f46a
......@@ -111,4 +111,4 @@ $RECYCLE.BIN/
lib/
data/
!/src/lib/
!/server/lib/
const path = require('path')
module.exports = {
loading: {
color: '#92D3CE'
},
rootDir: path.resolve(__dirname),
dev: process.env.NODE_ENV !== 'production'
}
This diff is collapsed.
......@@ -3,7 +3,7 @@
"description": "ActivityPub implementation for federated personal web sites.",
"version": "0.0.0",
"homepage": "https://indienet.info",
"main": "src",
"main": "server",
"keywords": [
"feathers"
],
......@@ -14,7 +14,7 @@
"contributors": [],
"bugs": {},
"directories": {
"lib": "src",
"lib": "server",
"test": "test/"
},
"engines": {
......@@ -22,9 +22,11 @@
"npm": ">= 3.0.0"
},
"scripts": {
"build": "nuxt build",
"dev": "DEBUG=nuxt:* nodemon --watch server/ --watch config/ server/index.js",
"prestart": "npm run build",
"start": "NODE_ENV=production node server/",
"test": "npm run -s mocha",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"start": "nodemon src/",
"mocha": "mocha test/ --recursive --exit"
},
"dependencies": {
......@@ -36,6 +38,7 @@
"compression": "^1.7.1",
"cors": "^2.8.4",
"helmet": "^3.9.0",
"nuxt": "^1.3.0",
"serve-favicon": "^2.4.5",
"winston": "^2.4.0",
"ws": "^4.0.0"
......@@ -44,6 +47,7 @@
"eslint": "^4.16.0",
"log-suppress": "^1.0.3",
"mocha": "^4.0.1",
"nodemon": "^1.14.12",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"standard": "^10.0.3"
......
<template>
<div>
<h1>Indie Site</h1>
<p>Welcome.</p>
</div>
</template>
......@@ -28,7 +28,7 @@ app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(favicon(path.join(app.get('public'), 'favicon.ico')))
// Host the public folder
app.use('/', express.static(app.get('public')))
// app.use('/', express.static(app.get('public')))
// Set up Plugins and providers
app.configure(express.rest())
......
......@@ -2,12 +2,19 @@
const logger = require('winston')
const app = require('./app')
const port = app.get('port')
const server = app.listen(port)
process.on('unhandledRejection', (reason, p) =>
logger.error('Unhandled Rejection at: Promise ', p, reason)
)
server.on('listening', () =>
logger.info('Feathers application started on http://%s:%d', app.get('host'), port)
)
// Once Nuxt is done rendering the clinet, start the FeathersJS Server.
process.on('nuxt:build:done', (err) => {
if (err) {
logger.error(err)
}
const server = app.listen(port)
server.on('listening', () => {
logger.info(`Feathers application started on ${app.get('host')}:${port}`)
})
})
// @ts-check
// Nuxt
const { render } = require('./nuxt')
// ActivityPub
const activity = require('../lib/activity.js')
// Setup logger.
......@@ -8,12 +13,13 @@ if (process.env.NODE_ENV === 'test') {
winston.remove(winston.transports.Console)
}
module.exports = function (app) { // eslint-disable-line no-unused-vars
module.exports = function (app) {
// Custom Express middleware. The order matters
// Content types enumeration.
const ContentType = Object.freeze({
HTML: 'text/html',
JSON: 'application/json',
ActivityPub: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
AlternateActivityPub: 'application/activity+json'
})
......@@ -25,13 +31,35 @@ module.exports = function (app) { // eslint-disable-line no-unused-vars
InternalServerError: 500
})
//
// Nuxt
//
// Use Nuxt's render middleware
app.use((request, response, next) => {
// Nuxt is greedy and will try to render all types of routes, not just HTML.
// Prevent it from rendering the ones that Express and Feathers should be handling.
switch (request.accepts(ContentType.HTML, ContentType.JSON, ContentType.ActivityPub, ContentType.AlternateActivityPub)) {
case ContentType.JSON:
case ContentType.ActivityPub:
case ContentType.AlternateActivityPub: {
next()
break
}
default: {
render(request, response, next)
}
}
})
//
// ActivityPub
//
// Feathers does not support unnamed services so that currently precludes
// having a service serve the root GET to return the Actor object for.
// having a service to serve the root GET to return the Actor object for.
// (See https://github.com/feathersjs/feathers/issues/728#issuecomment-355295764)
app.get('/', function (request, response, next) {
//
// Content negotiation
//
// ActivityPub response (return Actor object for the site owner).
const activityPub = () => {
......@@ -45,30 +73,11 @@ module.exports = function (app) { // eslint-disable-line no-unused-vars
response.end(ownerActorJSON)
}
// HTML response (return placeholder client page).
const html = () => {
response.status(HTTPStatusCode.OK)
// TODO: Serve the client.
response.end(`
<!doctype html>
<meta charset='utf-8'>
<title>Your site</title>
<style>html { font-family: sans-serif; }</style>
<h1>Welcome to your federated personal web site</h1>
<p>TODO: Create client.</p>
`)
}
response.format({
// Web client index.
[ContentType.HTML]: html,
// ActivityPub has two valid content types. (§7, w3.org/TR/activitypub).
[ContentType.ActivityPub]: activityPub,
[ContentType.AlternateActivityPub]: activityPub
})
})
}
const { Nuxt, Builder } = require('nuxt')
const logger = require('winston')
const config = require('../../nuxt.config')
const nuxt = new Nuxt(config)
if (config.dev) {
new Builder(nuxt).build()
.then(() => process.emit('nuxt:build:done'))
.catch((error) => {
logger.error(error)
process.exit(1)
})
} else {
process.nextTick(() => process.emit('nuxt:build:done'))
}
module.exports = nuxt
......@@ -6,7 +6,7 @@ process.env.NODE_ENV = 'test'
const assert = require('assert')
const request = require('request-promise')
const url = require('url')
const app = require('../src/app')
const app = require('../server/app')
//
// Helpers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment