Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

feat(): Adds a way to display content from GitHub to the website #24

Merged
merged 1 commit into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"express": "4.16.2",
"lodash": "4.17.15",
"mailgun-js": "0.21.0",
"md-mirror": "^1.0.0",
"node-sass-middleware": "0.11.0",
"passport": "0.4.0",
"pg": "7.4.1",
Expand Down
1 change: 1 addition & 0 deletions src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const publicRoutes = {
'/contact': ['GET', 'POST'],
'/membres': ['GET'],
'/healthcheck': ['GET'],
'/md': ['GET'],
'/inscription': ['GET', 'POST']
};

Expand Down
20 changes: 20 additions & 0 deletions src/controllers/md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mdMirror = require('md-mirror')

module.exports = (req, res) => {

const mm = new mdMirror({
baseLocalUrl: '/md',
baseRemoteUrl: 'https://raw.githubusercontent.com/openfab-lab',
viewManager: (content) => {
// While caching is not available set pages to be cached 24h
res.setHeader('Cache-Control', 'public, max-age=' + (60*60*24));
res.render('../src/views/md.ejs', {
content,
user: req.user
})
}
})

return mm.render(req.url)
}

3 changes: 3 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const sassMiddleware = require('node-sass-middleware');
const autoroute = require('./libs/autoroute');
const applyAuthentication = require('./authentication');

const mdDisplay = require('./controllers/md')

const log = require('saga-logger').create({
module: module.id
});
Expand Down Expand Up @@ -63,6 +65,7 @@ module.exports = () => {
applyAuthentication(app);

app.use('/', router);
app.get('/md/*', mdDisplay);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en fait, dût à autoroute (que j'ai opensourced ici btw: https://github.com/webinmove/barabara), tu dois pas déclarer ce qui est dans le dossier controllers :

  const router = autoroute(
    express.Router,
    path.join(__dirname, './controllers'), {
      read: 'get',
      create: 'post',
      update: 'patch',
      destroy: 'delete'
    }, [
      'user'
    ]
  );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En fait pour avoir un module générique utilisable ailleurs, il est attendu que la fonction prenne en arguments req, res
Il ne faut donc pas prendre la bretelle d'autoroute !
En revanche il faudrait peut-être le ranger ailleurs que dans le dossier scanné par 'autoroute'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si tu peux injecter req et res dans autoroute ;)

tu dois faire ta méthode comme ceci :
module.exports.update = async (params, meta, req, res) => {

Les deux fonctionnent, mais si un jour tu veux tester correctement tes controllers, où (mais peut probable ici) si tu veux réutiliser la logique pour un worker ou autre, c'est plus simple ;)

Pour le fait qu'il soit dnas le même dossier pas de souci, autoroute ne fait que générer des un router express et tu overide cette route là par après avec app.get('/md/*', mdDisplay); donc pas de soucis, c'est prévu pour justement les edge case. Mais ici, c'en est pas un ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Comment puis-je indiquer à autoroute que la route de ce controller sera /md/* et non pas /md ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ou je garde d'office l'override app.get('/md/*', mdDisplay); ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oui, ok je vois, tu peux avoir des paths complets du style md/mon/path/vers.md ?

Soit tu overide, soit on rajoute une fonction all sur autouroute que tu peux du coup avoir dans ton controller et qui créera un myRoute/*, c'est pas une mauvise idée à ajouter à la lib tiens 🤔 Tu peux laisser l'override for now, je te pingerai quand la lib sera updatée ;)


app.all('*', (req, res, next) => {
next(new ManagedError('API_GENERAL', 404));
Expand Down
26 changes: 26 additions & 0 deletions src/views/md.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%- include('./layout/top', {page: 'membre'}); %>
<!-- IFRAME TRICK -->
<!--
<div class="container-fluid">
<div class="row">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" srcdoc="<html>
<head><%= '<link rel="stylesheet" href="https://unpkg.com/sakura.css/css/sakura.css" type="text/css">' %></head>
<body><%= content %></body>
</html>"></iframe>
</div>
</div>
</div>
-->

<div class="container mdcontent mt-5">
<style>.mdcontent img {
max-width: 100%
}</style>
<div class="row">
<div class="col-12">
<%- content %>
</div>
</div>
</div>
<%- include('./layout/bottom'); %>