Skip to content

Commit 3dd386e

Browse files
authored
Merge pull request #235 from Seluj78/234-env-and-docker-refactor
2 parents 83b8672 + b479379 commit 3dd386e

22 files changed

+286
-139
lines changed

.env.enc

-64 Bytes
Binary file not shown.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,7 @@ yarn-error.log*
130130
node_modules
131131
frontend/node_modules
132132
www/
133-
*.db
133+
*.db
134+
.env.ci
135+
.env.dev
136+
.env.docker

.travis.yml

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,40 @@ jobs:
2121
# login to github registry
2222
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin docker.pkg.github.com
2323
# build image
24-
- docker build -t docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest .
24+
- docker build -t docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest . -f backend.Dockerfile
2525
# push image
2626
- docker push docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest
27-
# - stage: test
28-
# before_script:
29-
# - node --version
30-
# - npm --version
31-
# - npm install -g newman
32-
# - newman --version
33-
# script:
34-
# # login to github registry
35-
# - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin docker.pkg.github.com
36-
# # pull images
37-
# - docker pull docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest
38-
# # decrypt the env file
39-
# - openssl aes-256-cbc -K $encrypted_3c84dcdc6bbe_key -iv $encrypted_3c84dcdc6bbe_iv -in .env.enc -out .env -d
40-
# # Run the backend api
41-
# - docker run -d -p 5000:5000 --restart always --env-file .env docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest
42-
# # Wait for docker to be up
43-
# - sleep 10
44-
# # run newman tests
45-
# - newman run --color on 'PyMatcha.postman_collection.json'
46-
# after_script:
47-
# # stop and remove docker to prevent them from block port at next iteration/test
48-
# - docker stop $(docker ps -a -q)
49-
# - docker rm $(docker ps -a -q)
27+
- stage: test
28+
before_script:
29+
- node --version
30+
- npm --version
31+
- npm install -g newman
32+
- newman --version
33+
- wget https://repo.mysql.com//mysql-apt-config_0.8.15-1_all.deb
34+
- sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb
35+
- sudo apt-get update -q
36+
- sudo apt-get install -q -y --allow-unauthenticated -o Dpkg::Options::=--force-confnew mysql-server
37+
- sudo systemctl restart mysql
38+
- sudo mysql_upgrade
39+
- mysql --version
40+
- mysql -e 'CREATE DATABASE pymatcha;'
41+
script:
42+
# login to github registry
43+
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin docker.pkg.github.com
44+
# pull images
45+
- docker pull docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest
46+
# decrypt the env file
47+
- openssl aes-256-cbc -K $encrypted_3c84dcdc6bbe_key -iv $encrypted_3c84dcdc6bbe_iv -in .env.enc -out .env -d
48+
# Run the backend api
49+
- docker run -d -p 5000:5000 --restart always --env-file .env --network="host" docker.pkg.github.com/seluj78/pymatcha/pymatcha-api:latest
50+
# Wait for docker to be up
51+
- sleep 10
52+
# run newman tests
53+
- newman run --color on 'PyMatcha.postman_collection.json'
54+
after_script:
55+
# stop and remove docker to prevent them from block port at next iteration/test
56+
- docker stop $(docker ps -a -q)
57+
- docker rm $(docker ps -a -q)
5058
- stage: lint
5159
before_script:
5260
- pip install -r backend/requirements.txt

