-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·54 lines (39 loc) · 1.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
import os
import random
import string
from flask import Flask, request
from sqlalchemy import Table, Column, Integer, String, MetaData, create_engine, sql
app = Flask(__name__)
DB_USER = os.environ["DB_USER"]
DB_PASSWORD = os.environ["DB_PASSWORD"]
DB_HOST = os.environ["DB_HOST"]
DB_PORT = os.environ["DB_PORT"]
DB_NAME = os.environ["DB_NAME"]
engine = create_engine(f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}")
conn = engine.connect()
metadata = MetaData()
users = Table(
'users', metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
)
@app.route("/")
def hello():
return "Try urls: /createtable, /insert, /select"
@app.route("/createtable")
def createtable():
res = metadata.create_all(engine)
return f"{res=}"
@app.route("/insert")
def insert():
name = ''.join(random.choices(string.ascii_lowercase, k=10))
insert = users.insert().values(name=name)
res = conn.execute(insert)
return({res.inserted_primary_key[0]: name})
@app.route("/select")
def select():
res = conn.execute(sql.select(users))
return {id: name for id, name in res}
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80, debug=True)