Skip to content

Commit

Permalink
Merge pull request #2 from rgeniesse/add_kubernetes
Browse files Browse the repository at this point in the history
Remove DB items.
  • Loading branch information
rgeniesse authored Apr 24, 2024
2 parents a105886 + 1c4d2d5 commit fa3ede9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 61 deletions.
35 changes: 4 additions & 31 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,19 @@

app = Flask(__name__)

if 'POSTGRES_PASSWORD_FILE' in os.environ:
with open(os.environ['POSTGRES_PASSWORD_FILE'], 'r') as f:
password = f.read().strip()
else:
password = os.environ['POSTGRES_PASSWORD']

@app.route('/')
def hello_world():
return 'Hello, Docker my old friend!'


@app.route('/widgets')
def get_widgets():
with psycopg2.connect(host="db", user="postgres", password=password, database="example") as conn:
with conn.cursor() as cur:
cur.execute("SELECT * FROM widgets")
row_headers = [x[0] for x in cur.description]
results = cur.fetchall()
conn.close()

json_data = [dict(zip(row_headers, result)) for result in results]
return json.dumps(json_data)
def hello_world_widgets():
return 'Hello, Widgets my old friend!'


@app.route('/initdb')
def db_init():
conn = psycopg2.connect(host="db", user="postgres", password=password)
conn.set_session(autocommit=True)
with conn.cursor() as cur:
cur.execute("DROP DATABASE IF EXISTS example")
cur.execute("CREATE DATABASE example")
conn.close()

with psycopg2.connect(host="db", user="postgres", password=password, database="example") as conn:
with conn.cursor() as cur:
cur.execute("DROP TABLE IF EXISTS widgets")
cur.execute("CREATE TABLE widgets (name VARCHAR(255), description VARCHAR(255))")
conn.close()

return 'init database'
def hello_world_initdb():
return 'Hello, initdb my old friend!'


if __name__ == "__main__":
Expand Down
30 changes: 0 additions & 30 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,7 @@ services:
context: .
ports:
- 5000:5000
environment:
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
depends_on:
db:
condition: service_healthy
secrets:
- db-password
develop:
watch:
- action: rebuild
path: .
db:
image: postgres
restart: always
user: postgres
secrets:
- db-password
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=example
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
expose:
- 5432
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
volumes:
db-data:
secrets:
db-password:
file: db/password.txt

0 comments on commit fa3ede9

Please sign in to comment.