-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (179 loc) · 5.49 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
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
193
194
195
196
197
198
199
200
//MOdulos
const express = require("express");
const handlebars = require("express-handlebars");
const Handlebars = require("handlebars");
const {
allowInsecurePrototypeAccess,
} = require("@handlebars/allow-prototype-access");
const bodyParser = require("body-parser");
const session = require("express-session");
const flash = require("connect-flash");
const path = require("path");
const passport = require("passport");
require("./config/Auth")(passport);
const admin = require("./routes/admin");
const guest = require("./routes/guest");
//Cron
const { result } = require("./helpers/result");
const cron = require("node-cron");
// Banco de Dados
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
//Cron Result
cron.schedule(process.env.CRON_TIME, () => result());
const hbs = handlebars.create({
defaultLayout: "main",
extname: "hbs",
handlebars: allowInsecurePrototypeAccess(Handlebars),
helpers: {
rendervoto: (data, eleicaoID) => {
if (data.numero == 100) {
return new Handlebars.SafeString(
'<br><div class="teste btn" style="width: auto;"><time><h4 style="margin: 10px">' +
data.nome +
'</h4><h1></h1></time><p></p><input type="radio" name="' +
eleicaoID +
'" value="' +
data._id +
'"></div>'
);
} else if (data.numero == 200) {
return new Handlebars.SafeString(
'<div class="teste btn" style="width: auto;"><time><h4 style="margin: 10px">' +
data.nome +
'</h4><h1></h1></time><p></p><input type="radio" name="' +
eleicaoID +
'" value="' +
data._id +
'"></div>'
);
} else if (data.numero > 10) {
return new Handlebars.SafeString(
'<div class="teste btn" style="display: inline-grid;">' +
data.nome +
'</p><input type="radio" name="' +
eleicaoID +
'" value="' +
data._id +
'"></div>'
);
} else if (data.candidatos[2] != undefined) {
return new Handlebars.SafeString(
'<div class="teste btn"><time><h4>Chapa</h4><h1>' +
data.numero +
'</h1></time><div class="row" style="display: inline-grid; width: inherit;"><p style="white-space: normal;">' +
data.nome +
'</p><p style="white-space: normal; font-size: 1.1rem; text-align: left; padding-left: 7%;"><b>' +
data.candidatos[0].cargo +
": " +
data.candidatos[0].nome +
"<br>" +
data.candidatos[1].cargo +
": " +
data.candidatos[1].nome +
"<br>" +
data.candidatos[2].cargo +
": " +
data.candidatos[2].nome +
'</b></p></div><input type="radio" name="' +
eleicaoID +
'" value="' +
data._id +
'"></div>'
);
} else {
return new Handlebars.SafeString(
'<div class="teste btn"><time><h4>Chapa</h4><h1>' +
data.numero +
'</h1></time><div class="row" style="display: inline-grid; width: inherit;"><p style="white-space: normal;">' +
data.nome +
'</p><p style="white-space: normal; font-size: 1.1rem; text-align: left; padding-left: 7%;"><b>' +
data.candidatos[0].cargo +
": " +
data.candidatos[0].nome +
"<br>" +
data.candidatos[1].cargo +
": " +
data.candidatos[1].nome +
'</b></p></div><input type="radio" name="' +
eleicaoID +
'" value="' +
data._id +
'"></div>'
);
}
},
},
});
//Configurações
//session
app.use(
session({
secret: "aplicativovotta",
resave: true,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
//middleware
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
res.locals.user = req.user || null;
res.locals.StartTime = +new Date(process.env.START_TIME);
res.locals.EndTime = +new Date(process.env.END_TIME);
next();
});
//body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//IP
app.set("trust proxy", true);
//Handlebars
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
app.set("views", "views");
// app.engine('handlebars', handlebars({defaultLayout: 'main'}))
// app.set('view engine', 'handlebars');
//mongoose
mongoose.Promise = global.Promise;
// mongoose.connect('mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@ttayson.cf/'+process.env.DB_NAME, {
mongoose
.connect(
"mongodb://" +
process.env.DB_USER +
":" +
process.env.DB_PASS +
"@" +
process.env.DOMAIN +
":" +
process.env.DB_PORT +
"/" +
process.env.DB_NAME +
"?authSource=admin",
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
}
)
.then(() => {
console.log("Mongo Conectado");
})
.catch((err) => {
console.log("Erro ao se conectar ao banco:" + err);
});
//Public
app.use(express.static(path.join(__dirname, "public")));
// Rotas
app.use("/admin", admin);
app.use("/", guest);
const PORT = 3000;
app.listen(PORT, () => {
console.log("Servidor Rodando!!");
});