forked from tari-project/tari-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (96 loc) · 2.98 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
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const asciichart = require("asciichart");
var indexRouter = require("./routes/index");
var blocksRouter = require("./routes/blocks");
var mempoolRouter = require("./routes/mempool");
var searchRouter = require("./routes/search");
var assetsRouter = require("./routes/assets");
var validatorRouter = require("./routes/validator");
var hbs = require("hbs");
const [hex, script] = require("./script");
hbs.registerHelper("hex", hex);
hbs.registerHelper("script", script);
hbs.registerHelper("json", function (obj) {
return Buffer.from(JSON.stringify(obj)).toString("base64");
});
hbs.registerHelper("timestamp", function (timestamp) {
var dateObj = new Date(timestamp.seconds * 1000);
const day = dateObj.getUTCDate();
const month = dateObj.getUTCMonth() + 1;
const year = dateObj.getUTCFullYear();
const hours = dateObj.getUTCHours();
const minutes = dateObj.getUTCMinutes();
const seconds = dateObj.getSeconds();
return (
year.toString() +
"-" +
month.toString().padStart(2, "0") +
"-" +
day.toString().padStart(2, "0") +
" " +
hours.toString().padStart(2, "0") +
":" +
minutes.toString().padStart(2, "0") +
":" +
seconds.toString().padStart(2, "0")
);
});
hbs.registerHelper("percentbar", function (a, b) {
var percent = (a / (a + b)) * 100;
var barWidth = percent / 10;
var bar = "**********".slice(0, barWidth);
var space = "...........".slice(0, 10 - barWidth);
return bar + space + " " + parseInt(percent) + "% ";
});
hbs.registerHelper("chart", function (data, height) {
if (data.length > 0) {
return asciichart.plot(data, {
height: height,
});
} else {
return "**No data**";
}
});
hbs.registerHelper("json", function (obj) {
return JSON.stringify(obj);
});
hbs.registerPartials(path.join(__dirname, "partials"));
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
app.use(logger("dev"));
app.use(express.json());
app.use(
express.urlencoded({
extended: false,
})
);
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/blocks", blocksRouter);
app.use("/assets", assetsRouter);
app.use("/validator", validatorRouter);
app.use("/mempool", mempoolRouter);
app.use("/search", searchRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;