diff --git a/README.md b/README.md index f12724cf72..3b1ddffdc6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # Full Stack FastAPI Template -Test Docker Compose -Test Backend -Coverage +[![Test Docker Compose](../../actions/workflows/test-docker-compose.yml/badge.svg)](../../actions/workflows/test-docker-compose.yml) +[![Test Backend](../../actions/workflows/test-backend.yml/badge.svg)](../../actions/workflows/test-backend.yml) ## Technology Stack and Features @@ -15,128 +14,41 @@ - ๐Ÿ’ƒ Using TypeScript, hooks, [Vite](https://vitejs.dev), and other parts of a modern frontend stack. - ๐ŸŽจ [Tailwind CSS](https://tailwindcss.com) and [shadcn/ui](https://ui.shadcn.com) for the frontend components. - ๐Ÿค– An automatically generated frontend client. - - ๐Ÿงช [Playwright](https://playwright.dev) for End-to-End testing. + - ๐Ÿงช [Playwright](https://playwright.dev) for end-to-end testing. - ๐Ÿฆ‡ Dark mode support. - โ˜๏ธ [FastAPI Cloud](https://fastapicloud.com) for deployment. - ๐Ÿ‹ [Docker Compose](https://www.docker.com) for local services and self-hosted deployment. - ๐Ÿ“ž [Traefik](https://traefik.io) as a reverse proxy with automatic HTTPS. - ๐Ÿ”’ Secure password hashing by default. - ๐Ÿ”‘ JWT (JSON Web Token) authentication. -- ๐Ÿ“ซ Email based password recovery. +- ๐Ÿ“ซ Email-based password recovery. - ๐Ÿ“ฌ [Mailcatcher](https://mailcatcher.me) for local email testing during development. - โœ… Tests with [Pytest](https://pytest.org). -- ๐Ÿšข Deployment with FastAPI Cloud or self-hosting with Docker Compose and Traefik. - ๐Ÿญ CI (continuous integration) and CD (continuous deployment) based on GitHub Actions. ### Dashboard Login -[![Dashboard login screenshot](img/login.png)](https://github.com/fastapi/full-stack-fastapi-template) +![Dashboard login screenshot](img/login.png) ### Dashboard - Admin -[![Admin dashboard screenshot](img/dashboard.png)](https://github.com/fastapi/full-stack-fastapi-template) +![Admin dashboard screenshot](img/dashboard.png) ### Dashboard - Items -[![Items dashboard screenshot](img/dashboard-items.png)](https://github.com/fastapi/full-stack-fastapi-template) +![Items dashboard screenshot](img/dashboard-items.png) ### Dashboard - Dark Mode -[![Dark mode dashboard screenshot](img/dashboard-dark.png)](https://github.com/fastapi/full-stack-fastapi-template) +![Dark mode dashboard screenshot](img/dashboard-dark.png) ### Interactive API Documentation -[![API docs](img/docs.png)](https://github.com/fastapi/full-stack-fastapi-template) +![API docs](img/docs.png) -## How To Use It +## How to Use It -You can **just fork or clone** this repository and use it as is. - -โœจ It just works. โœจ - -### How to Use a Private Repository - -If you want to have a private repository, GitHub won't allow you to simply fork it as it doesn't allow changing the visibility of forks. - -But you can do the following: - -- Create a new GitHub repo, for example `my-full-stack`. -- Clone this repository manually, set the name with the name of the project you want to use, for example `my-full-stack`: - -```bash -git clone git@github.com:fastapi/full-stack-fastapi-template.git my-full-stack -``` - -- Enter into the new directory: - -```bash -cd my-full-stack -``` - -- Set the new origin to your new repository, copy it from the GitHub interface, for example: - -```bash -git remote set-url origin git@github.com:octocat/my-full-stack.git -``` - -- Add this repo as another "remote" to allow you to get updates later: - -```bash -git remote add upstream git@github.com:fastapi/full-stack-fastapi-template.git -``` - -- Push the code to your new repository: - -```bash -git push -u origin master -``` - -### Update From the Original Template - -After cloning the repository, and after doing changes, you might want to get the latest changes from this original template. - -- Make sure you added the original repository as a remote, you can check it with: - -```bash -git remote -v - -origin git@github.com:octocat/my-full-stack.git (fetch) -origin git@github.com:octocat/my-full-stack.git (push) -upstream git@github.com:fastapi/full-stack-fastapi-template.git (fetch) -upstream git@github.com:fastapi/full-stack-fastapi-template.git (push) -``` - -- Pull the latest changes without merging: - -```bash -git pull --no-commit upstream master -``` - -This will download the latest changes from this template without committing them, that way you can check everything is right before committing. - -- If there are conflicts, solve them in your editor. - -- Once you are done, commit the changes: - -```bash -git merge --continue -``` - -### Configure - -You can update the local settings in the `.env` file. Before deploying, configure the environment variables and secrets described in the [FastAPI Cloud deployment guide](./deployment.md) or the [Docker Compose deployment guide](./deployment-docker-compose.md). Never deploy with the default `changethis` values. - -### Generate Secret Keys - -Some environment variables in the `.env` file have a default value of `changethis`. - -You have to change them with a secret key, to generate secret keys you can run the following command: - -```bash -python -c "import secrets; print(secrets.token_urlsafe(32))" -``` - -Copy the content and use that as password / secret key. And run that again to generate another secure key. +Click the **Use this template** button at the top of this page to create a new repository. ## Backend Development @@ -148,9 +60,9 @@ Frontend docs: [frontend/README.md](./frontend/README.md). ## Deployment -Deployment docs: [deployment.md](./deployment.md). +FastAPI Cloud deployment: [deployment.md](./deployment.md). -Docker Compose deployment: [deployment-docker-compose.md](./deployment-docker-compose.md). +Self-hosted deployment with Docker Compose: [deployment-docker-compose.md](./deployment-docker-compose.md). ## Development diff --git a/backend/README.md b/backend/README.md index f453619484..c8605ffb49 100644 --- a/backend/README.md +++ b/backend/README.md @@ -58,19 +58,19 @@ To open a shell in the backend container: $ docker compose exec backend bash ``` -## Backend tests +## Backend Tests To test the backend from the `backend` directory, run: ```console -$ uv run bash ./scripts/test.sh +$ uv run bash scripts/test.sh ``` -The tests run with Pytest, modify and add tests to `./backend/tests/`. +The tests run with Pytest. Modify existing tests or add new ones in `./backend/tests/`. -If you use GitHub Actions the tests will run automatically. +If you use GitHub Actions, the tests will run automatically. -### Test running stack +### Test a Running Stack If your stack is already up and you just want to run the tests, you can use: @@ -78,7 +78,7 @@ If your stack is already up and you just want to run the tests, you can use: docker compose exec backend bash scripts/tests-start.sh ``` -That `/app/scripts/tests-start.sh` script just calls `pytest` after making sure that the rest of the stack is running. If you need to pass extra arguments to `pytest`, you can pass them to that command and they will be forwarded. +The `/app/backend/scripts/tests-start.sh` script calls `pytest` after making sure that the rest of the stack is running. If you need to pass extra arguments to `pytest`, you can pass them to that command and they will be forwarded. For example, to stop on first error: @@ -88,7 +88,7 @@ docker compose exec backend bash scripts/tests-start.sh -x ### Test Coverage -When the tests are run, a file `htmlcov/index.html` is generated, you can open it in your browser to see the coverage of the tests. +When the tests run, they generate `htmlcov/index.html`. Open it in your browser to inspect the test coverage. ## Migrations @@ -128,7 +128,7 @@ If you don't want to start with the default models and want to remove them / mod The email templates are written with [React Email](https://react.email) in `./packages/react-email/`. The `emails` directory holds one component per email and the `ui` directory holds the shared components (layout, heading, button, link, callout). -The rendered HTML in `./backend/app/email-templates/` is generated from those components, it is what the application sends, and it shouldn't be edited by hand. +The rendered HTML in `./backend/app/email-templates/` is generated from those components. It is what the application sends and should not be edited by hand. To preview the emails while editing them, start the dev server from the root of the project: diff --git a/development.md b/development.md index 07eb04916a..2cce9d12f6 100644 --- a/development.md +++ b/development.md @@ -81,11 +81,11 @@ Stop a locally running FastAPI server before starting the Compose backend becaus Mailcatcher captures emails sent during local development instead of delivering them. The local backend connects to it at `localhost:1025`, and the Compose backend connects to the `mailcatcher` service. Captured emails are available at . -## Docker Compose files and env vars +## Docker Compose Files and Environment Variables -There is a main `compose.yml` file with all the configurations that apply to the whole stack, it is used automatically by `docker compose`. +The main `compose.yml` file contains the configuration shared by the whole stack. Docker Compose loads it automatically. -And there's also a `compose.override.yml` with overrides for development, for example to mount the source code as a volume. It is used automatically by `docker compose` to apply overrides on top of `compose.yml`. +The `compose.override.yml` file adds local development settings, such as mounting the source code as a volume. Docker Compose also loads it automatically and applies it on top of `compose.yml`. The `compose.deploy.yml` file contains the deployment-specific settings, including HTTPS and automatic certificate handling. It is explicitly combined with `compose.yml` when deploying the application. @@ -97,79 +97,42 @@ After changing variables, make sure you restart the stack: docker compose watch ``` -## The .env file +## The `.env` File -The `.env` file contains the shared local defaults, generated keys, passwords, and other configuration. Its hostnames use `localhost` for processes running on your machine. Docker Compose overrides hostnames such as the database and SMTP server with their Compose service names. +The tracked `.env` file contains local development defaults, passwords, and other configuration. Its hostnames use `localhost` for processes running on your machine. Docker Compose overrides hostnames such as the database and SMTP server with their Compose service names. -Depending on your workflow, you could want to exclude it from Git, for example if your project is public. In that case, you would have to make sure to set up a way for your CI tools to obtain it while building or deploying your project. +Do not store deployment secrets in `.env`. Configure them as described in the [FastAPI Cloud deployment guide](./deployment.md) or the [Docker Compose deployment guide](./deployment-docker-compose.md). -One way to do it could be to add each environment variable to your CI/CD system. +## Pre-commit Hooks and Code Linting -## Pre-commits and code linting - -we are using a tool called [prek](https://prek.j178.dev/) (modern alternative to [Pre-commit](https://pre-commit.com/)) for code linting and formatting. - -When you install it, it runs right before making a commit in git. This way it ensures that the code is consistent and formatted even before it is committed. +The project uses [prek](https://prek.j178.dev/), a modern alternative to [pre-commit](https://pre-commit.com/), for code linting and formatting. You can find a file `.pre-commit-config.yaml` with configurations at the root of the project. -#### Install prek to run automatically +### Install `prek` to Run Automatically `prek` is already part of the dependencies of the project. -After having the `prek` tool installed and available, you need to "install" it in the local repository, so that it runs automatically before each commit. - -Using `uv`, you could do it with (make sure you are inside `backend` folder): +From the project root, install the Git hook so that `prek` runs automatically before each commit: ```bash -โฏ uv run prek install -f -prek installed at `../.git/hooks/pre-commit` +uv run prek install -f ``` The `-f` flag forces the installation, in case there was already a `pre-commit` hook previously installed. -Now whenever you try to commit, e.g. with: +Now whenever you try to commit, for example with: ```bash git commit ``` -...prek will run and check and format the code you are about to commit, and will ask you to add that code (stage it) with git again before committing. - -Then you can `git add` the modified/fixed files again and now you can commit. +`prek` will check and format the code you are about to commit. If it modifies any files, add those files to Git again before committing. -#### Running prek hooks manually +### Run `prek` Manually -you can also run `prek` manually on all the files, you can do it using `uv` with: +You can also run `prek` manually on all files from the project root: ```bash -โฏ uv run prek run --all-files -check for added large files..............................................Passed -check toml...............................................................Passed -check yaml...............................................................Passed -fix end of files.........................................................Passed -trim trailing whitespace.................................................Passed -ruff.....................................................................Passed -ruff-format..............................................................Passed -biome check..............................................................Passed +uv run prek run --all-files ``` - -## URLs - -The deployed URLs use these same paths, but with your own domain. - -### Development URLs - -Development URLs, for local development. - -Application: - -Automatic Interactive Docs (Swagger UI): - -Automatic Alternative Docs (ReDoc): - -Adminer: - -Traefik UI: - -MailCatcher: diff --git a/frontend/README.md b/frontend/README.md index af7cfb6b24..5055e35c8f 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -1,27 +1,29 @@ # FastAPI Project - Frontend -The frontend is built with [Vite](https://vitejs.dev/), [React](https://reactjs.org/), [TypeScript](https://www.typescriptlang.org/), [TanStack Query](https://tanstack.com/query), [TanStack Router](https://tanstack.com/router) and [Tailwind CSS](https://tailwindcss.com/). +The frontend is built with [Vite](https://vitejs.dev/), [React](https://react.dev/), [TypeScript](https://www.typescriptlang.org/), [TanStack Query](https://tanstack.com/query), [TanStack Router](https://tanstack.com/router), [Tailwind CSS](https://tailwindcss.com/), and [shadcn/ui](https://ui.shadcn.com/). ## Requirements -- [Bun](https://bun.sh/) (recommended) or [Node.js](https://nodejs.org/) +- [Bun](https://bun.sh/) ## Quick Start +From the project root, install the dependencies and start the frontend development server: + ```bash bun install bun run dev ``` -* Then open your browser at http://localhost:5173/. +Then open in your browser. Run `uv run bash scripts/prestart.sh` and `uv run fastapi dev` from the `backend` directory, with PostgreSQL running in Docker Compose. See [../development.md](../development.md) for the complete setup. To serve the frontend with FastAPI, run `bun run build` from the `frontend` directory and open `http://localhost:8000`. -Check the file `package.json` to see other available options. +Check `frontend/package.json` to see the other available commands. -### Removing the frontend +## Removing the Frontend If you are developing an API-only app and want to remove the frontend, you can do it easily: @@ -37,13 +39,13 @@ If you are developing an API-only app and want to remove the frontend, you can d * In the `.fastapicloudignore` file, remove the `!backend/app/frontend/` entry. -Done, you have a frontend-less (api-only) app. ๐Ÿค“ +Done, you now have an API-only app. ๐Ÿค“ ## Generate Client ### Automatically -* From the top level project directory, run the script: +* From the project root, run the script: ```bash bash ./scripts/generate-client.sh @@ -65,7 +67,7 @@ bun run generate-client * Commit the changes. -Notice that everytime the backend changes (changing the OpenAPI schema), you should follow these steps again to update the frontend client. +Regenerate the client whenever backend changes affect the OpenAPI schema. ## Using a Remote API @@ -82,11 +84,12 @@ Then, when you run the frontend, it will use that URL as the base URL for the AP The frontend code is structured as follows: * `frontend/src` - The main frontend code. -* `frontend/src/assets` - Static assets. +* `frontend/public` - Static assets. * `frontend/src/client` - The generated OpenAPI client. -* `frontend/src/components` - The different components of the frontend. +* `frontend/src/components` - The components of the frontend, including the shadcn/ui components in `frontend/src/components/ui`. * `frontend/src/hooks` - Custom hooks. -* `frontend/src/routes` - The different routes of the frontend which include the pages. +* `frontend/src/lib` - Shared frontend utilities. +* `frontend/src/routes` - The frontend routes and pages. ## End-to-End Testing with Playwright