-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
75 lines (59 loc) · 1.96 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import datetime
import logging
import os
from flask import Flask
from flask_cors import CORS
import sqlalchemy
import requests
from apis import api
def onGAE():
return os.getenv('GAE_ENV', '').startswith('standard')
app = Flask(__name__)
CORS(app)
api.init_app(app)
app.config['JSON_AS_ASCII'] = False
db_user = os.environ.get('DB_USER')
db_pass = os.environ.get('DB_PASS')
db_name = os.environ.get('DB_NAME')
cloud_sql_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME')
slack_webhook_url = os.environ.get('SLACK_WEBHOOK_URL')
if onGAE():
import google.cloud.logging
client = google.cloud.logging.Client('yori-server')
client.setup_logging(logging.INFO)
connection_string = sqlalchemy.engine.url.URL(
drivername='mysql+pymysql',
username=db_user,
password=db_pass,
database=db_name,
query={
'unix_socket': '/cloudsql/{}'.format(cloud_sql_connection_name)
})
else:
connection_string = sqlalchemy.engine.url.URL(
drivername='mysql+pymysql',
username=db_user,
password=db_pass,
database=db_name
)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
db = sqlalchemy.create_engine(connection_string)
# TODO:
# We should create a package and move this into __init__.py of that package
def notify():
logger.info('will notify {}'.format(slack_webhook_url))
if slack_webhook_url:
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT')
gae_version = os.environ.get('GAE_VERSION')
gae_instance = os.environ.get('GAE_INSTANCE')
message = 'Launch {0}. GAE_VERSION: {1} on GAE_INSTANCE: {2}'.format(
project_id, gae_version, gae_instance)
logger.info(message)
requests.post(slack_webhook_url, json={'text': message})
# TODO:
# We should create a package and move this into __init__.py of that package
if onGAE():
notify()
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)