@@ -14,8 +18,18 @@ function Layout() {
Caddy Proxy Manager
+
+ {auth.user && (
+
+
+
+
+ )}
+
-{/* Home
+ {/* Home
Hosts */}
diff --git a/frontend/src/pages/Hosts.tsx b/frontend/src/pages/Hosts.tsx
index 7e8c6b6..fdbb432 100644
--- a/frontend/src/pages/Hosts.tsx
+++ b/frontend/src/pages/Hosts.tsx
@@ -11,6 +11,7 @@ import { joiResolver } from "@hookform/resolvers/joi";
import Joi from "joi";
import { HiAdjustments, HiTrash, HiDocumentAdd } from "react-icons/hi";
import React from "react";
+import http, { RequestResponse } from "../utils/axios";
type Domain = {
fqdn: string;
@@ -104,10 +105,9 @@ function HostsPage() {
}, []);
const getHosts = async () => {
- return await fetch(`http://${window.location.hostname}:3001/api/hosts`)
- .then((res) => res.json())
- .then((json) => {
- setHostData(json);
+ return await http.get
(`/hosts`)
+ .then((res) => {
+ setHostData(res.data);
});
};
@@ -122,22 +122,14 @@ function HostsPage() {
upstreams: data.upstreams,
};
setLoading(true);
- const res = await fetch(`http://${window.location.hostname}:3001/api/hosts`, {
- body: JSON.stringify(jsonBody),
- method: "POST",
- });
- if (res.status !== 200) {
- // handle Error
- }
+ await http.post(`hosts`, jsonBody);
setLoading(false);
setModal(false);
getHosts();
};
const deleteHost = async (hostID: number) => {
- await fetch(`http://${window.location.hostname}:3001/api/hosts/${hostID}`, {
- method: "DELETE",
- });
+ await http.delete(`/hosts/${hostID}`);
getHosts();
};
diff --git a/frontend/src/utils/axios.ts b/frontend/src/utils/axios.ts
new file mode 100644
index 0000000..43a5c2e
--- /dev/null
+++ b/frontend/src/utils/axios.ts
@@ -0,0 +1,37 @@
+import axios, { AxiosRequestConfig } from "axios";
+
+export interface RequestResponse {
+ result: any;
+ error: Error;
+ }
+
+ interface Error {
+ code: number;
+ message: string;
+ }
+
+
+axios.interceptors.request.use((config: AxiosRequestConfig) => {
+ if (!config) {
+ config = {};
+ }
+ if (!config.headers) {
+ config.headers = {};
+ }
+ const token = localStorage.getItem("token");
+ if (token) {
+ config.headers.authorization = `Bearer ${token}`;
+ }
+ config.baseURL = "http://localhost:3001/api/";
+ return config;
+});
+
+const methods = {
+ get: axios.get,
+ post: axios.post,
+ put: axios.put,
+ delete: axios.delete,
+ patch: axios.patch,
+ };
+
+export default methods;
\ No newline at end of file
diff --git a/frontend/yarn.lock b/frontend/yarn.lock
index 953a97e..8eb01a8 100644
--- a/frontend/yarn.lock
+++ b/frontend/yarn.lock
@@ -2829,6 +2829,14 @@ axe-core@^4.4.3:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.3.tgz#11c74d23d5013c0fa5d183796729bc3482bd2f6f"
integrity sha512-32+ub6kkdhhWick/UjvEwRchgoetXqTK14INLqbGm5U2TzBkBNF3nQtLYm8ovxSkQWArjEQvftCKryjZaATu3w==
+axios@^0.27.2:
+ version "0.27.2"
+ resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
+ integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
+ dependencies:
+ follow-redirects "^1.14.9"
+ form-data "^4.0.0"
+
axobject-query@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -4792,7 +4800,7 @@ flowbite@^1.5.3:
"@popperjs/core" "^2.9.3"
mini-svg-data-uri "^1.4.3"
-follow-redirects@^1.0.0:
+follow-redirects@^1.0.0, follow-redirects@^1.14.9:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
@@ -4825,6 +4833,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"
+form-data@^4.0.0:
+ version "4.0.0"
+ resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
+ integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
+ dependencies:
+ asynckit "^0.4.0"
+ combined-stream "^1.0.8"
+ mime-types "^2.1.12"
+
forwarded@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"