-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (121 loc) · 3.81 KB
/
app.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
// const autoUpdater = require('update-electron-app');
const bodyParser = require("body-parser");
// const dotenv = require('dotenv');
const express = require("express");
const favicon = require("serve-favicon");
const http = require("http");
// const i18n = require('i18n');
const path = require("path");
const session = require("express-session");
const {
app: electronApp,
clipboard,
dialog,
Menu,
nativeImage,
shell,
Tray,
} = require("electron");
const APP_PORT = process.env.APP_PORT || 10101;
const expressApp = express();
const localServer = http.createServer(expressApp);
const { craeteOrGetEncrpytedPrivateKey } = require("./utils/wallet");
const indexRouteController = require("./routes/indexRoute");
const peersRouteController = require("./routes/peerRoute");
const roomRouteController = require("./routes/roomRoute");
const gossipRouteController = require("./routes/gossipRoute");
expressApp.set("view engine", "pug");
expressApp.set("views", path.join(__dirname, "views"));
expressApp.use(express.static(path.join(__dirname, "public")));
expressApp.use(
favicon(path.join(__dirname, "public", "img/icons/favicon.ico"))
);
expressApp.use(bodyParser.json());
expressApp.use(bodyParser.urlencoded({ extended: true }));
expressApp.use(
session({
secret: "node101", // TODO: change this data/ yoksa oluştur varsa al
resave: false,
saveUninitialized: true,
})
);
expressApp.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
next();
});
expressApp.use("/", indexRouteController);
expressApp.use("/peer", peersRouteController);
expressApp.use("/room", roomRouteController);
expressApp.use("/gossip", gossipRouteController);
expressApp.all("*", (req, res) => {
return res.redirect("/");
});
const setupTrayMenu = (_) => {
const image = nativeImage.createFromPath(
path.join(__dirname, "build/icon.png")
);
const tray = Tray(image.resize({ width: 16, height: 16 }));
const menu = Menu.buildFromTemplate([
{
label: "Launch",
// click: _ => shell.openExternal(`http://localhost:${APP_PORT}/auth?app_key=${AppKey.get()}`)
},
{
label: "About",
click: (_) =>
dialog.showMessageBox({
type: "info",
message: `node101 | ${electronApp.getVersion()}`,
icon: image,
}),
},
{
type: "separator",
},
{
label: "Quit",
click: (_) => electronApp.quit(),
},
]);
tray.setContextMenu(menu);
};
if (!electronApp.requestSingleInstanceLock()) electronApp.quit();
electronApp.dock.hide();
electronApp
.on("ready", (_) => {
craeteOrGetEncrpytedPrivateKey((err, privateKey) => {
if (err) console.log(err);
localServer
.listen(APP_PORT, (_) => {
console.log(`Server is on port ${APP_PORT} and is running.`);
setupTrayMenu();
})
.on("error", (err) => {
if (err.code == "EADDRINUSE")
dialog.showMessageBoxSync({
type: "warning",
message: `Port ${APP_PORT} is already in use by another application. System restart is recommended.`,
});
else
dialog.showMessageBoxSync({
type: "error",
message: `Server could not be started: ${err}`,
});
electronApp.quit();
});
});
})
.on("open-url", (event, url) => {
dialog.showErrorBox("Welcome Back", `You arrived from: ${url}`);
})
.on("second-instance", (event, commandLine, workingDirectory) => {
dialog.showErrorBox(
"Welcome Back",
`You arrived from: ${commandLine.pop()}`
);
});
const { init } = require("./socket");
init(localServer);
const os = require("os");