Skip to content

Commit

Permalink
Gateway - stop hard coding urls (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
hevans66 committed Sep 14, 2023
1 parent 2978e8b commit 7627833
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Install [docker](https://docs.docker.com/engine/install/)
* Define necessary env variables
```
NEXT_PUBLIC_BACKEND_URL=http://localhost:8080
FRONTEND_URL=http://localhost:3000
POSTGRES_PASSWORD=MAKE_UP_SOMETHING_RANDOM
POSTGRES_USER=labdao
POSTGRES_DB=labdao
Expand Down
2 changes: 1 addition & 1 deletion gateway/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {

// Set up CORS
corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"},
AllowedOrigins: []string{os.Getenv("FRONTEND_URL")},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST"},
})
Expand Down
4 changes: 3 additions & 1 deletion gateway/frontend/app/datafile/list/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import TableContainer from '@mui/material/TableContainer'
import TableHead from '@mui/material/TableHead'
import TableRow from '@mui/material/TableRow'

import backendUrl from 'lib/backendUrl'

export default function ListDataFiles() {
interface DataFile {
CID: string;
Expand All @@ -20,7 +22,7 @@ export default function ListDataFiles() {
const [datafiles, setDataFiles] = useState<DataFile[]>([]);

useEffect(() => {
fetch('http://localhost:8080/get-datafiles')
fetch(`${backendUrl()}/get-datafiles`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
Expand Down
8 changes: 5 additions & 3 deletions gateway/frontend/app/job/init/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import React, { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import backendUrl from 'lib/backendUrl';


export default function InitJob() {
const dispatch = useDispatch();
Expand All @@ -28,12 +30,12 @@ export default function InitJob() {
const [selectedDataFiles, setSelectedDataFiles] = useState([]);

useEffect(() => {
fetch('http://localhost:8080/get-tools')
fetch(`${backendUrl()}/get-tools`)
.then(response => response.json())
.then(data => setTools(data))
.catch(error => console.error('Error fetching tools:', error));

fetch('http://localhost:8080/get-datafiles')
fetch(`${backendUrl()}/get-datafiles`)
.then(response => response.json())
.then(data => setDataFiles(data))
.catch(error => console.error('Error fetching data files:', error));
Expand All @@ -58,7 +60,7 @@ export default function InitJob() {

console.log('Sending request with data:', data)

fetch('http://localhost:8080/init-job', {
fetch(`${backendUrl()}/init-job`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion gateway/frontend/app/tool/list/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import TableContainer from '@mui/material/TableContainer'
import TableHead from '@mui/material/TableHead'
import TableRow from '@mui/material/TableRow'

import backendUrl from 'lib/backendUrl'

export default function ListToolFiles() {
interface Tool {
CID: string;
Expand All @@ -18,7 +20,7 @@ export default function ListToolFiles() {
const [tools, setTools] = useState<Tool[]>([]);

useEffect(() => {
fetch('http://localhost:8080/get-tools')
fetch(`${backendUrl()}/get-tools`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
Expand Down
6 changes: 6 additions & 0 deletions gateway/frontend/lib/backendUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function backendUrl() {
if(!process.env.NEXT_PUBLIC_BACKEND_URL) {
throw new Error("The environment variable NEXT_PUBLIC_BACKEND_URL must be defined.");
}
return process.env.NEXT_PUBLIC_BACKEND_URL
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import backendUrl from "lib/backendUrl";

export const saveDataFileToServer = async (
file: File,
metadata: { [key: string]: any }
Expand All @@ -12,7 +14,7 @@ export const saveDataFileToServer = async (
formData.append(key, metadata[key]);
}

const response = await fetch('http://localhost:8080/add-datafile', {
const response = await fetch(`${backendUrl()}/add-datafile`, {
method: 'POST',
body: formData,
})
Expand Down
4 changes: 3 additions & 1 deletion gateway/frontend/lib/redux/slices/jobSlice/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import backendUrl from "lib/backendUrl"

export const initJobOnServer = async (
jobData: { [key: string]: any }
): Promise<any> => {
const response = await fetch('http://localhost:8080/init-job', {
const response = await fetch(`${backendUrl()}/init-job`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion gateway/frontend/lib/redux/slices/toolAddSlice/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import backendUrl from "lib/backendUrl"

export const addToolToServer = async (
payload: { toolData: { [key: string]: any }, walletAddress: string }
): Promise<any> => {
const response = await fetch('http://localhost:8080/add-tool', {
const response = await fetch(`${backendUrl()}/add-tool`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion gateway/frontend/lib/redux/slices/userSlice/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import backendUrl from "lib/backendUrl"

export const saveUserDataToServer = async (
username: string,
walletAddress: string
): Promise<{ username: string, walletAddress: string }> => {
const response = await fetch('http://localhost:8080/user', {
const response = await fetch(`${backendUrl()}/user`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, walletAddress }),
Expand Down

0 comments on commit 7627833

Please sign in to comment.