Skip to content

Commit

Permalink
On branch dev-1
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   app.js
	modified:   lib/database.js
	modified:   lib/interchange.js
	new file:   public/stylesheets/setting.css
	new file:   routes/setting/index.js
	new file:   views/setting/index.ejs
  • Loading branch information
AnasMubarakYasin committed Jul 8, 2022
1 parent b5d9b0f commit 345ca92
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 44 deletions.
82 changes: 43 additions & 39 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const express = require("express");
const session = require("express-session");
const createError = require("http-errors");
const helmet = require("helmet");
const cookieParser = require("cookie-parser");
const loggerHttp = require("morgan");
const createHttpError = require("http-errors");
const rfs = require("rotating-file-stream");
const fs = require("fs");
const path = require("path");

const Logger = require("#root/lib/logger");
Expand All @@ -25,6 +26,8 @@ const routes_api_v1_projects = require("#routes/api/v1/projects");
const routes_api_v1_tasks = require("#routes/api/v1/tasks");
// const routesLangEnAccount = require("#routes/lang/en/account");

const routes_setting = require("#routes/setting/index");

loggerHttp.token("protocol", function (req, res) {
// @ts-ignore
return req.protocol;
Expand Down Expand Up @@ -55,6 +58,18 @@ async function init() {
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

app.set("database", db.connected);
app.set(
"storage",
await fs.promises
.access(path.join(env.PWD, "storage"), fs.constants.F_OK)
.then(
() => true,
() => false
)
);
app.set("environment", !!env.SERVER_HAS_ENV);

app.use(limitter);
app.use(
loggerHttp(env.LOG_STDOUT ? "dev" : env.LOG_FORMAT, {
Expand All @@ -66,51 +81,40 @@ async function init() {
}),
})
);
// app.use(express.json());
// app.use(express.urlencoded({ extended: false }));
// app.use(cookieParser());
// app.use(
// session({
// secret: crypto.randomBytes(48).toString("hex"),
// name: "sessionId",
// resave: true,
// saveUninitialized: true,
// })
// );
// app.use(helmet);

// @ts-ignore
app.use(helmet());
app.use("lang", cookieParser());
app.use(
"lang",
session({
secret: "secret",
name: "sessionId",
resave: true,
saveUninitialized: true,
})
);
app.use("/public", express.static(path.join(__dirname, "public")));
app.use("/resources", express.static(path.join(__dirname, "storage")));

app.use("/api/v1", express.json(), express.urlencoded({ extended: true }));
app.use("/setting", await routes_setting(app));

app.use("/api", express.json(), express.urlencoded({ extended: true }));
app.use("/api/v1/members", await routes_api_v1_members(app));
app.use("/api/v1/projects", await routes_api_v1_projects(app));
app.use("/api/v1/tasks", await routes_api_v1_tasks(app));
app.use(
"/api/v1/",
Interchange.not_found_middle(),
Interchange.error_middle()
);
app.use("/api", Interchange.not_found_middle(), Interchange.error_middle());

// app.use(function (req, res, next) {
// next(new createError.NotFound());
// });

// app.use(function (err, req, res, next) {
// console.log("cause", err.cause);
// console.log("expose", err.expose);
// console.log("headers", err.headers);
// console.log("message", err.message);
// console.log("name", err.name);
// console.log("stack", err.stack);
// console.log("status", err.status);
// console.log("statusCode", err.statusCode);

// res.locals.message = err.message;
// res.locals.error = req.app.get("env") === "development" ? err : {};

// res.status(err.status || 500);
// res.render("error");
// });
app.use(function (req, res, nx) {
nx(createHttpError(404));
});
app.use(function (err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};

res.status(err.status || 500);
res.render("error");
});

return app;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = class Database {
* @type {Logger}
*/
logger;
connected = false;
/**
*
* @param {NSDatabase.Options} opts
Expand Down Expand Up @@ -78,8 +79,10 @@ module.exports = class Database {
this.logger.profile("Connection has been established successfully.");
await this.connection.authenticate();
this.logger.profile("Connection has been established successfully.");
this.connected = true;
} catch (error) {
this.logger.error("Unable to connect to the database");
this.connected = false;
}
}
async close() {
Expand Down
13 changes: 8 additions & 5 deletions lib/interchange.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const createHttpError = require("http-errors");
const Logger = require("./logger");
const chalk = require("chalk");

/**
* @namespace NSInterchange
Expand All @@ -17,20 +18,23 @@ const Logger = require("./logger");
module.exports = class Interchange {
static Error = createHttpError;
/**
* @deprecated use instance instead
* @param {import('express').Response} response
* @param {import('http-errors').HttpError} error
*/
static error(response, error) {
response.status(error.status).send(error);
}
/**
* @deprecated use instance instead
* @param {import('express').Response} response
* @param {Object} data
*/
static ok(response, data) {
response.status(200).set("content-type", "application/json").send(data);
}
/**
* @deprecated use instance instead
* @param {import('express').Response} response
* @param {Object} data
*/
Expand All @@ -51,7 +55,7 @@ module.exports = class Interchange {
*/
static error_middle() {
return function (error, request, response, next) {
console.log("[static error_middle]", error);
console.log(chalk.red("[static error_middle]"), error);
// console.log("error_middle");
// console.log("cause", error.cause);
// console.log("expose", error.expose);
Expand Down Expand Up @@ -128,10 +132,7 @@ module.exports = class Interchange {
this.logger.warn(`${response.req.originalUrl} response already sent`);
return response;
}
return response
.status(status)
.json(this.on_response(data))
.end();
return response.status(status).json(this.on_response(data)).end();
}
/**
* @param {number} status
Expand All @@ -149,6 +150,7 @@ module.exports = class Interchange {
}
on_response = (data) => data;
/**
* @deprecated use static instead
* @return {import('express').RequestHandler}
*/
not_found_middle() {
Expand All @@ -157,6 +159,7 @@ module.exports = class Interchange {
};
}
/**
* @deprecated use static instead
* @return {import('express').ErrorRequestHandler}
*/
error_middle() {
Expand Down
141 changes: 141 additions & 0 deletions public/stylesheets/setting.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
html {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

body {
display: grid;
gap: 16px;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
margin: 0;
padding: 0;
}

#s-sys {
display: grid;
gap: 8px;
}

#s-sys form {
display: grid;
gap: 8px;
}

#s-sys .key-val {
display: flex;
gap: 8px;
align-items: center;
padding: 4px 8px;
}

#s-req {
display: grid;
gap: 8px;
}

#s-req form {
display: grid;
gap: 8px;
}

#s-req .key-val {
display: flex;
gap: 8px;
align-items: center;
padding: 4px 8px;
}

#s-env {
display: grid;
gap: 8px;
}

#s-env form {
display: grid;
gap: 8px;
}

.env-key {
padding: 0;
}

.env-key input {
font-size: 1rem;
width: -webkit-fill-available;
width: calc(100% - 16px);
margin: 0;
padding: 4px 8px;
border: none;
}

table,
th,
td {
border: 1px solid;
}

table {
width: 100%;
/* max-width: 400px; */
/* height: 240px; */
/* margin: 0 auto; */
/* display: block; */
/* overflow: auto; */
border-spacing: 0;
border-radius: 4px;
}

tbody {
white-space: nowrap;
}

th,
td {
padding: 4px 8px;
border-top-width: 0;
border-left-width: 0;
}

th {
position: sticky;
top: 0;
background: #fff;
vertical-align: middle;
}

th:last-child,
td:last-child {
border-right-width: 0;
}

tr:last-child td {
border-bottom-width: 0;
}

tr:hover {
background-color: #efefef;
}

input:hover {
background-color: #efefef;
}

button {
padding: 4px 8px;
margin: 0;
font-size: 14px;
border-radius: 4px;
border: 1px solid;
}

button:hover {
filter: brightness(0.9);
}
33 changes: 33 additions & 0 deletions routes/setting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const express = require("express");
const router = express.Router();

/**
*
* @param {import('express').Application} app
* @returns
*/
module.exports = async function (app) {
router.route("/").get(function (req, res, nx) {
const sys = {
node_ver: process.version,
platform: process.platform,
uptime: process.uptime(),
cpuUsage: process.cpuUsage(),
resourceUsage: process.resourceUsage(),
memoryUsage: process.memoryUsage(),
hrtime: process.hrtime(),
cwd: process.cwd(),
pid: process.pid,
};
const reqs = {
database: app.get("database"),
storage: app.get("storage"),
environment: app.get("environment"),
};
const env = require("dotenv-expand").expand(require("dotenv").config());

res.render("setting", { env: env.parsed, sys, reqs });
});

return router;
};
Loading

0 comments on commit 345ca92

Please sign in to comment.