diff --git a/backend/server.ts b/backend/server.ts index 96db10e..608a095 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -4,6 +4,7 @@ import multipart from "@fastify/multipart"; import { libreConvert } from "./src/routes/libreconvert.route"; import { colorConvert } from "./src/routes/colorconvert.route"; import {passwordGenerate} from "./src/routes/passwordgenerate.route"; +import { regexTest } from "./src/routes/regextest.route"; const app = Fastify({ logger: true }); @@ -17,6 +18,7 @@ app.register(multipart); app.register(libreConvert); app.register(colorConvert); app.register(passwordGenerate); +app.register(regexTest); const PORT = process.env.PORT || 4000; app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => { diff --git a/backend/src/routes/regextest.route.ts b/backend/src/routes/regextest.route.ts new file mode 100644 index 0000000..0d1bf4f --- /dev/null +++ b/backend/src/routes/regextest.route.ts @@ -0,0 +1,48 @@ +import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; + +interface RequestBody { + input: string; +} + +export async function regexTest(app: FastifyInstance) { + app.post( + "/api/regex-test", + async ( + request: FastifyRequest<{ Body: RequestBody }>, + reply: FastifyReply, + ) => { + try { + const data = request.body; + if (!data) { + return reply.status(400).send({ error: "No Regex declared!" }); + } + const regexPattern = data.input; + + // Überprüfe, ob der Regex-Pattern korrekt ist + let regex: RegExp; + try { + regex = new RegExp(regexPattern); + } catch (e) { + return reply.status(400).send({ error: "Invalid regular expression!" }); + } + + // Teste den Regex + const result = regex.test(data.input); + + // Erstelle eine Ausgabe basierend auf dem Test-Ergebnis + let output = ""; + + if (result) { + output = `The input matches the regular expression!`; + } else { + output = `The input does not match the regular expression.`; + } + + reply.header("Content-Type", "text/plain").status(200).send(output); + } catch (error) { + console.error("Convert error:", error); + reply.status(500).send({ error: "Error while converting!" }); + } + }, + ); +} diff --git a/frontend/src/app/regex-tester/layout.tsx b/frontend/src/app/regex-tester/layout.tsx new file mode 100644 index 0000000..935382f --- /dev/null +++ b/frontend/src/app/regex-tester/layout.tsx @@ -0,0 +1,20 @@ +import React from "react"; +import type { Metadata } from "next"; +import { toolLinks } from "@/constants"; + +export const metadata: Metadata = { + title: toolLinks[2].title, + description: "Tester for regular expressions!", +}; + +export default function RootLayout({ + children, + }: Readonly<{ + children: React.ReactNode; +}>) { + return ( + +
{children} + + ); +} diff --git a/frontend/src/app/regex-tester/page.tsx b/frontend/src/app/regex-tester/page.tsx new file mode 100644 index 0000000..85bc4e1 --- /dev/null +++ b/frontend/src/app/regex-tester/page.tsx @@ -0,0 +1,91 @@ +"use client"; + +import React, { useState } from "react"; +import Navbar from "../../components/Navbar"; +import Footer from "../../components/Footer"; +import Button from "../../components/Button"; + +export default function RgbToHex() { + + const [loading, setLoading] = useState(false); + const [output, setOutput] = useState(""); + + const testRegex = async () => { + + setLoading(true); + + const input = (document.getElementById("input") as HTMLInputElement).value; + + try { + const response = await fetch( + process.env.backend_url + "/api/regex-test", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ input: input}), + }, + ); + if (!response.ok) { + return new Error(`Error: ${response.statusText}`); + } + const output: string = await response.text(); + console.log(output); + setOutput(output); + + } catch (error) { + console.error("Error while converting:", error); + alert("Error while converting"); + } finally { + setLoading(false); + } + }; + + const clearInAndOutput = () => { + setOutput(""); + const input = document.getElementById("input") as HTMLInputElement; + input.value = ""; + }; + + return ( +