Skip to content
This repository was archived by the owner on Sep 8, 2024. It is now read-only.

Commit a1923c6

Browse files
committed
Update backend runtime, backend-frontend connection, and console output
- Change 'API_URL' to a specific localhost port for simplicity and rename it for clarity - Add 'update me' not to backend 'settings.py' for clarity - Update docstring for 'ExampleDB' in backend for clarity - Refactor backend 'build.py' -> 'start.py' and move into project for 'app' directory for Poetry script configuration - Add details in console, after project build, to inform next steps - Update backend run command to 'app-start' - Update README
1 parent 86af436 commit a1923c6

File tree

20 files changed

+130
-48
lines changed

20 files changed

+130
-48
lines changed

README.md

+74-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
# Create API App Quickstart Tool
1+
# Create API App Quickstart Tool <!-- omit in toc -->
22

3-
Welcome to the quickstart tool for creating a `FastAPI` project with a `NextJS` frontend.
3+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/create-api-app?style=flat&color=green)
44

5-
This tool is intended to be dynamic and installs the most recent packages where possible, while maintaining compatibility across the main OS's (Mac, Linux and Windows). You can use the tool by installing the `PIP` package. See the [Using The Tool section](#using-the-tool) for more details.
5+
Welcome to the quickstart tool for creating a `FastAPI` project with a `NextJS` frontend.
66

