1
1
# FastAPI + React Template · [ ![ CircleCI] ( https://circleci.com/gh/Buuntu/fastapi-react.svg?style=shield )] ( https://circleci.com/gh/Buuntu/fastapi-react ) [ ![ license] ( https://img.shields.io/github/license/peaceiris/actions-gh-pages.svg )] ( LICENSE ) [ ![ Dependabot Status] ( https://img.shields.io/badge/Dependabot-active-brightgreen.svg )] ( https://dependabot.com )
2
2
3
+ <div >
3
4
<img src =" assets/fastapi-logo.png " alt =" fastapi-logo " height =" 60 " /> <img
4
5
src="assets/react-logo.png" alt="react-logo" height="60" />   ;   ; <img
6
+ src="assets/react-admin.png" alt="react-admin" height="60" />   ;   ; <img
5
7
src="assets/typescript.png" alt="react-logo" height="60" />   ;  ;  ;
6
- <img src =" assets/postgres.png " alt =" react-logo " height =" 60 " />
7
- & nbsp ;& nbsp ;
8
- <img src = " assets/sql-alchemy.png " alt = " sql-alchemy " height = " 60 " / >
8
+ <img src =" assets/postgres.png " alt =" react-logo " height =" 60 " /> <img
9
+ src="assets/sql-alchemy.png" alt="sql-alchemy" height="60" />
10
+ </ div >
9
11
10
12
This project serves as a template for bootstrapping a FastAPI and React project
11
13
using a modern stack.
12
14
13
15
## Features
14
16
15
- 1 . ** [ FastAPI] ( https://fastapi.tiangolo.com/ ) ** (Python 3.8)
16
- 2 . ** [ React] ( https://reactjs.org/ ) ** (with Typescript)
17
- 3 . ** [ PostgreSQL] ( https://www.postgresql.org/ ) ** for the database
18
- 4 . ** [ SqlAlchemy] ( https://www.sqlalchemy.org/ ) ** for ORM
19
- 5 . ** [ Alembic] ( https://alembic.sqlalchemy.org/en/latest/ ) ** for database
20
- migrations
21
- 6 . ** [ Pytest] ( https://docs.pytest.org/en/latest/ ) ** for backend tests
22
- 7 . ** [ Prettier] ( https://prettier.io/ ) ** /** [ ESLint] ( https://eslint.org/ ) **
23
- (Airbnb style guide)
24
- 8 . ** [ Docker Compose] ( https://docs.docker.com/compose/ ) ** for development
25
- 9 . ** [ Nginx] ( https://www.nginx.com/ ) ** as a reverse proxy to allow
26
- backend/frontend on the same port
27
- 10 . [ ** MaterialUI** ] ( https://material-ui.com/ ) for styling
28
- 11 . [ ** react-admin** ] ( https://github.com/marmelab/react-admin ) for the admin
29
- dashboard
17
+ - ** [ FastAPI] ( https://fastapi.tiangolo.com/ ) ** (Python 3.8)
18
+ - ** [ React] ( https://reactjs.org/ ) ** (with Typescript)
19
+ - ** [ PostgreSQL] ( https://www.postgresql.org/ ) ** for the database
20
+ - ** [ SqlAlchemy] ( https://www.sqlalchemy.org/ ) ** for ORM
21
+ - ** [ Alembic] ( https://alembic.sqlalchemy.org/en/latest/ ) ** for database
22
+ migrations
23
+ - ** [ Pytest] ( https://docs.pytest.org/en/latest/ ) ** for backend tests
24
+ - Includes test database, client, and user fixtures
25
+ - ** [ Prettier] ( https://prettier.io/ ) ** /** [ ESLint] ( https://eslint.org/ ) ** (Airbnb
26
+ style guide)
27
+ - ** [ Docker Compose] ( https://docs.docker.com/compose/ ) ** for development
28
+ - ** [ Nginx] ( https://www.nginx.com/ ) ** as a reverse proxy to allow
29
+ backend/frontend on the same port
30
+ - ** [ MaterialUI] ( https://material-ui.com/ ) ** for styling
31
+ - ** [ react-admin] ( https://github.com/marmelab/react-admin ) ** for the admin
32
+ dashboard
33
+ - Using JWT authentication and login/redirects configured based on status
34
+ codes
35
+ - ** JWT** authentication using OAuth2 and PyJWT
30
36
31
37
## Background
32
38
33
39
This project is meant as a lightweight/React alternative to [ FastAPI's official
34
40
fullstack project] ( https://github.com/tiangolo/full-stack-fastapi-postgresql ) .
35
- If you want a fullstack, comprehensive project in Vue, I would suggest you start
41
+ If you want a more comprehensive project in Vue, I would suggest you start
36
42
there.
37
43
38
44
Most of the boilerplate backend code is taken from that project or the [ FastAPI
@@ -60,6 +66,10 @@ This will ask for the following variables to be set:
60
66
- port [ default 8000]
61
67
- postgres_user [ default postgres]
62
68
- postgres_password [ default password]
69
+ - postgres_database [ default app]
70
+ - initial_user_email
[ default
[email protected] ]
71
+ - initial_user_password [ default password]
72
+ - secret_key [ default super_secret]
63
73
64
74
and will create a directory called whatever you set for ` project_slug ` .
65
75
@@ -68,23 +78,35 @@ and will create a directory called whatever you set for `project_slug`.
68
78
Change into your project directory and run:
69
79
70
80
``` bash
71
- docker-compose up -d
72
- docker-compose run --rm backend alembic upgrade head
81
+ chmod +x scripts/build.sh
82
+ ./scripts/build.sh
73
83
```
74
84
75
- This will take a while to build the first time it's run since it needs to fetch
76
- all the docker images.
85
+ This will build and run the docker containers, run the alembic migrations, and
86
+ load the initial data (a test user).
87
+
88
+ It may take a while to build the first time it's run since it needs to fetch all
89
+ the docker images.
90
+
91
+ Once you've built the images once, you can simply use regular ` docker-compose `
92
+ commands to manage your development environment, for example to start your
93
+ containers:
94
+
95
+ ``` bash
96
+ docker-compose up -d
97
+ ```
77
98
78
99
Once this finishes you can navigate to the port set during setup (default is
79
100
` localhost:8000 ` ), you should see the slightly modified create-react-app page:
80
101
81
102
![ default create-react-app] ( assets/create-react-app.png )
82
103
83
- * Note : If you see an Nginx error at first with a ` 502: Bad Gateway ` page, you
84
- may have to wait for webpack to build the development server (the nginx
85
- container builds much more quickly).*
104
+ _ Note : If you see an Nginx error at first with a ` 502: Bad Gateway ` page, you
105
+ may have to wait for webpack to build the development server (the nginx
106
+ container builds much more quickly)._
86
107
87
- The backend docs will be at ` http://localhost:8000/api/docs ` .
108
+ The backend docs will be at ` http://localhost:8000/api/docs ` . ![ API
109
+ Docs] ( assets/api-docs.png )
88
110
89
111
Backend routes will be at ` http://localhost:8000/api ` .
90
112
@@ -93,16 +115,36 @@ Backend routes will be at `http://localhost:8000/api`.
93
115
This project uses [ react-admin] ( https://marmelab.com/react-admin/ ) for a highly
94
116
configurable admin dashboard.
95
117
96
- After starting the project, navigate to ` http://localhost:8000/admin ` . You
97
- should see a list of users, which you can edit, add, and delete. These are all
98
- based off of the ` users ` routes in the backend.
118
+ After starting the project, navigate to ` http://localhost:8000/admin ` . You
119
+ should see a login screen. Use the username/password you set for the initial
120
+ user on project setup.
121
+
122
+ ![ React Adming Login] ( assets/login-screen.png )
123
+
124
+ You should now see a list of users which you can edit, add, and delete. The
125
+ table is configured with the REST endpoints to the FastAPI ` /users ` routes in
126
+ the backend.
99
127
100
128
![ React Admin Dashboard] ( assets/admin-dashboard.png )
101
129
102
130
The admin dashboard is kept in the ` frontend/src/admin ` directory to keep it
103
131
separate from the regular frontend.
104
132
133
+ ## Security
134
+
135
+ To generate a secure key used for encrypting/decrypting the JSON Web Tokens, you can run this command:
136
+
137
+ ``` bash
138
+ openssl rand -hex 32
139
+ ```
140
+
141
+ The default is fine for development but you will want something more secure for
142
+ production.
143
+
144
+ You can either set this on project setup as ` secret_key ` or manually edit the
145
+ Python ` SECRET_KEY ` variable in ` backend/app/core/security.py ` .
146
+
105
147
## Contributing
106
148
107
- Contributing is more than welcome. Please read the [ Contributing
149
+ Contributing is more than welcome. Please read the [ Contributing
108
150
doc] ( CONTRIBUTING.md ) to find out more.
0 commit comments