-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
192 lines (158 loc) · 4.6 KB
/
index.ts
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
185
186
187
188
189
190
191
192
import express = require("express");
import path = require("path");
import * as fs from "fs";
import http = require("http");
import bodyParser = require("body-parser");
import { app as electronApp, BrowserWindow } from "electron";
import * as routes from "./server/routes/routes";
import { HttpStatus } from "./server/types/HttpStatus";
import { ConsoleColors } from "./server/types/Utils";
import { sendErr } from "./server/functions/functions";
import { FilesHandler } from "./server/handlers/FilesHandler";
const APP_TITLE = "Ip Changer";
const app = express();
// Startup server
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
// BodyParser e CookieParser Setup
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// It makes sure that file to keep data is present
FilesHandler.ensureCreated();
// File router
// By default, static files are served from the <root>/client/dist folder
const clientDistPath = "client";
app.use(express.static(path.join(__dirname, clientDistPath)));
// Set routes router
app.use("/", routes.router);
// Catch all other routes and return the index file
// This catch all route, denoted with *, MUST come last after all other API routes have been defined
app.use("*", (req, res) => {
if (fs.existsSync(clientDistPath)) {
res.sendFile(path.join(__dirname, clientDistPath, "index.html"));
} else {
if (fs.existsSync(path.join(__dirname, "client"))) {
res.sendFile(path.join(__dirname, "client", "index.html"));
} else {
sendErr(res, HttpStatus.NotFound, { error: "Page not found" }, "Page not found");
}
}
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found");
err["status"] = 404;
next(err);
});
// ERROR HANDLERS
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use((err: any, req, res, next) => {
res.status(err["status"] || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use((err: any, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
function normalizePort(val) {
const _port = parseInt(val, 10);
if (isNaN(_port)) {
// named pipe
return val;
}
if (_port >= 0) {
// port number
return _port;
}
return false;
}
function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string"
? "Pipe " + port
: "Port " + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
const addr = server.address();
const bind = typeof addr === "string"
? "pipe " + addr
: "port " + addr.port;
console.log(ConsoleColors.BgMagenta + "%s" + ConsoleColors.Reset, " Listening on " + bind + " ");
}
// Electron setup
let win: BrowserWindow;
/**
* This function creates the window
*/
function createWindow() {
const windowStateKeeper = require("electron-window-state");
// Load the previous state with fallback to defaults
const mainWindowState = windowStateKeeper({
defaultWidth: 1366,
defaultHeight: 768
});
win = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
icon: path.join(__dirname, "icon.ico"),
title: APP_TITLE,
webPreferences: {
contextIsolation: true,
nodeIntegration: false
}
});
win.loadURL("http://localhost:" + (process.env.PORT || "3000") + "/");
win.on("closed", () => {
win = null;
});
// Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
mainWindowState.manage(win);
}
if (electronApp) {
electronApp.on("ready", createWindow);
electronApp.on("window-all-closed", () => {
// macOs specific close process
if (process.platform !== "darwin") {
electronApp.quit();
}
});
electronApp.on("activate", () => {
if (win === null) {
createWindow();
}
});
}