feature: handle multiple configs

This commit is contained in:
Jean-Baptiste Pasquier 2020-12-14 11:24:35 +01:00
parent 7b87f436a0
commit deb3b25520
No known key found for this signature in database
GPG Key ID: CC04B91B949C163A
7 changed files with 169 additions and 116 deletions

23
.gitignore vendored
View File

@ -1,13 +1,22 @@
**/node_modules **/node_modules
config.json
.DS_Store .DS_Store
*.iml *.iml
*.swp *.swp
dist .npm
# Test cache
cache
cypress/screenshots cypress/screenshots
cypress/videos cypress/videos
cache
.npm # Built files
.DS_Store .cache
src/manifest.webmanifest dist
.cache
# Config specific files
config.json
config.*.json
!config.sample.json
# PWA Generated file
src/manifest.webmanifest

101
README.md
View File

@ -71,6 +71,22 @@ Serve, watch files & rebuild on change with this command:
npm run watch npm run watch
``` ```
### Multiple config.json
You can have as many `config.*.json` as you need.
Watch on a custom config file:
```bash
CONFIG_PATH='config.customName.json' npm run watch
```
Build with a custom config file:
```bash
CONFIG_PATH='config.customName.json' npm run build
```
## Mandatory modules ## Mandatory modules
By default, a Hubl includes only individual chat modules. By default, a Hubl includes only individual chat modules.
@ -86,9 +102,14 @@ On `config.json`:
"clientLogo": "/images/logo.webp", "clientLogo": "/images/logo.webp",
"authority": "http://localhost:8000/", "authority": "http://localhost:8000/",
"endpoints": { "endpoints": {
"skills": "http://server.url/skills/", "get": {
"users": "http://server.url/users/", "skills": "http://server.url/skills/",
"uploads": "http://server.url/upload/" "users": "http://server.url/users/"
},
"post": {
"users": "http://server.url/users/",
"uploads": "http://server.url/upload/"
}
} }
} }
``` ```
@ -99,9 +120,9 @@ Where:
* `clientLogo` is an URL to an image file * `clientLogo` is an URL to an image file
* `xmppWebsocket` is your [Prosody](https://prosody.im/) with [appropriate modules](https://git.startinblox.com/infra/prosody-modules/) configured on. * `xmppWebsocket` is your [Prosody](https://prosody.im/) with [appropriate modules](https://git.startinblox.com/infra/prosody-modules/) configured on.
* `authority` is the OpenID Provider. Usually, if you use `djangoldp-account` it's the same as your djangoldp server. * `authority` is the OpenID Provider. Usually, if you use `djangoldp-account` it's the same as your djangoldp server.
* `endpoints.users` is the API endpoints for Users on your djangoldp server. (djangoldp-account) * `endpoints.*.users` is the API endpoints for Users on your djangoldp server. (djangoldp-account)
* `endpoints.skills` is the API endpoints for Skills on your djangoldp server. (djangoldp-skill) * `endpoints.*.skills` is the API endpoints for Skills on your djangoldp server. (djangoldp-skill)
* `endpoints.uploads` is the API endpoints for Uploads on your djangoldp server. (djangoldp-upload) * `endpoints.*.uploads` is the API endpoints for Uploads on your djangoldp server. (djangoldp-upload)
### Communities ### Communities
@ -157,7 +178,12 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"circle": "http://server.url/circles/" "get": {
"circle": "http://server.url/circles/"
},
"post": {
"circle": "http://server.url/circles/"
},
} }
``` ```
@ -171,7 +197,9 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"dashboard": "http://server.url/dashboard/" "get": {
"dashboard": "http://server.url/dashboard/"
}
} }
``` ```
@ -187,9 +215,9 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"joboffers": "http://server.url/job-offers/", "get": {
"skills": "http://server.url/skills/", "joboffers": "http://server.url/job-offers/"
"uploads": "http://server.url/upload/" }
} }
``` ```
@ -203,10 +231,12 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"projects": "http://server.url/projects/", "get": {
"customers": "http://server.url/customers/", "projects": "http://server.url/projects/"
"businessproviders": "http://server.url/businessproviders/", },
"skills": "http://server.url/skills/" "post": {
"projects": "http://server.url/projects/"
}
} }
``` ```
@ -219,12 +249,7 @@ On Server: `djangoldp_skill`, `djangoldp_upload` packages
On `config.json`: On `config.json`:
```json ```json
"publicDirectory": true, "publicDirectory": true
"endpoints": {
"groups": "http://server.url/groups/",
"skills": "http://server.url/skills/",
"uploads": "http://server.url/upload/"
}
``` ```
## Optional community modules ## Optional community modules
@ -241,9 +266,14 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"events":"http://server.url/events/", "get": {
"typeevents":"http://server.url/typeevents/", "events":"http://server.url/events/",
"uploads": "http://server.url/upload/" "typeevents":"http://server.url/typeevents/"
},
"post": {
"events":"http://server.url/events/",
"typeevents":"http://server.url/typeevents/"
}
} }
``` ```
@ -259,10 +289,16 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"resources":"http://server.url/resources/", "get": {
"resourceskeywords":"http://server.url/keywords/", "resources":"http://server.url/resources/",
"resourcestypes":"http://server.url/types/", "resourceskeywords":"http://server.url/keywords/",
"uploads": "http://server.url/upload/" "resourcestypes":"http://server.url/types/"
},
"post": {
"resources":"http://server.url/resources/",
"resourceskeywords":"http://server.url/keywords/",
"resourcestypes":"http://server.url/types/"
}
} }
``` ```
@ -276,7 +312,12 @@ On `config.json`:
```json ```json
"endpoints": { "endpoints": {
"polls":"http://server.url/polls/" "get": {
"polls":"http://server.url/polls/"
},
"post": {
"polls":"http://server.url/polls/"
}
} }
``` ```
@ -316,4 +357,4 @@ Did you properly created subscriptions on your DjangoLDP's server? You can quick
## Built With ## Built With
* [Sib-Core](https://git.startinblox.com/framework/sib-core/) - A SOLID-Compliant framework * [Sib-Core](https://git.startinblox.com/framework/sib-core/) - A SOLID-Compliant framework

View File

@ -6,13 +6,11 @@
"endpoints": { "endpoints": {
"get": { "get": {
"skills": "http://localhost:8000/skills/", "skills": "http://localhost:8000/skills/",
"users": "http://localhost:8000/users/", "users": "http://localhost:8000/users/"
"groups": "http://localhost:8000/groups/"
}, },
"post": { "post": {
"skills": "http://localhost:8000/skills/",
"users": "http://localhost:8000/users/", "users": "http://localhost:8000/users/",
"groups": "http://localhost:8000/groups/" "upload": "http://localhost:8000/upload/"
} }
} }
} }

