aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: 79c80853adb63fc46431b206b7eb4899675bb8c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use-strict';

const fs = require('fs');
const path = require('path');
const dirTree = require('directory-tree');
const express = require('express');
const report = require('vfile-reporter');

const hmd = require('./tohtml');

const pathMD = 'md';
const app = express();

process.on('uncaughtException', err => {
  console.error(`[${new Date()}] > Error - ${err}`);
});

const useLandScript = '';
const rawButton = '<button class="raw_button" ><div><div>Raw</div></div></button></form>';
const editButton = '<button class="raw_button" style="right: 45px;"><div><div>Edit</div></div></button></form>';

app.use(express.static('public'));
app.use('/edit', express.json());

app.get(`/${pathMD}/*`, (req, res) => {
  const url = decodeURI(req._parsedUrl.pathname);
  const {query} = req;

  console.log(`[${new Date()}] > ${200} - ${url}`);

  if (query && query.edit === 'true') {
    res.redirect(`/edit${url.slice(3)}`);
  } else if (query && query.raw === 'true') {
    res.sendFile(url, {
      root: '.',
      dotfiles: 'deny',
      headers: {
        'x-timestamp': Date.now(),
        'x-sent': true,
      },
    }, err => {
      if (err) {
        console.error(`Error: ${err}`);
      } else {
        console.log(`Sent: ${url}`);
      }
    });
    return;
  }

  fs.readFile(url.substr(1), 'utf8', (err, data) => {
    if (err) {
      return console.error(err);
    }

    hmd(data, (err, file) => {
      res.send(`${String(file) + useLandScript
      }<a href="${url}?raw=true" class="no-style">${rawButton}</a><a href="${url}?edit=true" class="no-style">${editButton}</a>`);
      console.error(report(err || file));
    });
  });
});

app.get('/data', (req, res) => {
  console.log(`[${new Date()}] > ${200} - ${req.url}`);
  res.send(dirTree(pathMD, {extensions: /\.md/}));
});

app.get('/img/*', (req, res) => {
  console.log(`[${new Date()}] > ${200} - ${req.url}`);
  const pathR = req.url;
  if (pathR === '/img/ic_info_black_48px.svg' ||
        pathR === '/imr/ic_error_black_48px.svg' ||
        pathR === '/imr/ic_good_black_48px.svg' ||
        pathR === '/imr/ic_bad_black_48px.svg' ||
        pathR === '/imr/ic_comment_black_48px.svg' ||
        pathR === '/imr/ic_help_black_48px.svg') {
    const img = fs.readFileSync(pathR);
    res.writeHead(200, {'Content-Type': 'image/svg'});
    res.end(img, 'binary');
  } else {
    const img = fs.readFileSync(req.url.replace('/img', pathR));
    res.writeHead(200, {'Content-Type': 'image/gif'});
    res.end(img, 'binary');
  }
});

app.get('/', (req, res) => {
  console.log(`[${new Date()}] > ${200} - ${req.url}`);
  fs.readFile('public/index.html', 'utf8', (err, data) => {
    if (err) {
      return console.error(err);
    }

    res.send(data);
  });
});
app.get('/edit/?*', (req, res) => {
  console.log(`[${new Date()}] > ${200} - ${req.url}`);

  const pathReq = decodeURIComponent(path.normalize(`md/${req.url.slice(5)}`));

  console.log(`[${new Date()}] > ${200} - ${pathReq}`);

  fs.readFile('public/edit.html', 'utf8', (err, d) => {
    if (err) {
      return console.error(err);
    }

    res.send(d);
  });
});
app.put('/edit/?*', (req, res) => {
  const pathR = decodeURIComponent(path.normalize(`md${req.url.slice(5)}`));
  console.log('PUT !');
  console.log(pathR);
  if (pathR.startsWith('md/public') ||
      (pathR.startsWith('md/') && req.body.password === 'renard crétin')) {
    fs.writeFile(pathR, req.body.text, err => {
      if (err) {
        res.status(404).send('Not found');
        return console.log(err);
      }
    });

    res.send('Saved !');
    return;
  }

  console.log("Send error !");
  res.status(500).send('Error');
});

app.get('*', (req, res) => {
  console.error(`[${new Date()}] > ${404} - ${req.url}`);
  fs.readFile('public/404.md', 'utf8', (err, data) => {
    if (err) {
      return console.error(err);
    }

    fs.readFile('public/css/style.css', 'utf8', (err, style) => {
      if (err) {
        let html = '<html>';
        html += '<body>';
        html += '<h1>500 - Internal Server Error</h1>';
        html += `Debug : ${err}`;
        html += '</body>';
        html += '</html>';
        res.status(500).send(html);
        return;
      }

      hmd(data, (err, file) => {
        if (err) {
          let html = '<html>';
          html += '<body>';
          html += '<h1>500 - Internal Server Error</h1>';
          html += 'Debug : 404 page can\'t be converted to HTML<br>';
          html += `Debug : ${err}`;
          html += '</body>';
          html += '</html>';
          res.status(404).send(html);
        }

        let html = '<html>';
        html += '<head>';
        html += `<style>${style}</style>`;
        html += '</head>';
        html += '<body class=\'markdown-preview\'>';
        html += String(file);
        html += '</body>';
        html += '</html>';
        res.status(404).send(html);
      });
    });
  });
});

const server = app.listen(8090, () => {
  const host = server.address().address;
  const {port} = server.address();

  console.log(`[${new Date()}] > App listening at http://%s:%s`, host, port);
});