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

Frontend: basic request to API #76

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 40 additions & 13 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
A simple guide on how to start on poetry, a simple package manager like npm but for python.
## Poetry + FastAPI

Make sure you cd into the parent backend folder when running the api.
A simple guide on how to start on poetry, a simple **package manager** like npm but for python.

-------------------------------------------------------------------------------------------
ref :
- [official docs](https://python-poetry.org/docs/)
- [medium article](https://medium.com/@caetanoog/start-your-first-fastapi-server-with-poetry-in-10-minutes-fef90e9604d9)

### Setup

Make sure you `cd` into the parent backend folder when running the api.

-----------------------------------------

To start with Poetry on mac, run:

brew install poetry
`brew install poetry`

(or) Linux, macOS, Windows (WSL) :

`curl -sSL https://install.python-poetry.org | python3 -`

This will set up poetry.
This will set up poetry. check installation using `poetry --version`

-------------------------------------------------------------------------------------------
------------------------------------------

To install dependencies, run:

poetry install
`poetry install`

- The install command reads the `pyproject.toml` file from the current project, resolves the dependencies, and installs them.
- If there is a `poetry.lock` file in the current directory, it will use the exact versions from there instead of resolving them.
- This ensures that everyone using the library will get the same versions of the dependencies.

------------------------------------------


To start the FastAPI server locally (using virtual env), run:

-------------------------------------------------------------------------------------------
`poetry shell`

and then run:

To start the api locally, run:
`uvicorn main:app --reload`

poetry shell
- Your local host on port `8000` should now be running the api : http://127.0.0.1:8000/

AND THEN:
### Frontend: basic request to API

uvicorn main:app --reload
ref :
- guide : https://vercel.com/templates/next.js/nextjs-fastapi-starter
- repo : https://github.com/digitros/nextjs-fastapi

> Make sure both the frontend and backend setups are up and running

Your local host on port 8000 should now be running the api
- next.js frontend running on http://localhost:3000/
- fastAPI server runs on http://127.0.0.1:8000/
- front end api requests made from http://localhost:3000/api/python
7 changes: 5 additions & 2 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello World"}
return {"message": "Hello World"}

@app.get("/api/python")
async def root():
return {"message": "Hello from fastAPI backend"}
30 changes: 27 additions & 3 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}
rewrites: async () => {
return [
{
source: "/api/:path*",
destination:
process.env.NODE_ENV === "development"
? "http://127.0.0.1:8000/api/:path*"
: "/api/",
},
{
source: "/docs",
destination:
process.env.NODE_ENV === "development"
? "http://127.0.0.1:8000/docs"
: "/api/docs",
},
{
source: "/openapi.json",
destination:
process.env.NODE_ENV === "development"
? "http://127.0.0.1:8000/openapi.json"
: "/api/openapi.json",
},
];
},
};

module.exports = nextConfig
module.exports = nextConfig;
2 changes: 1 addition & 1 deletion frontend/package-lock.json

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

4 changes: 3 additions & 1 deletion frontend/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function Home() {
return <h1></h1>;
return <h1>
Hello Team!
</h1>;
}