View File

@ -2,7 +2,7 @@ const HTMLAsset = require('parcel-bundler/lib/assets/HTMLAsset')
function shouldIgnore (file) { function shouldIgnore (file) {
// Ignore img(src="${...}") on pug & keep the components folder pristine // Ignore img(src="${...}") on pug & keep the components folder pristine
return /\${.+}/.test(file) || /components/.test(file); return /\${.+}/.test(file) || /components/.test(file) || /\/lib\/solid-/.test(file);
} }
class SkipStartinbloxWidgetAsset extends HTMLAsset { class SkipStartinbloxWidgetAsset extends HTMLAsset {

View File

@ -26,12 +26,14 @@ const options = {
}; };
(async function() { (async function() {
if(!fs.existsSync("config.json")) throw "[Error] (Mandatory) Missing config.json file"; let configPath = process.env.CONFIG_PATH || 'config.json';
if(!fs.existsSync(configPath)) throw `[Error] (Mandatory) Missing ${configPath} file`;
console.log(`Using ${configPath} config file`);
let config = JSON.parse(fs.readFileSync('config.json')); let config = JSON.parse(fs.readFileSync(configPath));
if(!config.clientName) throw "[Error] (Mandatory) Missing clientName on config.json"; if(!config.clientName) throw `[Error] (Mandatory) Missing clientName on ${configPath}`;
if(!config.clientLogo) throw "[Error] (Mandatory) Missing clientLogo on config.json"; if(!config.clientLogo) throw `[Error] (Mandatory) Missing clientLogo on ${configPath}`;
let manifest = { let manifest = {
"lang": "fr", "lang": "fr",

View File

@ -1,4 +1,5 @@
const config = require("../config.json"); let configPath = process.env.CONFIG_PATH || 'config.json';
const config = require(`../${configPath}`);
module.exports = { module.exports = {
locals: config locals: config

140
src/sw.js
View File

@ -30,84 +30,86 @@ self.addEventListener('activate', function (e) {
self.clients.claim(); self.clients.claim();
}); });
self.addEventListener('fetch', function (event) { if(process.env.NODE_ENV === 'production'){
let requestURL = new URL(event.request.url); self.addEventListener('fetch', function (event) {
if (requestURL.origin == location.origin) { let requestURL = new URL(event.request.url);
// Static asset, cache then network if (requestURL.origin == location.origin) {
event.respondWith( // Static asset, cache then network
caches.open(CACHE_NAME).then(function (cache) { event.respondWith(
return cache.match(event.request).then(function (response) { caches.open(CACHE_NAME).then(function (cache) {
var fetchPromise = fetch(event.request).then(function (networkResponse) { return cache.match(event.request).then(function (response) {
cache.put(event.request, networkResponse.clone()); var fetchPromise = fetch(event.request).then(function (networkResponse) {
return networkResponse; cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
}); });
return response || fetchPromise; }),
}); );
}),
);
} else {
if (
event.request.method == 'POST' ||
event.request.method == 'PUT'
) {
// disabled: lead to cors errors
// // POST/PUT to api, rewrite the cache
// event.respondWith(
// caches.open(CACHE_NAME + '-api').then(function (cache) {
// return fetch(event.request).then(function (response) {
// cache.put(event.request, response.clone());
// return response;
// })
// }));
// api: no cache
event.respondWith(fetch(event.request));
} else if (
/matomo/.test(requestURL.origin) ||
/sentry/.test(requestURL.origin) ||
/jabber/.test(requestURL.origin) ||
/xmpp/.test(requestURL.origin)
) {
// analytics, always distant
event.respondWith(fetch(event.request));
} else { } else {
if ( if (
/unpkg/.test(requestURL.origin) || event.request.method == 'POST' ||
/skypack/.test(requestURL.origin) || event.request.method == 'PUT'
/jspm/.test(requestURL.origin) ||
/jsdeliver/.test(requestURL.origin) ||
/cdn/.test(requestURL.origin) ||
/googleapis/.test(requestURL.origin)
) { ) {
// cdn: cache then network
event.respondWith(
caches.open(CACHE_NAME + '-cdn').then(function (cache) {
return cache.match(event.request).then(function (response) {
var fetchPromise = fetch(event.request).then(function (networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
});
}),
);
} else {
// disabled: lead to cors errors // disabled: lead to cors errors
// // api: distant then cache // // POST/PUT to api, rewrite the cache
// event.respondWith( // event.respondWith(
// fetch(event.request) // caches.open(CACHE_NAME + '-api').then(function (cache) {
// .then((response) => { // return fetch(event.request).then(function (response) {
// caches.open(CACHE_NAME + '-api').then(function (cache) {
// cache.put(event.request, response.clone()); // cache.put(event.request, response.clone());
// return response; // return response;
// }); // })
// }) // }));
// .catch(() => {
// return caches.match(event.request);
// })
// );
// api: no cache // api: no cache
event.respondWith(fetch(event.request)); event.respondWith(fetch(event.request));
} else if (
/matomo/.test(requestURL.origin) ||
/sentry/.test(requestURL.origin) ||
/jabber/.test(requestURL.origin) ||
/xmpp/.test(requestURL.origin)
) {
// analytics, always distant
event.respondWith(fetch(event.request));
} else {
if (
/unpkg/.test(requestURL.origin) ||
/skypack/.test(requestURL.origin) ||
/jspm/.test(requestURL.origin) ||
/jsdeliver/.test(requestURL.origin) ||
/cdn/.test(requestURL.origin) ||
/googleapis/.test(requestURL.origin)
) {
// cdn: cache then network
event.respondWith(
caches.open(CACHE_NAME + '-cdn').then(function (cache) {
return cache.match(event.request).then(function (response) {
var fetchPromise = fetch(event.request).then(function (networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
});
}),
);
} else {
// disabled: lead to cors errors
// // api: distant then cache
// event.respondWith(
// fetch(event.request)
// .then((response) => {
// caches.open(CACHE_NAME + '-api').then(function (cache) {
// cache.put(event.request, response.clone());
// return response;
// });
// })
// .catch(() => {
// return caches.match(event.request);
// })
// );
// api: no cache
event.respondWith(fetch(event.request));
}
} }
} }
} });
}); }