forked from dosyago/dn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libraryServer.js
154 lines (131 loc) · 3.18 KB
/
libraryServer.js
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
import fs from 'fs';
import path from 'path';
import express from 'express';
import args from './args.js';
import {say, sleep} from './common.js';
import Archivist from './archivist.js';
const SITE_PATH = path.resolve(__dirname, 'public');
const app = express();
const INDEX_FILE = args.index_file;
let Server, upAt, port;
const LibraryServer = {
start, stop
}
export default LibraryServer;
async function start({server_port}) {
port = server_port;
addHandlers();
Server = app.listen(Number(port), err => {
if ( err ) {
throw err;
}
upAt = new Date;
say({server_up:{upAt,port}});
});
}
function addHandlers() {
const {chrome_port} = args;
app.use(express.urlencoded({extended:true}));
app.use(express.static(SITE_PATH));
if ( !! args.library_path() ) {
app.use("/library", express.static(args.library_path()))
}
app.get('/search', async (req, res) => {
res.end('Not implemented yet');
});
app.get('/mode', async (req, res) => {
res.end(Archivist.getMode());
});
app.get('/archive_index.html', async (req, res) => {
const index = JSON.parse(fs.readFileSync(INDEX_FILE()));
res.end(IndexView(index));
});
app.post('/mode', async (req, res) => {
const {mode} = req.body;
await Archivist.changeMode(mode);
res.end(`Mode set to ${mode}`);
});
app.get('/base_path', async (req, res) => {
res.end(args.getBasePath());
});
app.post('/base_path', async (req, res) => {
const {base_path} = req.body;
const change = args.updateBasePath(base_path);
if ( change ) {
Archivist.handlePathChanged();
Server.close(async () => {
console.log(`Server closed.`);
console.log(`Waiting 1 second...`);
await sleep(1000);
await start({server_port:port});
console.log(`Server restarted.`);
});
res.end(`Base path set to ${base_path} and saved to preferences. Server restarting...`);
} else {
res.end(`Base path not changed.`);
}
});
}
async function stop() {
let resolve;
const pr = new Promise(res => resolve = res);
console.log(`Closing library server...`);
Server.close(() => {
console.log(`Library server closed.`);
resolve();
});
return pr;
}
function IndexView(urls) {
return `
<!DOCTYPE html>
<meta charset=utf-8>
<title>Your HTML Library</title>
<style>
:root {
font-family: sans-serif;
background: lavenderblush;
}
body {
display: table;
margin: 0 auto;
background: silver;
padding: 0.5em;
box-shadow: 0 1px 1px purple;
}
form {
}
fieldset {
border: thin solid purple;
}
button, input, output {
}
input.long {
width: 100%;
min-width: 250px;
}
output {
font-size: smaller;
color: purple;
}
h1 {
margin: 0;
}
h2 {
margin-top: 0;
}
</style>
<h1>22120</h1>
<h2>Internet Offline Library</h2>
<h2>Archive Index</h2>
<ul>
${
urls.map(([url,title]) => `
<li>
<a target=_blank href=${url}>${title||url}</a>
</li>
`).join('\n')
}
</ul>
`
}