-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'openstad-apos-v3/main-headless' into fe…
…ature/add-apostrophe-cms
- Loading branch information
Showing
283 changed files
with
47,402 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.env | ||
node_modules/ | ||
.DS_Store | ||
nginx.config.example | ||
nginx.config.example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/public/apos-frontend | ||
/data/temp | ||
/apos-build | ||
/modules/asset/ui/public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": [ | ||
"apostrophe" | ||
], | ||
"globals": { | ||
"apos": true | ||
}, | ||
"rules": { | ||
"no-var": "error", | ||
"no-console": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package-lock.json | ||
/locales | ||
npm-debug.log | ||
/data | ||
/public/uploads | ||
/public/apos-minified | ||
/data/temp/uploadfs | ||
node_modules | ||
# This folder is created on the fly and contains symlinks updated at startup (we'll come up with a Windows solution that actually copies things) | ||
/public/modules | ||
# Don't commit build files | ||
/apos-build | ||
/public/apos-frontend | ||
/modules/asset/ui/public/site.js | ||
# Don't commit masters generated on the fly at startup, these import all the rest | ||
/public/css/master-*.less | ||
.jshintrc | ||
.DS_Store | ||
/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## Helpers for scsss | ||
|
||
### Media queries | ||
``` | ||
@include media-breakpoint-up(sm) { ... } | ||
@include media-breakpoint-up(md) { ... } | ||
@include media-breakpoint-up(lg) { ... } | ||
@include media-breakpoint-up(xl) { ... } | ||
``` | ||
|
||
## Moving from ApostropheV2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) 2020 Apostrophe Technologies | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Openstad Frontend - With ApostropheCMS v3 | ||
|
||
## Intro | ||
The Openstad software allows organisation to easily build software for digital participation & democracy. | ||
|
||
The main software runs ApostropheCMS v2 for site management, this is a first draft for moving that to v3. It implements loading sites, connecting to auth & moved over a few widgets. | ||
|
||
## Installation | ||
### 1. Create a .env file | ||
``` | ||
PORT=3000 | ||
# able to set an external domain for dev & test purposed, make sure the oAuth is allowed to redirect to localhost | ||
OVERWRITE_DOMAIN= | ||
SITE_API_KEY= | ||
API_URL= | ||
# if your mongodb is not on localhost:27017 | ||
APOS_MONGODB_URI= | ||
``` | ||
### Npm i & npm run | ||
``` | ||
npm i & npm run dev | ||
``` | ||
|
||
### Crash on startup | ||
In development mode at times there is a crash after a change on startup. | ||
A restart solves that. Needs more investigation. | ||
|
||
## Moving from ApostropheV2 | ||
Moving from ApostropheCMS v2 a few things changed: | ||
|
||
- Construct is now named init | ||
- Schema fields are now structured as objects instead of an array, the name is now the key of the object | ||
- Options in a widgets are not top level elements, but nested under options key options: {label: 'Name of widget'} instead of label: 'Name of widget' | ||
- Widgets do not end in -widgets, but in -widget | ||
- Arraging is now then under key group: | ||
- less is no longer supported, have to migrate over to sass | ||
- To link directly to a static file in a module now, make sure it's in public dir and link to it as such, assuming images is a direcotry in the ui/public directory. Example: lib/modules/agenda-widget/ui/public/img/bg.png : /modules/agenda-widget/img/bg.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
require('dotenv').config(); | ||
|
||
const apostrophe = require('apostrophe'); | ||
const express = require('express'); | ||
const app = express(); | ||
const _ = require('lodash'); | ||
const apiUrl = process.env.API_URL || 'http://localhost:8111'; | ||
const projectService = require('./services/projects'); | ||
const aposConfig = require('./lib/apos-config'); | ||
const { refresh } = require('less'); | ||
const REFRESH_PROJECTS_INTERVAL = 60000 * 5; | ||
const Url = require('node:url'); | ||
|
||
let projects = {}; | ||
const apostropheServer = {}; | ||
|
||
let startUpIsBusy = false; | ||
let startUpQueue = []; | ||
|
||
async function loadProjects () { | ||
try { | ||
|
||
const allProjects = await projectService.fetchAll(); | ||
projects = {}; | ||
|
||
allProjects.forEach((project, i) => { | ||
if (!project.url) return; | ||
let url = Url.parse(project.url); | ||
console.log('Project fetched: ' + url.host); | ||
|
||
// for convenience and speed we set the domain name as the key | ||
projects[url.host] = project; | ||
}); | ||
|
||
cleanUpProjects(); | ||
|
||
} catch(err) { | ||
console.log('Error fetching projects:', err); | ||
} | ||
} | ||
|
||
// run through all projects see if anyone is not active anymore and needs to be shut down | ||
function cleanUpProjects() { | ||
const runningDomains = Object.keys(apostropheServer); | ||
|
||
if (runningDomains) { | ||
runningDomains.forEach((runningDomain) => { | ||
if (!projects[runningDomain]) { | ||
try { | ||
apostropheServer[runningDomain].apos.destroy(); | ||
} catch (e) { | ||
console.log('Error stopping project', e); | ||
} | ||
|
||
delete apostropheServer[runningDomain]; | ||
|
||
} | ||
}); | ||
} | ||
} | ||
|
||
async function doStartServer(domain, req, res) { | ||
if (!apostropheServer[domain]) { | ||
console.log('Starting up project: ', domain); | ||
apostropheServer[domain] = await run(domain, projects[domain], {}); | ||
apostropheServer[domain].app.set('trust proxy', true); | ||
apostropheServer[domain].app(req, res); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
async function run(id, projectData, options, callback) { | ||
|
||
const project = { | ||
...aposConfig, | ||
baseUrl: process.env.OVERWRITE_DOMAIN ? 'http://localhost:3000' : projectData.url, | ||
options: projectData, | ||
project: projectData, | ||
_id: id, | ||
shortName: 'openstad-' + projectData.id, | ||
mongo: {}, | ||
}; | ||
|
||
if (process.env.MONGODB_URI) { | ||
project.mongo.uri = process.env.MONGODB_URI + '/' + project.shortName; | ||
} | ||
|
||
const config = project; | ||
|
||
let assetsIdentifier; | ||
|
||
// for dev projects grab the assetsIdentifier from the first project in order to share assets | ||
|
||
if (Object.keys(apostropheServer).length > 0) { | ||
const firstProject = apostropheServer[Object.keys(apostropheServer)[0]]; | ||
// assetsIdentifier = firstProject.assets.generation; | ||
} | ||
|
||
const projectConfig = config; | ||
|
||
projectConfig.afterListen = function () { | ||
apos._id = project._id; | ||
if (callback) { | ||
return callback(null, apos); | ||
} | ||
}; | ||
|
||
const apos = apostrophe( | ||
projectConfig | ||
); | ||
|
||
return apos; | ||
} | ||
|
||
app.use(async function (req, res, next) { | ||
/** | ||
* Stop server if Project Api Key is not set. | ||
*/ | ||
if (!process.env.API_KEY) { | ||
console.error('Project api key is not set!'); | ||
if (res) { | ||
res.status(500).json({ error: 'Project api key is not set!' }); | ||
} | ||
return; | ||
} | ||
|
||
const apiUrl = process.env.INTERNAL_API_URL ? process.env.INTERNAL_API_URL : process.env.API_URL; | ||
|
||
/** | ||
* Stop server if no API URL is set | ||
*/ | ||
if (!apiUrl) { | ||
console.error('API url is not set!'); | ||
if (res) { | ||
res.status(500).json({ error: 'Api URL is not set!' }); | ||
} | ||
return; | ||
} | ||
|
||
/** | ||
* If projects are not loaded fetch from API | ||
* | ||
* All we be loaded in memory and refreshed every few minutes | ||
*/ | ||
if (Object.keys(projects).length === 0) { | ||
console.log('Fetching config for all projects'); | ||
|
||
try { | ||
await loadProjects(); | ||
} catch (err) { | ||
next(err); | ||
} | ||
} | ||
|
||
if (Object.keys(projects).length === 0) { | ||
console.log('No config for projects found'); | ||
return res.status(500).json({ error: 'No projects found' }); | ||
} | ||
|
||
// format domain to our specification | ||
let domain = req.headers['x-forwarded-host'] || req.get('host'); | ||
domain = domain.replace([ 'http://', 'https://' ], [ '' ]); | ||
domain = domain.replace([ 'www' ], [ '' ]); | ||
|
||
req.openstadDomain = domain; | ||
|
||
next(); | ||
}); | ||
|
||
app.use('/config-reset', async function (req, res, next) { | ||
await loadProjects(); | ||
next(); | ||
}); | ||
|
||
app.use('/login', function (req, res, next) { | ||
const domainAndPath = req.openstadDomain; | ||
const i = req.url.indexOf('?'); | ||
const query = req.url.substr(i + 1); | ||
const protocol = req.headers['x-forwarded-proto'] || req.protocol; | ||
const url = protocol + '://' + domainAndPath + '/auth/login'; | ||
|
||
return res.redirect(url && query ? url + '?' + query : url); | ||
}); | ||
|
||
app.get('/auth/login', (req, res, next) => { | ||
// check in url if returnTo params is set for redirecting to page | ||
// req.session.returnTo = req.query.returnTo ? decodeURIComponent(req.query.returnTo) : null; | ||
let projectDomain = process.env.OVERWRITE_DOMAIN ? process.env.OVERWRITE_DOMAIN : req.openstadDomain; | ||
const project = (projects[projectDomain] ? projects[projectDomain] : false); | ||
//const project = projects[0]; | ||
|
||
// req.session.save(() => { | ||
const thisHost = req.headers['x-forwarded-host'] || req.get('host'); | ||
const protocol = req.headers['x-forwarded-proto'] || req.protocol; | ||
let returnUrl = protocol + '://' + thisHost; | ||
|
||
if (req.query.returnTo && typeof req.query.returnTo === 'string') { | ||
// only get the pathname to prevent external redirects | ||
let pathToReturnTo = Url.parse(req.query.returnTo, true); | ||
pathToReturnTo = pathToReturnTo.path; | ||
returnUrl = returnUrl + pathToReturnTo; | ||
} | ||
|
||
returnUrl = encodeURIComponent(returnUrl + '?logintoken=[[jwt]]'); | ||
|
||
let url = `${apiUrl}/auth/project/${project.id}/login?redirectUri=${returnUrl}`; | ||
url = req.query.useOauth ? url + '&useOauth=' + req.query.useOauth : url; | ||
url = req.query.loginPriviliged ? url + '&loginPriviliged=1' : url + '&forceNewLogin=1'; // ; | ||
|
||
return res.redirect(url); | ||
}); | ||
|
||
app.use(async function (req, res, next) { | ||
|
||
try { | ||
|
||
// format domain to our specification | ||
let domain = req.headers['x-forwarded-host'] || req.get('host'); | ||
domain = domain.replace([ 'http://', 'https://' ], [ '' ]); | ||
domain = domain.replace([ 'www' ], [ '' ]); | ||
|
||
// for dev purposes allow overwrite domain name | ||
domain = process.env.OVERWRITE_DOMAIN ? process.env.OVERWRITE_DOMAIN : domain; | ||
|
||
if (!projects[domain]) { | ||
console.log('Project not found: ', domain); | ||
res.status(404).json({ error: 'Project not found' }); | ||
return; | ||
} | ||
|
||
// use existing server | ||
if (apostropheServer[domain]) { | ||
return apostropheServer[domain].app(req , res); | ||
} | ||
|
||
// busy? add to queue | ||
if (startUpIsBusy) { | ||
return startUpQueue.push([domain, req, res]); | ||
} else { | ||
startUpIsBusy = true; | ||
} | ||
|
||
await doStartServer(domain, req, res) | ||
|
||
// handle queue | ||
while (startUpQueue.length) { | ||
let nextInQueue = startUpQueue.shift(); | ||
let [ nextDomain, nextReq, nextRes ] = nextInQueue; | ||
if (apostropheServer[nextDomain]) { | ||
apostropheServer[nextDomain].app(nextReq, nextRes); | ||
} else { | ||
doStartServer(...nextInQueue); | ||
} | ||
} | ||
|
||
startUpIsBusy = false; | ||
|
||
} catch (e) { | ||
console.log('Error starting up project: ', domain, e); | ||
res.status(500).json({ | ||
error: 'An error occured running project ', | ||
domain | ||
}); | ||
} | ||
|
||
}); | ||
|
||
setInterval(loadProjects, REFRESH_PROJECTS_INTERVAL); | ||
|
||
app.listen(process.env.PORT || 3000); |
Oops, something went wrong.