Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init with db #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/init.sql
3 changes: 3 additions & 0 deletions database/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

CREATE USER this_user WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE "postgres" to this_user;
16 changes: 15 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
version: '2'

services:

postgres:
restart: always
image: postgres:latest
build: database
volumes:
- /var/lib/postgresql
ports:
- 54322:5432

product-service:
build: ./python
restart: always
build: python
volumes:
- ./python:/usr/src/app
ports:
- 5555:80
depends_on:
- postgres

website:
image: php:apache
volumes:
Expand Down
6 changes: 4 additions & 2 deletions python/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FROM python:3-onbuild
COPY . /usr/src/app
CMD ["python", "api.py"]
COPY . /usr/src/app
ENTRYPOINT ["bash", "docker-entrypoint.sh", "python", "api.py"]
CMD ["--no-daemonize"]

Binary file added python/__pycache__/api.cpython-36.pyc
Binary file not shown.
Binary file added python/__pycache__/models.cpython-36.pyc
Binary file not shown.
24 changes: 21 additions & 3 deletions python/api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
from flask import Flask
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy
import json


################
#### config ####
################
app = Flask(__name__)
api = Api(app)
app.config.from_pyfile('flask.cfg')
db = SQLAlchemy(app)

import views



class Product(Resource):
def get(self):
return {
'products': ['geomapfish', 'georchestra', 'geonetwork']
'products': ['geomapfish', 'georchestra', 'geonetwork' ]
}


# routes
api.add_resource(Product, '/')

@app.route('/db')
def index():
names = []
for result in views.articles():
names.append(result.article_name)
return json.dumps(names) # '[



if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)
16 changes: 16 additions & 0 deletions python/db_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from api import db
from models import Article


# create the database and the database table
db.create_all()

# insert recipe data
ar1 = Article('Geomapfish', 'magnifique')
ar2 = Article('OpenLayers', 'aussi')
db.session.add(ar1)
db.session.add(ar2)


# commit the changes
db.session.commit()
5 changes: 5 additions & 0 deletions python/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

python db_create.py

exec "$@"
3 changes: 3 additions & 0 deletions python/flask.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SQLAlchemy
SQLALCHEMY_DATABASE_URI = 'postgresql://this_user:password@postgres/postgres'
SQLALCHEMY_TRACK_MODIFICATIONS = True
16 changes: 16 additions & 0 deletions python/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from api import db

class Article(db.Model):

__tablename__ = "articles"

article_id = db.Column(db.Integer, primary_key=True)
article_name = db.Column(db.String(200), unique=False, nullable=True)
article_desc = db.Column(db.String(200), unique=False, nullable=True)

def __init__(self, title, description):
self.article_name = title
self.article_desc = description

def __repr__(self):
return '<title {}'.format(self.article_name)
3 changes: 3 additions & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Flask==0.12
flask-restful==0.3.5
flask_sqlalchemy==2.3.2
psycopg2
ipdb
4 changes: 4 additions & 0 deletions python/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from models import Article

def articles():
return Article.query.all()
10 changes: 10 additions & 0 deletions website/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@

?>
</ul>
<ul>
<h1> Using the database </h1>
<?php
$jsons = file_get_contents('http://product-service/db');
$results = json_decode($jsons);
foreach ($results as $result) {
echo "<li>$result</li>";
}
?>
</ul>
</body>
</html>