Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: create webservice for api handling #25

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_BACKEND_URL=http://localhost:8080/
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ cd playground-web
npm install
```

## Environment Variables

Before running the project, make sure to set up the necessary environment variables. Create a `.env.local` file in the root directory of the project and add the following variables:

````bash
# .env.local
NEXT_PUBLIC_BACKEND_URL=http://localhost:8080/

## Development

To start the development server, run:

```bash
npm run dev
```
````
tarun-29 marked this conversation as resolved.
Show resolved Hide resolved

This will launch the app on [http://localhost:3000](http://localhost:3000). The app will automatically reload if you make changes to the code.

Expand Down Expand Up @@ -71,6 +79,7 @@ docker-compose up
```

## Step-by-Step Setup for End-to-End Development with Docker

We have added `Dockerfile.dev` which is for development purposes, ensuring your Next.js application supports hot reloading and reflects code changes without requiring image rebuilds.

`docker-compose.dev.yml` configures Docker Compose to build and run your Next.js app in development mode.
Expand All @@ -79,7 +88,6 @@ We have added `Dockerfile.dev` which is for development purposes, ensuring your
docker-compose -f docker-compose.dev.yml up --build
```


## Building for Production

To create a production build:
Expand Down
61 changes: 61 additions & 0 deletions package-lock.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not adding/updating any packages this file shouldn't be checked in correct?

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@mui/icons-material": "^6.1.1",
"@mui/material": "^6.1.1",
"autoprefixer": "^10.4.20",
"axios": "^1.7.7",
tarun-29 marked this conversation as resolved.
Show resolved Hide resolved
"lucide-react": "^0.446.0",
"next": "14.2.13",
"package.json": "^2.0.1",
Expand Down
28 changes: 7 additions & 21 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
// src/lib/api.ts
import { CLI_COMMAND_URL } from "@/shared/constants/apiEndpoints";
import { WebService } from "@/services/webService";

export const executeCLICommandOnServer = async (
cmd: string,
cmdOptions: object
): Promise<string> => {
) => {
try {
const response = await fetch(`${CLI_COMMAND_URL}/${cmd}`, {
method: "POST",
body: JSON.stringify(cmdOptions),
headers: {
"Content-Type": "application/json",
},
});

const response = await WebService.post(`/cli/${cmd}`, cmdOptions);
// TODO: This needs to be looked at
tarun-29 marked this conversation as resolved.
Show resolved Hide resolved
const data = await response.json();
if (Object.prototype.hasOwnProperty.call(data, "data")) {
return data.data;
} else if (Object.prototype.hasOwnProperty.call(data, "error")) {
return data.error;
}

if (!response.ok) {
throw new Error("Network response was not ok");
if (response?.data) {
return response.data;
} else {
throw new Error("Unexpected response structure");
}

return data;
} catch (error: unknown) {
console.error("Error executing command:", error);
return `Error: ${error}`;
Expand Down
15 changes: 15 additions & 0 deletions src/services/webService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from "axios";
let BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL;

if (!BACKEND_URL) {
console.warn(
"Warning: NEXT_PUBLIC_BACKEND_URL is not defined. Defaulting to http://localhost:3000"
);
BACKEND_URL = "http://localhost:8080";
}
export const WebService = axios.create({
baseURL: BACKEND_URL,
headers: {
"Content-Type": "application/json",
},
});
2 changes: 1 addition & 1 deletion src/shared/constants/apiEndpoints.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This folder will contain or hold constants. For example in this particular file we can store the api endpoints as constants
export const CLI_COMMAND_URL = "http://localhost:8080/cli";
export const ROOT_URL = "http://localhost:8080/";
tarun-29 marked this conversation as resolved.
Show resolved Hide resolved
Loading