|
|
@@ -6,7 +6,8 @@ import mysql.connector
|
|
|
passfile = open('/run/secrets/db-password', 'r')
|
|
|
|
|
|
#give db some time to start
|
|
|
-time.sleep(3)
|
|
|
+time.sleep(5)
|
|
|
+
|
|
|
#connect to db
|
|
|
conn = mysql.connector.connect(
|
|
|
user='root',
|
|
|
@@ -15,12 +16,19 @@ conn = mysql.connector.connect(
|
|
|
database='example',
|
|
|
auth_plugin='mysql_native_password'
|
|
|
)
|
|
|
-passfile.close()
|
|
|
|
|
|
+passfile.close()
|
|
|
+# populate db
|
|
|
cursor = conn.cursor()
|
|
|
+def prepare_db():
|
|
|
+ cursor.execute('DROP TABLE IF EXISTS blog')
|
|
|
+ cursor.execute('CREATE TABLE blog (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255))')
|
|
|
+ cursor.executemany('INSERT INTO blog (id, title) VALUES (%s, %s);', [(i, 'Blog post #%d'% i) for i in range (1,5)])
|
|
|
+ conn.commit()
|
|
|
+prepare_db()
|
|
|
|
|
|
+# server
|
|
|
app = Flask(__name__)
|
|
|
-
|
|
|
@app.route('/')
|
|
|
def listBlog():
|
|
|
cursor.execute('SELECT title FROM blog')
|
|
|
@@ -29,13 +37,6 @@ def listBlog():
|
|
|
response = response + '<div>' + c[0] + '</div>'
|
|
|
return response
|
|
|
|
|
|
-def prepare_db():
|
|
|
- cursor.execute('DROP TABLE IF EXISTS blog')
|
|
|
- cursor.execute('CREATE TABLE blog (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255))')
|
|
|
- cursor.executemany('INSERT INTO blog (id, title) VALUES (%s, %s);', [(i, 'Blog post #%d'% i) for i in range (1,5)])
|
|
|
- conn.commit()
|
|
|
-
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- prepare_db()
|
|
|
app.run()
|