Skip to content

Commit

Permalink
add Flask framework (#1)
Browse files Browse the repository at this point in the history
* add vscode to git ignore

* create flask server

* modified git ignore

* add dummy data
  • Loading branch information
z6wdc authored Jul 1, 2019
1 parent 09f0073 commit f80f12a
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ venv.bak/

# mypy
.mypy_cache/

# vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

18 changes: 18 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10

82 changes: 82 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

from flask import Flask
from flask import jsonify

# test data
dummy0 = {
"photo_group": "乃木坂46",
"photo_member": "樋口日奈",
"photo_costume": "2019 summer concert 1",
"photo_type": "ヨリ",
"photo_number": 1,
"photo_folder": "summer concert",
"photo_tag": []
}

dummy1 = {
"photo_group": "乃木坂46",
"photo_member": "樋口日奈",
"photo_costume": "2019 summer concert 1",
"photo_type": "チュウ",
"photo_number": 3,
"photo_folder": "summer concert",
"photo_tag": []
}

dummy2 = {
"photo_group": "乃木坂46",
"photo_member": "樋口日奈",
"photo_costume": "2019 summer concert 1",
"photo_type": "ヒキ",
"photo_number": 1,
"photo_folder": "summer concert",
"photo_tag": []
}

photos = [dummy0, dummy1, dummy2]

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

@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello World!'


@app.route('/photos/all', methods=['GET'])
def photos_all():
return jsonify(photos)


@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500


if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]

24 changes: 24 additions & 0 deletions main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import main


def test_index():
main.app.testing = True
client = main.app.test_client()

r = client.get('/')
assert r.status_code == 200
assert 'Hello World' in r.data.decode('utf-8')
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==0.12.2
gunicorn==19.7.1

0 comments on commit f80f12a

Please sign in to comment.