Skip to content

Commit 1e6b21e

Browse files
committed
Added NGINX proxy for app/api and improved documentation
1 parent 6382307 commit 1e6b21e

22 files changed

+629
-287
lines changed

.env.example

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FLASK_API_VERSION="1.1"
2+
FLASK_SERVER_NAME="My API Project"
3+
FLASK_SERVER_DESCRIPTION="Dockerized Flask API boilerplate using an LDAP and token-based authentication"
4+
FLASK_SECRET_KEY="Some secret key"
5+
FLASK_LEVEL="dev" # dev, test or prod
6+
LOG_LEVEL="DEBUG" # DEBUG, INFO, WARNING or ERROR
7+
8+
LDAP_ORGANISATION="My Company"
9+
LDAP_DOMAIN="mycompany.com"
10+
LDAP_HOST="ldap"
11+
LDAP_SCHEME="ldap" # "ldaps" if using secure LDAP, "ldap" else
12+
LDAP_PORT=389
13+
LDAP_USERS_DN="dc=mycompany,dc=com"
14+
LDAP_ADMIN_DN="cn=admin,dc=mycompany,dc=com"
15+
LDAP_ADMIN_PASSWORD="adminpwd"
16+
17+
POSTGRES_HOST="database"
18+
POSTGRES_PORT=5432
19+
POSTGRES_USER="myproject"
20+
POSTGRES_PASSWORD="myprojectpwd"
21+
POSTGRES_DB="myproject"

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.vscode/
22

3+
.env
4+
35
web/app/node_modules/
4-
ldap/
56
logs/
6-
migrations/
77

88
api/database.mwb.bak

README.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</a>
77
<a href="https://codebeat.co/projects/github-com-flavienbwk-reactjs-flask-ldap-boilerplate-master"><img alt="codebeat badge" src="https://codebeat.co/badges/940a0bd0-5aa5-4f96-b6fc-39b6e1b7e14b" /></a>
88
</p>
9-
<p align="center">ReactJS + Flask + Docker<br/>boilerplate using a token-based LDAP authentication</p>
9+
<p align="center">ReactJS + Flask + Docker (+ K8S)<br/>boilerplate using a token-based LDAP authentication</p>
1010

