Browse Source

Add source code link, basic input validation, and monospace error display

Sven Slootweg 10 months ago
parent
commit
6c43050a8c
5 changed files with 24 additions and 3 deletions
  1. 13 1
      index.js
  2. 2 1
      package.json
  3. 5 0
      public/style.css
  4. 3 1
      views/error.jsx
  5. 1 0
      views/layout.jsx

+ 13 - 1
index.js

@ -9,6 +9,7 @@ const url = require("url");
9 9
const autodiscoverClientConfiguration = require("@modular-matrix/autodiscover-client-configuration");
10 10
const bhttp = require("bhttp");
11 11
const createError = require("create-error");
12
const { ValidationError, validateValue, required, isString } = require("validatem");
12 13
13 14
let UpstreamError = createError("UpstreamError");
14 15
@ -34,6 +35,17 @@ router.get("/", (req, res) => {
34 35
});
35 36
36 37
router.get("/show-rooms", (req, res) => {
38
	validateValue(req.query, {
39
		hostname: [ required, isString, (string) => {
40
			console.log(string.length);
41
			
42
			if (string.length === 0) {
43
				throw new ValidationError("May not be empty");
44
			}
45
		} ],
46
		since: [ isString ]
47
	});
48
37 49
	return Promise.try(() => {
38 50
		return autodiscoverClientConfiguration.discover(req.query.hostname);
39 51
	}).then((clientConfiguration) => {
@ -68,7 +80,7 @@ router.get("/show-rooms", (req, res) => {
68 80
app.use(router);
69 81
70 82
app.use((error, req, res, next) => {
71
	if (error instanceof UpstreamError || error instanceof autodiscoverClientConfiguration.LookupFailed) {
83
	if (error instanceof UpstreamError || error instanceof autodiscoverClientConfiguration.LookupFailed || error instanceof ValidationError) {
72 84
		res.render("error", { error: error });
73 85
	} else {
74 86
		throw error;

+ 2 - 1
package.json

@ -15,7 +15,8 @@
15 15
    "express": "^4.17.1",
16 16
    "express-promise-router": "^3.0.3",
17 17
    "react": "^16.9.0",
18
    "react-dom": "^16.9.0"
18
    "react-dom": "^16.9.0",
19
    "validatem": "^0.2.0"
19 20
  },
20 21
  "devDependencies": {
21 22
    "nodemon": "^1.19.1"

+ 5 - 0
public/style.css

@ -2,6 +2,11 @@ body {
2 2
	font-family: sans-serif;
3 3
}
4 4
5
h1 {
6
	display: inline-block;
7
	margin-right: 2em;
8
}
9
5 10
.container {
6 11
	max-width: 960px;
7 12
	margin: 0 auto;

+ 3 - 1
views/error.jsx

@ -7,7 +7,9 @@ const Layout = require("./layout");
7 7
module.exports = function Error({ error }) {
8 8
	return (
9 9
		<Layout>
10
			{error.message}
10
			<pre>
11
				{error.message}
12
			</pre>
11 13
		</Layout>
12 14
	);
13 15
};

+ 1 - 0
views/layout.jsx

@ -15,6 +15,7 @@ module.exports = function Layout({ children }) {
15 15
		<body>
16 16
			<div className="container">
17 17
				<h1>Matrix Room List Viewer</h1>
18
				<a href="https://git.cryto.net/joepie91/matrix-room-list-viewer">Source code</a>
18 19
				<hr/>
19 20
				{children}
20 21
			</div>