7-
If there are any issues using the tool, please flag them in the [issues](https://github.com/Achronus/create-api-app/issues) section of this repository.
7+
This tool creates a predefined template while installing the most recent packages where possible.
88

99
Found on:
1010

1111
- [PyPi](https://pypi.org/project/create-api-app/)
1212
- [GitHub](https://github.com/Achronus/create-api-app/)
1313

14+
## Contents <!-- omit in toc -->
15+
16+
- [Why This Tool?](#why-this-tool)
17+
- [The Stack](#the-stack)
18+
- [Installation](#installation)
19+
- [Running The Backend](#running-the-backend)
20+
- [Running the Frontend](#running-the-frontend)
21+
- [Customization](#customization)
22+
1423
## Why This Tool?
1524

1625
Creating a project from scratch can be a tedious process. Not only do you have to create all the files yourself, it typically requires a lot of small minor changes that can easily be automated. So, rather than wasting a lot of time setting up projects, I created a tool that does it all for me!
@@ -24,6 +33,7 @@ All projects are created using the same stack, consisting of the following:
2433
1. Backend
2534

2635
- [FastAPI](https://github.com/tiangolo/fastapi)
36+
- [Pydantic](https://docs.pydantic.dev/)
2737
- [MongoDB](https://www.mongodb.com/)
2838
- [Beanie](https://beanie-odm.dev/)
2939
- [Poetry](https://python-poetry.org/)
@@ -42,26 +52,78 @@ All projects are created using the same stack, consisting of the following:
4252

4353
_Note: all libraries and packages are automatically installed to their latest versions when running the tool._
4454

45-
### Useful Styling Options
46-
47-
- [Clerk Themes](https://clerk.com/docs/components/customization/themes)
48-
- [Shadcn UI Theme Generator](https://gradient.page/tools/shadcn-ui-theme-generator)
49-
- [Modern Background Snippets](https://bg.ibelick.com/)
55+
We've also added some extra files too! You can find out more about them in our [documentation](https://create.achronus.dev/file-structure/).
5056

51-
## Using The Tool
57+
## Installation
5258

5359
1. Firstly, install [Docker](https://docs.docker.com/get-docker/), we use this to create the frontend files dynamically using the [Build NextJS App Tool](https://github.com/Achronus/build-nextjs-app).
5460

55-
2. Install the package through `PIP` using the following command (requires `Python 3.12` minimum):
61+
2. Install the package through `PIP`:
5662

5763
```python
5864
pip install create_api_app
5965
```
6066

61-
3. Create a project with the following command:
67+
3. Create a project:
6268

6369
```python
6470
create-api-app <project_name>
6571
```
6672

6773
And that's it! You'll find two folders in your project, one called `frontend` (for NextJS) and another called `backend` (for FastAPI).
74+
75+
## Running The Backend
76+
77+
1. Open a terminal and navigate to the `backend` directory:
78+
79+
```cmd
80+
cd backend
81+
```
82+
83+
2. Install a virtual environment for `Poetry`:
84+
85+
```cmd
86+
python -m venv env
87+
```
88+
89+
3. Access it using one of the following (first -> `Windows`; second -> `Mac/Linux`):
90+
91+
```cmd
92+
.\env\Scripts\activate
93+
```
94+
95+
```cmd
96+
source ./env/bin/activate
97+
```
98+
99+
_Not working? Refer to the [virtual env docs](https://docs.python.org/3/library/venv.html#how-venvs-work)._
100+
101+
4. Run the `uvicorn` server using the `Poetry` script:
102+
103+
```cmd
104+
app-start
105+
```
106+
107+
## Running the Frontend
108+
109+
1. Open a terminal and navigate to the `frontend` directory:
110+
111+
```cmd
112+
cd frontend
113+
```
114+
115+
2. Install the packages using [Node.js](https://nodejs.org/en):
116+
117+
```cmd
118+
npm install
119+
```
120+
121+
3. Run the development server:
122+
123+
```cmd
124+
npm run dev
125+
```
126+
127+
## Customization
128+
129+
Customization options are found in our [documentation](https://create.achronus.dev/customization/).

_project_demo/.env.local.example

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# An optional API URL only used when populating your database with data from an existing API. Refer to 'backend/db_insert.py'
2-
API_URL=
1+
# The URL to connect FastAPI and NextJS together - used in `frontend/next.config.mjs`
2+
FASTAPI_CONNECTION_URL=http://127.0.0.1:8000/
33

44
# MongoDB: The connection url to your database
55
# https://www.mongodb.com/

_project_demo/backend/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from utils.fileloader import FileLoader
3+
from app.utils.fileloader import FileLoader
44

55

66
class Settings:
@@ -10,8 +10,8 @@ class Settings:
1010
FILEPATHS = __fileloader.FILEPATHS
1111

1212
DB_URL = os.getenv("DATABASE_URL")
13-
DB_NAME = ""
14-
DB_COLLECTION_NAME = ""
13+
DB_NAME = "" # Update me!
14+
DB_COLLECTION_NAME = "" # Update me!
1515

1616

1717
settings = Settings()

_project_demo/backend/app/models/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88

99
class ExampleDB(Document):
10-
"""The main model for database collection based on the database."""
10+
"""
11+
The main model for your database collection. Should represent the structure of the data in the collection.
12+
13+
For more details check the [Beanie docs](https://beanie-odm.dev/).
14+
"""
1115

1216
name: str
1317
desc: Optional[str] = None

_project_demo/backend/build.py renamed to _project_demo/backend/app/start.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uvicorn
44

55

6-
def start(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> None:
6+
def run(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> None:
77
"""Start the server."""
88
dev_mode = True if env_mode == "dev" else False
99

@@ -25,4 +25,4 @@ def start(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> N
2525
parser.add_argument("-pt", "--port", type=int, default=8000, required=False)
2626

2727
args = parser.parse_args()
28-
start(args.env, args.host, args.port)
28+
run(args.env, args.host, args.port)

_project_demo/backend/poetry.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_project_demo/backend/pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Ryan Partridge <rpartridge101@gmail.com>"]
66
readme = "README.md"
77

88
[tool.poetry.scripts]
9-
run = "build:start"
9+
app-start = "app.start:run"
1010

1111
[tool.poetry.dependencies]
1212
python = "^3.12"
@@ -19,7 +19,7 @@ beanie = "^1.26.0"
1919
[tool.poetry.group.dev.dependencies]
2020
pytest = "^8.2.2"
2121
pytest-cov = "^5.0.0"
22-
hypothesis = "^6.103.2"
22+
hypothesis = "^6.103.5"
2323
aiohttp = "^3.9.5"
2424
requests = "^2.32.3"
2525

_project_demo/frontend/next.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const loadEnv = (filePath) => {
1111

1212
loadEnv(path.resolve(process.cwd(), ".env.local"));
1313

14-
const apiUrl = process.env.API_URL;
14+
const apiUrl = process.env.FASTAPI_CONNECTION_URL;
1515

1616
const nextConfig = {
1717
images: {

create_api_app/conf/constants/content.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from .filepaths import AssetFilenames
2-
3-
41
class PoetryContent:
52
"""A helper class for retrieving content for the Poetry installation."""
63

74
def __init__(self) -> None:
8-
self.START_SERVER_CMD = f"{AssetFilenames.BUILD.split('.')[0]}:start"
5+
self.start_server_cmd = "app-start"
6+
self.start_server_location = "app.start:run"
97

108
def pyproject_desc(self) -> str:
119
return 'description = "A FastAPI backend for processing API data and passing it to the frontend."'
@@ -14,7 +12,7 @@ def pyproject_author(self) -> str:
1412
return "rpartridge101@gmail.com"
1513

1614
def pyproject_scripts(self) -> str:
17-
return f'\n\n[tool.poetry.scripts]\nrun = "{self.START_SERVER_CMD}"\n\n'
15+
return f'\n\n[tool.poetry.scripts]\n{self.start_server_cmd} = "{self.start_server_location}"\n\n'
1816

1917

2018
class FrontendContent:

create_api_app/conf/constants/filepaths.py

+4
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ def __init__(self, project_name: str = None) -> None:
6868
self.POETRY_CONF = os.path.join(self.BACKEND, AssetFilenames.POETRY_CONF)
6969

7070
self.TAILWIND_CONF = os.path.join(self.FRONTEND, AssetFilenames.TAILWIND)
71+
72+
self.ENV_LOCAL = os.path.join(self.ROOT, ".env.local")
73+
self.SETTINGS = os.path.join(self.BACKEND_APP, "config", "settings.py")
74+
self.MODELS = os.path.join(self.BACKEND_APP, "models", "__init__.py")

create_api_app/main.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import shutil
3+
import textwrap
34
import time
45

56
from create_api_app.setup.clean import CleanupController
@@ -9,7 +10,7 @@
910
)
1011
from create_api_app.setup.backend import VEnvController, BackendStaticAssetController
1112

12-
from .conf.constants.filepaths import set_project_name
13+
from .conf.constants.filepaths import ProjectPaths, set_project_name
1314
from .setup import run_frontend_tasks, run_tasks
1415
from .utils.helper import strip_whitespace_and_dashes
1516
from .utils.printables import project_table, project_complete_panel
@@ -134,7 +135,17 @@ def main(
134135
# End of script
135136
console.print(project_complete_panel())
136137
console.print(
137-
f"Access the project files here [link={os.getcwd()}]{name_print}[/link]\n"
138+
f"Access the project files here [link={os.getcwd()}]{name_print}[/link]"
139+
)
140+
141+
project_paths = ProjectPaths(name)
142+
console.print(
143+
textwrap.dedent(f"""
144+
[dark_goldenrod]Not sure where to start?[/dark_goldenrod]
145+
- [green][link={project_paths.ENV_LOCAL}].env.local[/link][/green] - Update your API keys
146+
- [yellow][link={project_paths.SETTINGS}]config/settings.py[/link][/yellow] - Update the [yellow]DB_NAME[/yellow] and [yellow]DB_COLLECTION_NAME[/yellow] for your [green]MongoDB[/green] database
147+
- [yellow][link={project_paths.MODELS}]models/__init__.py[/link][/yellow] - Update the [green]ExampleDB[/green] model\n
148+
""")
138149
)
139150

140151

create_api_app/setup_assets/backend/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from utils.fileloader import FileLoader
3+
from app.utils.fileloader import FileLoader
44

55

66
class Settings:
@@ -10,8 +10,8 @@ class Settings:
1010
FILEPATHS = __fileloader.FILEPATHS
1111

1212
DB_URL = os.getenv("DATABASE_URL")
13-
DB_NAME = ""
14-
DB_COLLECTION_NAME = ""
13+
DB_NAME = "" # Update me!
14+
DB_COLLECTION_NAME = "" # Update me!
1515

1616

1717
settings = Settings()

create_api_app/setup_assets/backend/app/models/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88

99
class ExampleDB(Document):
10-
"""The main model for database collection based on the database."""
10+
"""
11+
The main model for your database collection. Should represent the structure of the data in the collection.
12+
13+
For more details check the [Beanie docs](https://beanie-odm.dev/).
14+
"""
1115

1216
name: str
1317
desc: Optional[str] = None

create_api_app/setup_assets/backend/build.py renamed to create_api_app/setup_assets/backend/app/start.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uvicorn
44

55

6-
def start(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> None:
6+
def run(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> None:
77
"""Start the server."""
88
dev_mode = True if env_mode == "dev" else False
99

@@ -25,4 +25,4 @@ def start(env_mode: str = "dev", host: str = "127.0.0.1", port: int = 8000) -> N
2525
parser.add_argument("-pt", "--port", type=int, default=8000, required=False)
2626

2727
args = parser.parse_args()
28-
start(args.env, args.host, args.port)
28+
run(args.env, args.host, args.port)

create_api_app/setup_assets/frontend/next.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const loadEnv = (filePath) => {
1111

1212
loadEnv(path.resolve(process.cwd(), ".env.local"));
1313

14-
const apiUrl = process.env.API_URL;
14+
const apiUrl = process.env.FASTAPI_CONNECTION_URL;
1515

1616
const nextConfig = {
1717
images: {

create_api_app/setup_assets/root/.env.local

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# An optional API URL only used when populating your database with data from an existing API. Refer to 'backend/db_insert.py'
2-
API_URL=
1+
# The URL to connect FastAPI and NextJS together - used in `frontend/next.config.mjs`
2+
FASTAPI_CONNECTION_URL=http://127.0.0.1:8000/
33

44
# MongoDB: The connection url to your database
55
# https://www.mongodb.com/

pyproject.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "create-api-app"
3-
version = "2.0.1"
3+
version = "2.0.2"
44
description = "A quickstart tool for creating a FastAPI project with a NextJS frontend."
55
authors = ["Ryan Partridge <rpartridge101@gmail.com>"]
66
readme = "README.md"
@@ -15,7 +15,6 @@ pytest = "^8.2.2"
1515
build_nextjs_app = "^1.0.2"
1616
docker = "^7.1.0"
1717

18-
1918
[build-system]
2019
requires = ["poetry-core"]
2120
build-backend = "poetry.core.masonry.api"

tests/mappings/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'readme = "README.md"\n',
88
"\n",
99
"[tool.poetry.scripts]\n",
10-
'run = "build:start"\n',
10+
'app-start = "app.start:run"\n',
1111
"\n",
1212
]
1313

0 commit comments

Comments
 (0)