-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dinuta/flask-playing-ground
Flask server restful
- Loading branch information
Showing
22 changed files
with
213 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
version: "3" | ||
|
||
services: | ||
jinja2docker: | ||
container_name: jinja2docker | ||
image: dinutac/jinja2docker:latest | ||
hostname: jinja2docker | ||
entrypoint: tail -f /etc/alpine-release #keep the container up to use exec command from docker | ||
entrypoint: python3 /home/dev/scripts/main_flask.py | ||
# entrypoint: tail -f /etc/alpine-release | ||
environment: | ||
limit: 'sky' #example env var inserted. you can read it with environ('limit') | ||
volumes: | ||
- ./templates:/data | ||
- ./variables:/variables | ||
- ./:/out | ||
- ./inputs/templates:/data | ||
- ./inputs/variables:/variables | ||
- ./:/home/dev/scripts/ | ||
ports: | ||
- "5000:5000" | ||
|
||
expose: | ||
- "5000" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"os": "{{os}}", | ||
"version": {{version}}, | ||
"installed_apps": "{{installed_apps}}" | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"os": "{{os}}" | ||
"version": {{version}} | ||
"installed_apps": "{{installed_apps}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"os" : "windows", | ||
"os": "windows", | ||
"version": 10, | ||
"installed_apps": "json" | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
|
||
from entitities.render import Render | ||
|
||
if __name__ == '__main__': | ||
render = Render(os.environ.get('TEMPLATE'), os.environ.get('VARIABLES')) | ||
render.rend_template(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from rest.render_flask_app import init_app, app | ||
|
||
if __name__ == "__main__": | ||
init_app(app) | ||
app.run(host='0.0.0.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from flask import Flask | ||
|
||
print("Start init") | ||
|
||
|
||
def create_app(): | ||
print("Start init flask!") | ||
app = Flask(__name__, instance_relative_config=False) | ||
app.config.from_object('rest.flask_config.Config') | ||
with app.app_context(): | ||
return app | ||
|
||
|
||
print("End init") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Config: | ||
TESTING = True | ||
FLASK_DEBUG = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
|
||
from flask import Flask, jsonify, request, Blueprint | ||
from flask_restplus import Api | ||
|
||
from entitities.render import Render | ||
|
||
env_vars = { | ||
"TEMPLATES_DIR": os.environ.get('TEMPLATES_DIR'), | ||
"VARS_DIR": os.environ.get('VARS_DIR'), | ||
"TEMPLATE": os.environ.get('TEMPLATE'), | ||
"VARIABLES": os.environ.get('VARIABLES'), | ||
"TEMPLATES_DIR_FILES": os.listdir(os.environ.get('TEMPLATES_DIR')), | ||
"VARS_DIR_FILES": os.listdir(os.environ.get('VARS_DIR')), | ||
"PATH": os.environ.get('PATH') | ||
} | ||
|
||
app = Flask(__name__) | ||
api = Api(app) | ||
|
||
|
||
def init_app(flask_app): | ||
blueprint = Blueprint('api', __name__, url_prefix='/api') | ||
api.init_app(blueprint) | ||
flask_app.register_blueprint(blueprint) | ||
|
||
|
||
# TODO define swagger specs | ||
|
||
@app.route('/env') | ||
def get_vars(): | ||
return jsonify(env_vars), 200 | ||
|
||
|
||
@app.route('/rend/<template>/<variables>', methods=['GET']) | ||
def get_content(template, variables): | ||
os.environ['TEMPLATE'] = template | ||
os.environ['VARIABLES'] = variables | ||
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES']) | ||
try: | ||
result = r.rend_template("dummy") | ||
except Exception as e: | ||
result = "Exception({0})".format(e.__str__()) | ||
|
||
return result, 200 | ||
|
||
|
||
@app.route('/rendwithenv/<template>/<variables>', methods=['POST']) | ||
def get_content_with_env(template, variables): | ||
input_json = request.get_json(force=True) | ||
os.environ['TEMPLATE'] = template | ||
os.environ['VARIABLES'] = variables | ||
for key, value in input_json.items(): | ||
if key not in env_vars: | ||
os.environ[key] = value | ||
|
||
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES']) | ||
try: | ||
result = r.rend_template("dummy") | ||
except Exception as e: | ||
result = "Exception({0})".format(e.__str__()) | ||
|
||
return result, 200 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import unittest | ||
|
||
import requests | ||
import yaml | ||
from flask import json | ||
from parameterized import parameterized | ||
|
||
|
||
class FlaskServerTestCase(unittest.TestCase): | ||
server = "http://0.0.0.0:5000" | ||
|
||
def test_env_endpoint(self): | ||
response = json.loads(requests.get(self.server + "/env").text) | ||
self.assertEqual(len(response), 7) | ||
self.assertEqual(response.get('VARS_DIR'), os.environ['VARS_DIR']) | ||
self.assertEqual(response.get('TEMPLATES_DIR'), os.environ['TEMPLATES_DIR']) | ||
|
||
@parameterized.expand([ | ||
("json.j2", "json.json"), | ||
("yml.j2", "yml.yml") | ||
]) | ||
def test_rend_endpoint(self, template, variables): | ||
response = yaml.load(requests.get(self.server + f"/rend/{template}/{variables}").text) | ||
self.assertEqual(len(response), 3) | ||
|
||
@parameterized.expand([ | ||
("json.j2", "doesnotexists.json"), | ||
("yml.j2", "doesnotexists.yml") | ||
]) | ||
def test_rend_endpoint(self, template, variables): | ||
expected = f"Exception([Errno 2] No such file or directory: \'/variables/{variables}\')" | ||
response = requests.get(self.server + f"/rend/{template}/{variables}").text | ||
self.assertEqual(expected, response) | ||
|
||
@parameterized.expand([ | ||
("doesnotexists.j2", "json.json"), | ||
("doesnotexists.j2", "yml.yml") | ||
]) | ||
def test_rend_endpoint(self, template, variables): | ||
expected = f"Exception({template})" | ||
response = requests.get(self.server + f"/rend/{template}/{variables}").text | ||
self.assertEqual(expected, response) | ||
|
||
# @parameterized.expand([ | ||
# ("standalone.j2", "variables.yml") | ||
# ]) | ||
# def test_rendwithenv_endpoint(self, template, variables): | ||
# payload = {'DATABASE': 'mysql56', 'IMAGE': 'latest'} | ||
# response = yaml.load(requests.post(self.server + f"/rendwithenv/{template}/{variables}", data=payload).text) | ||
# self.assertEqual(len(response), 3) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters