This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·165 lines (139 loc) · 3.24 KB
/
server.js
File metadata and controls
executable file
·165 lines (139 loc) · 3.24 KB
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
/**
* 📸 PushSnapper
* – It's an URL to image service!
*/
"use strict";
const Hapi = require("hapi");
const Vision = require("vision");
const Inert = require("inert");
const puppeteer = require("puppeteer");
const { URL } = require("url");
const slugify = require("slugify");
const snapsFunc = require("./snapsFunc");
async function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Create a server with a host and port.
const server = Hapi.server({
host: "0.0.0.0",
port: process.env.PORT || 8189
});
// Add route for viewing a list of all snaps.
server.route({
method: "GET",
path: "/snaps",
handler: snapsFunc
});
// Add route for overview/start.
server.route({
method: ["GET", "POST"],
path: "/",
handler: async function(request, h) {
let url = request.query.url || "";
let basicAuthUser = request.query.basicAuthUser || null;
let basicAuthPass = request.query.basicAuthPass || null;
let width = request.query.width || 1024;
let height = request.query.height || 768;
let wait = request.query.wait || 0;
let isValidUrl = true;
let parsedUrl = null;
let getResult = null;
// Make sure URL contains http://.
if (!url.startsWith("http")) {
url = `http://${url}`;
}
try {
parsedUrl = new URL(url);
} catch (e) {
isValidUrl = false;
}
if (isValidUrl) {
let isoDateString = new Date().toISOString();
let divider = "@";
// Filename ends up to something like
// "texttv.nu-100-640x480-2018-04-14T07:49:32.384Z.png".
let filename = slugify(
`${parsedUrl.hostname}${divider}${
parsedUrl.pathname
}${divider}${width}x${height}${divider}${isoDateString}.png`
);
getResult = await getPage({
url,
width,
height,
wait,
basicAuthUser,
basicAuthPass,
filename
});
}
return h.view("index", {
isValidUrl,
getResult
});
}
});
server.route({
method: "GET",
path: "/image/{imageName}",
handler: function(request, h) {
return h.file(`./snaps/${request.params.imageName}`);
}
});
async function getPage({
url = "",
width = 1024,
height = 768,
basicAuthUser = null,
basicAuthPass = null,
filename = "snap.png",
wait = 0
}) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
width = parseInt(width);
height = parseInt(height);
page.setViewport({
width,
height
});
if (basicAuthUser && basicAuthUser) {
await page.authenticate({
username: basicAuthUser,
password: basicAuthPass
});
}
await page.goto(url);
if (wait) {
await timeout(parseInt(wait));
}
await page.screenshot({
path: `snaps/${filename}`
});
await browser.close();
return {
saved: true,
filename,
url
};
}
// Start the server
async function start() {
try {
await server.register(Vision);
await server.register(Inert);
server.views({
engines: {
html: require("handlebars")
},
relativeTo: __dirname,
path: "templates"
});
await server.start();
} catch (err) {
console.log(err);
process.exit(1);
}
console.log("Server running at:", server.info.uri);
}
start();