PyMatcha.postman_collection.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,7 +4821,7 @@
48214821
" pm.response.to.have.status(200);",
48224822
"});",
48234823
"",
4824-
"tests[\"Message is correct\"] = response.message == \"No new messages.\""
4824+
"tests[\"No new messages\"] = response.new_messages.length == 0"
48254825
],
48264826
"type": "text/javascript"
48274827
}
@@ -5856,92 +5856,92 @@
58565856
],
58575857
"variable": [
58585858
{
5859-
"id": "dff1a2e8-8383-4d1a-af74-6744539983c6",
5859+
"id": "3b1c4a05-afc9-4d76-92f8-a1eead538b43",
58605860
"key": "host",
58615861
"value": "http://127.0.0.1:5000"
58625862
},
58635863
{
5864-
"id": "2baf602e-1e34-4efc-957c-bbd23051fa18",
5864+
"id": "0f7fc70d-5f57-4d34-b25e-ec9a643d6af6",
58655865
"key": "user_id",
58665866
"value": ""
58675867
},
58685868
{
5869-
"id": "dd71d2c4-7078-40ea-af2b-69ebed33895e",
5869+
"id": "6b79a53d-16ca-4170-ad83-e2843c6b61ed",
58705870
"key": "user_email",
58715871
"value": "foo@example.org"
58725872
},
58735873
{
5874-
"id": "2c6ace6d-9880-47c0-a303-d57c85c11826",
5874+
"id": "5cc4b3de-ea74-49db-a6b5-c729f923ca5b",
58755875
"key": "user_username",
58765876
"value": "foo"
58775877
},
58785878
{
5879-
"id": "27762741-f737-47db-9982-bb6382a1102d",
5879+
"id": "08841b46-3845-454e-96ce-838996790ab6",
58805880
"key": "user_password",
58815881
"value": "admin"
58825882
},
58835883
{
5884-
"id": "32749353-1734-427a-b7e5-ea42f9bf0a81",
5884+
"id": "dbe5daf6-9aa1-42ff-bd6b-e09ab2dccbf0",
58855885
"key": "user_firstname",
58865886
"value": "Foo"
58875887
},
58885888
{
5889-
"id": "dfd31259-5a05-45b5-9b15-00565a3621f6",
5889+
"id": "10b3e4e0-1da8-40b7-b4fe-e5f60617bb78",
58905890
"key": "user_lastname",
58915891
"value": "Bar"
58925892
},
58935893
{
5894-
"id": "6fbac55b-72a7-4dda-9df6-a5b54b8e075a",
5894+
"id": "e29281ab-3bd4-4608-8b90-ee5658b81bbe",
58955895
"key": "debug_token",
58965896
"value": "xX69jules69Xx"
58975897
},
58985898
{
5899-
"id": "469a1742-e7ea-4784-8383-304e1a1254c8",
5899+
"id": "f3296656-44a1-4108-a326-57ee1d7b7da5",
59005900
"key": "user_access_token",
59015901
"value": ""
59025902
},
59035903
{
5904-
"id": "b4e018f7-21e5-4cc8-aa5d-b44f1d3fe5aa",
5904+
"id": "b06ae0b3-5690-42da-a641-93f81329a487",
59055905
"key": "user_refresh_token",
59065906
"value": ""
59075907
},
59085908
{
5909-
"id": "35a30c5c-700b-4cac-a87f-e9b85b1d59d5",
5909+
"id": "a72e4b18-16c0-4126-a6b3-e13d493369a6",
59105910
"key": "expired_token",
59115911
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTAyNDIyNjQsIm5iZiI6MTU5MDI0MjI2NCwianRpIjoiNTM0ZGI5NzQtOWE4Ni00MGViLWE1NDEtMDg3N2ZmNzQ3NDVhIiwiZXhwIjoxNTkwMjQyMzI0LCJpZGVudGl0eSI6eyJpZCI6NTEyLCJlbWFpbCI6ImZvb0BleGFtcGxlLm9yZyIsInVzZXJuYW1lIjoiYmFyIiwiaXNfb25saW5lIjp0cnVlLCJkYXRlX2xhc3RzZWVuIjoiU2F0LCAyMyBNYXkgMjAyMCAxMzo1Nzo0NCBHTVQifSwiZnJlc2giOnRydWUsInR5cGUiOiJhY2Nlc3MifQ.NTp2P0WNkXDwzuzrcNIQdeAfizQ57HQgkzBLSvY1yHU"
59125912
},
59135913
{
5914-
"id": "f9cb0a56-3fe5-4a43-8c34-c370ea32d68c",
5914+
"id": "d062d4f6-53e1-4a15-a66b-a138edf786ef",
59155915
"key": "user_orientation",
59165916
"value": "heterosexual"
59175917
},
59185918
{
5919-
"id": "473c2ba6-4781-4074-8ab7-b2d593ba027f",
5919+
"id": "d4cf3d62-6dff-4da5-843b-4c124e4c122a",
59205920
"key": "user_bio",
59215921
"value": "Lorem Ipsum is the single greatest threat. We are not - we are not keeping up with other websites. Lorem Ipsum best not make any more threats to your website. It will be met with fire and fury like the world has never seen. Does everybody know that pig named Lorem Ipsum? An ‘extremely credible source’ has called my office and told me that Barack Obama’s placeholder text is a fraud."
59225922
},
59235923
{
5924-
"id": "aa576ca8-3df8-4aa9-9365-d97b17a2fae9",
5924+
"id": "e51923fb-9335-459b-97ea-2dee4f464a8a",
59255925
"key": "user_gender",
59265926
"value": "male"
59275927
},
59285928
{
5929-
"id": "7295072e-641d-4991-8d9e-5b4219ddcbe3",
5929+
"id": "d6eccdcb-6a27-499e-a8ac-1474ad749b5e",
59305930
"key": "user_birthdate",
59315931
"value": "06/04/1998"
59325932
},
59335933
{
5934-
"id": "dee264ed-d89e-4493-92c9-3329e0864680",
5934+
"id": "142a5845-34bb-48bc-8895-30d467a99a6b",
59355935
"key": "user_lat",
59365936
"value": "44.8404"
59375937
},
59385938
{
5939-
"id": "1afbbe42-4c06-40e3-bc8c-775865907ec2",
5939+
"id": "9584624a-8a2e-4e4d-9833-42edcbad4625",
59405940
"key": "user_lng",
59415941
"value": "-0.5805"
59425942
},
59435943
{
5944-
"id": "e8028fd5-ba0c-474c-99a6-99f6aef3dac1",
5944+
"id": "09eab2dc-f84b-4982-b474-4465b7f87bb5",
59455945
"key": "user_ip",
59465946
"value": "77.205.46.181"
59475947
}

auteur

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
jlasne gmorer
1+
jlasne lskrauci
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
FROM python:3.8.3-buster
22
WORKDIR /www
33
ADD backend/requirements.txt .
4-
ADD .env .
5-
RUN export $(cat .env | xargs)
64
RUN pip install -r requirements.txt
75
ADD backend .
86
ENV PYTHONDONTWRITEBYTECODE 1
97
EXPOSE 5000
8+
ADD .env .
9+
RUN export $(cat .env | xargs)
1010
CMD exec gunicorn --chdir /www --bind :5000 --workers 1 --threads 1 PyMatcha:application

backend/PyMatcha/__init__.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,45 @@
3838
load_dotenv(dotenv_path)
3939

4040
REQUIRED_ENV_VARS = [
41+
"FLASK_PORT",
4142
"FLASK_DEBUG",
43+
"FLASK_HOST",
4244
"FLASK_SECRET_KEY",
43-
"FRONT_STATIC_FOLDER",
45+
"ENABLE_LOGGING",
46+
"CELERY_BROKER_URL",
47+
"CELERY_RESULT_BACKEND",
4448
"DB_HOST",
4549
"DB_PORT",
4650
"DB_USER",
4751
"DB_PASSWORD",
52+
"DB_NAME",
4853
"MAIL_PASSWORD",
49-
"APP_URL",
50-
"ENABLE_LOGGING",
54+
"REDIS_HOST",
55+
"REDIS_PORT",
5156
"DEBUG_AUTH_TOKEN",
57+
"FRONTEND_BASE_URL",
5258
]
5359

5460
for item in REQUIRED_ENV_VARS:
5561
if item not in os.environ:
5662
raise EnvironmentError(f"{item} is not set in the server's environment or .env file. It is required.")
5763

58-
if os.getenv("ENABLE_LOGGING") == "True":
64+
from PyMatcha.utils.static import (
65+
ENABLE_LOGGING,
66+
FLASK_SECRET_KEY,
67+
CELERY_RESULT_BACKEND,
68+
CELERY_BROKER_URL,
69+
DB_NAME,
70+
DB_USER,
71+
DB_PASSWORD,
72+
DB_PORT,
73+
DB_HOST,
74+
MAIL_PASSWORD,
75+
REDIS_PORT,
76+
REDIS_HOST,
77+
)
78+
79+
if ENABLE_LOGGING == "True":
5980
setup_logging()
6081

6182
application = Flask(__name__)
@@ -65,13 +86,11 @@
6586
else:
6687
application.debug = False
6788

68-
application.secret_key = os.getenv("FLASK_SECRET_KEY")
69-
application.config.update(FLASK_SECRET_KEY=os.getenv("FLASK_SECRET_KEY"))
70-
application.config["JWT_SECRET_KEY"] = os.environ.get("FLASK_SECRET_KEY")
89+
application.secret_key = FLASK_SECRET_KEY
90+
application.config.update(FLASK_SECRET_KEY=FLASK_SECRET_KEY)
91+
application.config["JWT_SECRET_KEY"] = FLASK_SECRET_KEY
7192

7293
logging.debug("Configuring Celery Redis URLs")
73-
CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL", "redis://localhost:6379/0")
74-
CELERY_RESULT_BACKEND = os.getenv("CELERY_RESULT_BACKEND", "redis://localhost:6379/0")
7594
# Celery configuration
7695
application.config["CELERY_BROKER_URL"] = CELERY_BROKER_URL
7796
application.config["CELERY_RESULT_BACKEND"] = CELERY_RESULT_BACKEND
@@ -114,18 +133,13 @@ def expired_token_callback(expired_token):
114133
logging.debug("Configuring CORS")
115134
CORS(application, expose_headers="Authorization", supports_credentials=True)
116135

117-
if os.getenv("CI"):
118-
database_password = ""
119-
else:
120-
database_password = os.getenv("DB_PASSWORD")
121-
122136
logging.debug("Setting database config from environment variables")
123137
database_config = {
124-
"host": os.getenv("DB_HOST"),
125-
"port": int(os.getenv("DB_PORT")),
126-
"user": os.getenv("DB_USER"),
127-
"password": database_password,
128-
"db": os.getenv("DB_NAME"),
138+
"host": DB_HOST,
139+
"port": int(DB_PORT),
140+
"user": DB_USER,
141+
"password": DB_PASSWORD,
142+
"db": DB_NAME,
129143
"charset": "utf8mb4",
130144
"cursorclass": DictCursor,
131145
}
@@ -142,16 +156,14 @@ def expired_token_callback(expired_token):
142156
MAIL_PORT=465,
143157
MAIL_USE_SSL=True,
144158
MAIL_USERNAME="pymatcha@gmail.com",
145-
MAIL_PASSWORD=os.getenv("MAIL_PASSWORD"),
159+
MAIL_PASSWORD=MAIL_PASSWORD,
146160
MAIL_DEBUG=False,
147161
MAIL_DEFAULT_SENDER="pymatcha@gmail.com",
148162
)
149163
logging.debug("Configuring mail")
150164
mail = Mail(application)
151165

152-
redis = StrictRedis(
153-
host=os.getenv("REDIS_HOST", "localhost"), port=os.getenv("REDIS_PORT", 6379), decode_responses=True, db=2
154-
)
166+
redis = StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True, db=2)
155167

156168
redis.flushdb()
157169

0 commit comments

Comments
 (0)