Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
.vscode
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM ubuntu:16.04

RUN apt-get update -y
RUN apt-get install -y python3 python3-pip python3-dev build-essential libssl-dev libffi-dev

COPY . /app

WORKDIR /app

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

CMD ["python3", "run.py"]
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ This is a simple example of Python Flask's usage as a simple "Task Manager" usin

### Installation

#### Docker

Using Docker compose, you can run this using one command:

```bash
$ docker-compose up
```

It will run pip install and also pull mongodb container as requirement of this project. Then you can access `localhost:80` (yes it is in port 80 since in `docker-compose.yml` it exposed to host port 80, you can change it if you like!).

#### Old way

> Don't forget to install python 3 if you don't have it, because this project use python 3 syntax such as `print()` function.

First, you should [install MongoDB](https://docs.mongodb.com/manual/installation/)

then install all dependencies by run the following command:
Expand All @@ -28,7 +42,7 @@ then, run the program:
$ python run.py
```

Open your browser and go to `localhost:5000 ` to see the running program.
Open your browser and go to `localhost:5000` to see the running program.

### External References:

Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '2'
services:
mongo:
image: mongo:3.4
volumes:
- /data/db
flask_mongodb:
build: .
ports:
- "80:5000"
environment:
- PORT=5000
- MONGODB_URL=mongo:27017
- SECRET_KEY=1234567890asdfghjkl
links:
- mongo
depends_on:
- mongo
18 changes: 15 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import os
from flask import Flask, render_template, redirect
from pymongo import MongoClient
from classes import *

# get from env variable, follows 12 factor app (https://12factor.net/config)
SECRET_KEY = os.getenv('SECRET_KEY', 'randomkeys')
MONGODB_URL = os.getenv('MONGODB_URL', 'localhost:27017')
PORT = os.getenv('PORT', '5000')

# config system
app = Flask(__name__)
app.config.update(dict(SECRET_KEY='yoursecretkey'))
client = MongoClient('localhost:27017')
app.config.update(dict(SECRET_KEY=SECRET_KEY))
client = MongoClient(MONGODB_URL)
db = client.TaskManager

if db.settings.find({'name': 'task_id'}).count() <= 0:
Expand Down Expand Up @@ -92,4 +98,10 @@ def main():
data = data, reset = reset)

if __name__=='__main__':
app.run(debug=True)
# The web server running in your container is listening for connections on port 5000
# #of the loopback network interface (127.0.0.1). As such this web server will only respond to http requests
# originating from that container itself.
# In order for the web server to accept connections originating from outside of the container
# you need to have it bind to the 0.0.0.0 IP address.
# see answer here https://stackoverflow.com/a/26434706/5489910
app.run(host='0.0.0.0', port=PORT)