Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
refactor(compose): unify flask and http handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Aug 22, 2019
1 parent bc2e441 commit 559a997
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -341,7 +341,7 @@ Now it's time to run! [GNES board](https://board.gnes.ai) can automatically gene
</a>
</p>

> 💡 You can also start a GNES board locally. Simply run `docker run -d -p 0.0.0.0:80:8080/tcp gnes/gnes compose --flask`
> 💡 You can also start a GNES board locally. Simply run `docker run -d -p 0.0.0.0:80:8080/tcp gnes/gnes compose --serve`
As a cloud-native application, GNES requires an **orchestration engine** to coordinate all micro-services. We support Kubernetes, Docker Swarm and shell-based multi-process. Let's see what the generated script looks like in this case.

Expand Down
21 changes: 21 additions & 0 deletions gnes/composer/base.py
Expand Up @@ -601,3 +601,24 @@ def rule9():
else:
rule8()
return [last_layer, *router_layers]


def parse_http_data(data, args):
import io
if not data or 'yaml-config' not in data:
return '<h1>Bad POST request</h1> your POST request does not contain "yaml-config" field!', 406
try:
args.yaml_path = io.StringIO(data['yaml-config'])
if data.get('mermaid_direction', 'top-down').lower() == 'left-right':
args.mermaid_leftright = True
else:
args.mermaid_leftright = False
if 'docker-image' in data:
args.docker_img = data['docker-image']
else:
args.docker_img = 'gnes/gnes:latest-alpine'

return YamlComposer(args).build_all()['html'], 200
except Exception as e:
return '<h1>Bad YAML input</h1> please kindly check the format, ' \
'indent and content of your YAML file! <h3>Traceback: </h3><p><code>%s</code></p>' % e, 400
21 changes: 2 additions & 19 deletions gnes/composer/flask.py
Expand Up @@ -12,9 +12,8 @@
# 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 io

from .base import YamlComposer
from .base import YamlComposer, parse_http_data
from ..cli.parser import set_composer_parser
from ..helper import set_logger

Expand Down Expand Up @@ -50,23 +49,7 @@ def _get_homepage():
@app.route('/generate', methods=['POST'])
def _regenerate():
data = request.form if request.form else request.json
if not data or 'yaml-config' not in data:
return '<h1>Bad POST request</h1> your POST request does not contain "yaml-config" field!', 406
try:
args.yaml_path = io.StringIO(data['yaml-config'])
if data.get('mermaid_direction', 'top-down').lower() == 'left-right':
args.mermaid_leftright = True
else:
args.mermaid_leftright = False
if 'docker-image' in data:
args.docker_img = data['docker-image']
else:
args.docker_img = 'gnes/gnes:latest-alpine'

return YamlComposer(args).build_all()['html']
except Exception as e:
self.logger.error(e)
return '<h1>Bad YAML input</h1> please kindly check the format, indent and content of your YAML file!', 400
return parse_http_data(data, args)

return app

Expand Down
24 changes: 2 additions & 22 deletions gnes/composer/http.py
@@ -1,8 +1,7 @@
import io
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qs

from .base import YamlComposer
from .base import YamlComposer, parse_http_data
from ..cli.parser import set_composer_parser
from ..helper import set_logger

Expand Down Expand Up @@ -36,26 +35,7 @@ def do_POST(self):
data = self.rfile.read(content_length) # <--- Gets the data itself

data = {k: v[0] for k, v in parse_qs(data.decode('utf-8')).items()}
if not data or 'yaml-config' not in data:
self._set_response('<h1>Bad POST request</h1> your POST request does not contain "yaml-config" field!',
406)
try:
self.args.yaml_path = io.StringIO(data['yaml-config'])
if data.get('mermaid_direction', 'top-down').lower() == 'left-right':
self.args.mermaid_leftright = True
else:
self.args.mermaid_leftright = False
if 'docker-image' in data:
self.args.docker_img = data['docker-image']
else:
self.args.docker_img = 'gnes/gnes:latest-alpine'

self._set_response(YamlComposer(self.args).build_all()['html'])
except Exception as e:
self._set_response(
'<h1>Bad YAML input</h1> please kindly check the format, '
'indent and content of your YAML file! <h3>Traceback: </h3><p><code>%s</code></p>' % e,
400)
self._set_response(*parse_http_data(data, self.args))

def run(self):
httpd = HTTPServer(('0.0.0.0', self.args.http_port), self._HttpServer)
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Expand Up @@ -53,8 +53,7 @@
'grpcio',
'ruamel.yaml>=0.15.89',
'pyzmq>=17.1.0',
'aiohttp',
]
'aiohttp']

# using pip install gnes[xx] is depreciated
# extras_dep is kept for legacy issue, will be removed soon
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compose.py
Expand Up @@ -35,7 +35,7 @@ def _test_topology(self, yaml_path: str, num_layer_before: int, num_layer_after:
print(a.build_dockerswarm(r))

@unittest.SkipTest
def test_flask_local(self):
def test_http_local(self):
args = set_composer_flask_parser().parse_args(['--serve'])
YamlComposerHttp(args).run()

Expand Down

0 comments on commit 559a997

Please sign in to comment.