Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MYSQL_ROOT_PASSWORD=docker
MYSQL_USER=docker
MYSQL_PASSWORD=docker
MYSQL_HOST=db
MYSQL_DATABASE=hadithdb

FLASK_ENV=development
FLASK_APP=main.py
13 changes: 7 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.env/
*.py[cod]
__pycache__
.DS_Store

.vscode/
.coverage
*.pyc
*/__pycache__
db/*.sql
venv

credentials.database
.vscode/*
.env.local
15 changes: 6 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
FROM python:3.6
FROM python:3.8-slim

WORKDIR /home/app/api
ENV PYTHONUNBUFFERED 1

COPY requirements.txt $WORKDIR
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/

RUN pip install -r requirements.txt

COPY . $WORKDIR

ENV PYTHONUNBUFFERED 1

EXPOSE 5000
CMD python main.py
COPY . /code/
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@ python 3+ is required to run the project.

# Getting started

Please follow the instructions below
Please follow the instructions below.

```
First create a local `.env.local` configuration file and update values as needed.
A sample file is provided at `.env.local.sample`.

Run manually:
```bash
git clone REPO
cd REPO
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
mv credentials.database.sample credentials.database
<edit the credentials as needed>
python main.py
export FLASK_ENV=development FLASK_APP=main.py
flask run --host=0.0.0.0
```

# Build

Build using:
Or alternatively use `docker-compose` which will give a full environment with a MySQL instance loaded with a sample dataset:

```bash
$ docker build -t sunnah-com/api .
docker-compose up
```

Run with:

```bash
$ docker run --init -p 5000:5000 sunnah-com/api
```
Use the `-d` option ot run in detached mode.
Use `--network="host"` on Linux to have MySQL on the local host be accessible within the container as if it were on localhost.
* Use `--build` option to re-build.
* Use the `-d` option to run in detached mode.

You can then visit [localhost:5000](http://localhost:5000) to verify that it's running on your machine. Or, alternatively:

Expand Down
8 changes: 8 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
from dotenv import load_dotenv

load_dotenv('.env.local')

class Config(object):
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}/{MYSQL_DATABASE}'.format(**os.environ)
5 changes: 0 additions & 5 deletions credentials.database.sample

This file was deleted.

6 changes: 0 additions & 6 deletions database_credentials.py

This file was deleted.

250 changes: 250 additions & 0 deletions db/samplegitdb.sql

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'

services:
db:
image: mysql:5.7.22
command: --default-authentication-plugin=mysql_native_password
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
env_file:
- .env.local
web:
build: .
command: flask run --host=0.0.0.0
volumes:
- .:/code
ports:
- "5000:5000"
env_file:
- .env.local
6 changes: 1 addition & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from database_credentials import get_db_credentials
from flask import Flask, jsonify
from models import db, HadithCollection

app = Flask(__name__)
app.config['DEBUG'] = True
db_creds = get_db_credentials()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}'.format(**db_creds)
app.config.from_object('config.Config')
db.init_app(app)

hadiths = [
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ MarkupSafe==1.1.1
mccabe==0.6.1
pylint==2.5.2
PyMySQL==0.9.3
python-dotenv==0.13.0
six==1.15.0
SQLAlchemy==1.3.17
toml==0.10.1
Expand Down