enh(#10): Frontend is now configurable from config.json
The host URL is now read from configuration.
This commit is contained in:
parent
4c36f35492
commit
0238e4b67b
4 changed files with 42 additions and 5 deletions
|
@ -5,10 +5,6 @@ Server:
|
||||||
cors: False
|
cors: False
|
||||||
static_folder: frontend/build
|
static_folder: frontend/build
|
||||||
|
|
||||||
Web:
|
|
||||||
host: 127.0.0.1
|
|
||||||
port: 8000
|
|
||||||
|
|
||||||
Database:
|
Database:
|
||||||
username: foo
|
username: foo
|
||||||
password: bar
|
password: bar
|
||||||
|
|
|
@ -3,6 +3,7 @@ import "./URLShortener.css";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { ToastContainer, toast } from "react-toastify";
|
import { ToastContainer, toast } from "react-toastify";
|
||||||
import "react-toastify/dist/ReactToastify.css";
|
import "react-toastify/dist/ReactToastify.css";
|
||||||
|
import Config from "../../config"
|
||||||
|
|
||||||
export default function URLShortener() {
|
export default function URLShortener() {
|
||||||
const [url, setUrl] = useState<string>("");
|
const [url, setUrl] = useState<string>("");
|
||||||
|
@ -28,10 +29,14 @@ export default function URLShortener() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const config = Config()
|
||||||
|
const api_endpoint = "/api/v1/shorten";
|
||||||
|
|
||||||
|
const api_url = `${config.url}${api_endpoint}`;
|
||||||
|
|
||||||
// Send the POST request to the backend
|
// Send the POST request to the backend
|
||||||
await axios
|
await axios
|
||||||
.post("http://127.0.0.1:8000/api/v1/shorten", {
|
.post(api_url , {
|
||||||
url: url
|
url: url
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
|
|
12
frontend/src/config.json
Normal file
12
frontend/src/config.json
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"frontend": {
|
||||||
|
"scheme": "http",
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": "8000"
|
||||||
|
},
|
||||||
|
"api": {
|
||||||
|
"scheme": "http",
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": "8000"
|
||||||
|
}
|
||||||
|
}
|
24
frontend/src/config.tsx
Normal file
24
frontend/src/config.tsx
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import configuration from './config.json';
|
||||||
|
|
||||||
|
class APIConfig {
|
||||||
|
scheme: string;
|
||||||
|
host: string;
|
||||||
|
port: string;
|
||||||
|
url: string;
|
||||||
|
|
||||||
|
constructor(scheme: string, host: string, port: string) {
|
||||||
|
this.scheme = scheme;
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
this.url = `${scheme}://${host}:${port}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Config() {
|
||||||
|
const scheme = configuration.api.scheme;
|
||||||
|
const host = configuration.api.host;
|
||||||
|
const port = configuration.api.port;
|
||||||
|
|
||||||
|
const apiConfig = new APIConfig(scheme, host, port);
|
||||||
|
return apiConfig
|
||||||
|
}
|
Loading…
Reference in a new issue