Skip to content

Commit

Permalink
Add logger and slack webhook, fixes #11 (#13)
Browse files Browse the repository at this point in the history
* Add logger and slack webhook

* Update connetion string and REAMDE to make local env work
  • Loading branch information
dwi2 authored Aug 4, 2019
1 parent ee63f8a commit ea69cbf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 24 deletions.
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Prerequisite

- Python 3
- [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy)

### Run Development Server

Expand All @@ -25,34 +26,43 @@
```sh
pip install -r requirements.txt
```

- Create a `secret.yaml` (if you haven't) and set your own environment variable
**NOTICE: do not upload your `secret.yaml` to repository**

```yaml
env_variables:
CLOUD_SQL_CONNECTION_NAME: your-connection-name
DB_USER: your-account
DB_PASS: your-password
DB_NAME: your-database
- Install and launch [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy)

```sh
cloud_sql_proxy -dir=/tmp/cloudsql -instances=yori_instance_name=tcp:3306
```
- Run

- Launch server

```sh
env FLASK_APP=main.py flask run
env FLASK_APP=main.py DB_USER=username DB_PASS=password DB_NAME=database_name CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run
```

or if you want to enable network access to local server (especially when you are developing yori-view)

```sh
env FLASK_APP=main.py flask run --host=0.0.0.0
env FLASK_APP=main.py DB_USER=username DB_PASS=password DB_NAME=database_name CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run --host=0.0.0.0
```

If setting environments bothers you, write them in your dotfiles.

## Deploy

Before deploy to gcloud, you have to install gcloud sdk. ([Install guide](https://cloud.google.com/sdk/docs/quickstarts))

- Create a `secret.yaml` (if you haven't) and set your own environment variable
**NOTICE: do not upload your `secret.yaml` to repository**

```yaml
env_variables:
CLOUD_SQL_CONNECTION_NAME: your-connection-name
DB_USER: your-account
DB_PASS: your-password
DB_NAME: your-database
SLACK_WEBHOOK_URL: slack-webhook-url
```
- Deploy Command
```sh
Expand Down
67 changes: 56 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,79 @@
from flask import Flask
from flask_cors import CORS
import sqlalchemy
import requests


def onGAE():
return os.getenv('GAE_ENV', '').startswith('standard')


app = Flask(__name__)
CORS(app)
app.config["JSON_AS_ASCII"] = False
app.config['JSON_AS_ASCII'] = False

logger = logging.getLogger()
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')

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")

db = sqlalchemy.create_engine(
sqlalchemy.engine.url.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 that package
@app.route('/photo', methods=['GET'])
def index():
result = db.execute("select * from photos")
result = db.execute('select * from photos')
return json.dumps([dict(data) for data in result])


# 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)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Flask==1.0.2
flask-cors>=3.0.8
SQLAlchemy>=1.3.0
PyMySQL==0.9.3
google-cloud-logging>=1.8.0
requests>=2.22.0

0 comments on commit ea69cbf

Please sign in to comment.