Geniální software
This commit is contained in:
69
index.js
Normal file
69
index.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import dotenv from "dotenv";
|
||||
import e from "express";
|
||||
import z from "zod";
|
||||
|
||||
import { execa } from "execa";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const CONTAINER = process.env.CONTAINER || "mailserver";
|
||||
const INVITE_CODE = process.env.INVITE_CODE;
|
||||
const DOMAIN = process.env.DOMAIN;
|
||||
|
||||
const RegisterSchema = z.object({
|
||||
invite: z.literal(INVITE_CODE, "ŠPATNÝ KÓD, GÓJI."),
|
||||
username: z
|
||||
.string()
|
||||
.trim()
|
||||
.regex(
|
||||
/^[a-zA-Z0-9]([a-zA-Z0-9._-]{0,62}[a-zA-Z0-9])?$/,
|
||||
"Neplatný formát jména."
|
||||
)
|
||||
.transform((s) => s.toLowerCase()),
|
||||
password: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(8, "Heslo by mělo mít aspoň 8 znaků.")
|
||||
.regex(/^[^\s]+$/, "Heslo prosím bez mezer."),
|
||||
});
|
||||
|
||||
const app = e();
|
||||
|
||||
app.use(e.urlencoded({ extended: true }));
|
||||
app.use(e.static("public"));
|
||||
|
||||
app.post("/register", async (req, res) => {
|
||||
const result = RegisterSchema.safeParse(req.body);
|
||||
|
||||
if (!result.success) {
|
||||
return res
|
||||
.status(400)
|
||||
.send(`Tvůj požadavek je neplatný: ${result.error.issues[0].message}`);
|
||||
}
|
||||
|
||||
const email = `${result.data.username}@${DOMAIN}`;
|
||||
const password = result.data.password;
|
||||
|
||||
try {
|
||||
await execa(
|
||||
"docker",
|
||||
["exec", "-i", CONTAINER, "setup", "email", "add", email, password],
|
||||
{ timeout: 10000 }
|
||||
);
|
||||
|
||||
res.send(
|
||||
`VÍTEJ V TAJNÉM ELITNÍM KLUBU. Tvůj nový profesionální e-mail je ${email}.`
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("CHYBA:", err.stderr || err.message);
|
||||
|
||||
res
|
||||
.status(500)
|
||||
.send(
|
||||
"Buď se něco rozbilo nebo tento e-mail už existuje. Kdyžtak si postěžuj sneedNEGROVI."
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(PORT);
|
||||
Reference in New Issue
Block a user