1111
> :smiley: Suggestions and feedbacks are [highly appreciated](https://github.com/flavienbwk/reactjs-flask-ldap-boilerplate/issues/new)
1212
@@ -20,12 +20,13 @@
2020
- Flask-Migrate
2121
- Flask-SQLAlchemy (PostgreSQL was chosen)
2222
- [Logging and logs rotation](./api/app/utils/Logger.py#L12)
23-
- [Choose](./app/app/src/App.js#L64) between sidebar and navbar (or use both !)
23+
- [Choose](./app/app/src/App.js#L65) between sidebar and navbar (or use both !)
2424
- Responsive design
25+
- Production and development builds
2526

2627
## API documentation
2728

28-
I chose to use Swagger to document the API. Just run the API following the steps below and browse to [`http://localhost:5000`](http://localhost:5000)
29+
I chose to use Swagger to document the API. Run the API following the steps below and go to [`http://localhost:5000`](http://localhost:5000).
2930

3031
Here you can take a look at the database architecture scheme :
3132

@@ -35,76 +36,76 @@ Here you can take a look at the database architecture scheme :
3536

3637
> Reminder : there is no `password` field because we use LDAP for authentication.
3738
38-
## Setting up the API
39+
## Getting started (development)
3940

4041
The API is made to run with an LDAP server for managing users. Whether use the provided Docker LDAP server or remove the conf. in [`docker-compose.yml`](./docker-compose.yml) and use your own LDAP server.
4142

4243
This section will explain to you how to run this project and set-up the LDAP server with one user.
4344

44-
### Starting services
45+
### 1. Starting authentication services
4546

46-
First, please change the database/LDAP passwords and keys in `docker-compose.yml`
47+
1. Copy the `.env.example` to `.env`
4748

48-
Then, run :
49+
```bash
50+
cp .env.example .env
51+
```
4952

50-
```bash
51-
docker-compose up ldap phpldapadmin database adminer -d
52-
```
53+
> This is a good practice so your `.env` can't be committed along with your modifications (is in `.gitignore`)
54+
55+
2. Change the database/LDAP passwords and keys in `.env`
56+
57+
You can now run :
5358
54-
> **adminer** (PostgreSQL management) will be available through `http://localhost:8082`
55-
> **phpLDAPAdmin** (LDAP management) will be available through `https://localhost:8081`
59+
```bash
60+
docker-compose up ldap phpldapadmin database adminer -d
61+
```
5662
57-
### Creating the first user in the LDAP
63+
- **adminer** (PostgreSQL management) will be available at `http://localhost:8082`
64+
- **phpLDAPAdmin** (LDAP management) will be available at `https://localhost:8081`
5865
59-
Access phpLDAPAdmin with : `https://localhost:8081`
66+
### 2. Creating the first user in the LDAP
6067
61-
If you are not familiar with LDAP, [read my LDAP user creation guide](./CREATE_LDAP_USER.md) to add your first user
68+
Access phpLDAPAdmin at `https://localhost:8081` and [follow the LDAP user creation guide](./CREATE_LDAP_USER.md) to add your first user
6269
63-
### Run the API
70+
### 3. NGINX reverse-proxy
6471
65-
The database will be automatically set-up thanks to Flask Migrate and any future modification brought to [models](./api/app/model) will be automatically applied to the database when the API is **restarted**.
72+
This boilerplate includes NGINX as a reverse proxy so we can have a unique endpoint for our app and API. Else, we would have to open two endpoints : one for the app, the other for the API.
6673
6774
```bash
68-
docker-compose up api
75+
docker-compose up --build -d nginx
6976
```
7077
71-
Access the API and its documentation browsing [`http://localhost:5000`](http://localhost:5000)
78+
NGINX will auto restart until you have started the app and API below.
7279
73-
## Setting up the web application
80+
### 4. Run the API
7481
75-
:clock9: This step may take quite a lot of time due to npm's initial modules download
82+
The database will be automatically set-up thanks to Flask Migrate and any future modification brought to [models](./api/app/model) will be automatically applied when the API is **restarted**.
7683
77-
Just run :
84+
You might wait some time before the database get updated after starting the API :
7885
7986
```bash
80-
# Build is quick, **first** launch is long (expect at least 5 min.)
81-
docker-compose up app
87+
docker-compose up --build -d api
8288
```
8389
84-
You can now enjoy the app on [`http://localhost:8080`](http://localhost:8080)
85-
86-
> :information_source: If you want to add a dependency, just stop & re-launch a `docker-compose up app`. You won't have to wait as for first launch.
87-
88-
## Why using LDAP authentication ?
90+
> For development, go to [`http://localhost:5000`](http://localhost:5000) to access the API documentation
8991
90-
LDAP services are used in a lot of companies and institutions around the world to manage their user accounts and rights in a central place.
92+
### 5. Run the web application
9193
92-
With this boilerplate, you will be able to develop corporate-ready services AND avoid yourself the troubles of developing registration / password forgotten / change password / profile update code.
94+
:clock9: NPM's initial install may take quite a lot of time
9395

94-
## Left TODOs
96+
Start the app :
9597

96-
API :
98+
```bash
99+
# Expect several minutes for first launch (npm install)
100+
docker-compose up --build -d app
101+
```
97102

98-
_Completed_
103+
You can now enjoy the app on [`http://localhost:8080`](http://localhost:8080)
99104

100-
App :
105+
> :information_source: If you want to add a NPM package, just stop & re-launch `docker-compose up app`.
101106

102-
_Completed_
107+
## Why using LDAP authentication ?
103108

104-
Architecture :
109+
LDAP services are used in a lot of companies and institutions around the world to manage their user accounts and rights in a central place.
105110

106-
- [P3] Add "Deploy to prod" guide in README
107-
- [P3] Create a `prod.docker-compose.yml` file that :
108-
- Uses NGINX with SSL
109-
- Builds & serves the front-end
110-
- Disables Swagger UI
111+
With this boilerplate, you will be able to develop corporate-ready services AND avoid yourself the troubles of developing registration / password forgotten / change password / profile update code.

api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM python:3.7-alpine
22

33
# python-ldap requirements
4-
RUN apk add openldap-dev libc-dev gcc
4+
RUN apk update && apk add openldap-dev libc-dev gcc g++
55

66
# psycopg2 requirements
77
RUN apk add libpq python3-dev musl-dev postgresql-dev

api/app/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ def create_app():
2121
app = Flask(__name__)
2222
app.register_error_handler(404, page_not_found)
2323
app.config.from_object(config_by_name[FLASK_LEVEL])
24+
os.environ["FLASK_ENV"] = config_by_name[FLASK_LEVEL].FLASK_ENV
2425
database.initDatabase(app)
2526
return app

api/app/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ class Config:
2020

2121
class DevelopmentConfig(Config):
2222
DEBUG = True
23-
SQLALCHEMY_TRACK_MODIFICATIONS = False
23+
SQLALCHEMY_TRACK_MODIFICATIONS = True
24+
FLASK_ENV = "development"
2425

2526

2627
class TestingConfig(Config):
2728
DEBUG = True
2829
TESTING = True
2930
PRESERVE_CONTEXT_ON_EXCEPTION = False
3031
SQLALCHEMY_TRACK_MODIFICATIONS = False
32+
FLASK_ENV = "development"
3133

3234

3335
class ProductionConfig(Config):
3436
DEBUG = False
37+
FLASK_ENV = "production"
3538

3639

3740
config_by_name = dict(

0 commit comments

Comments
 (0)