Skip to content
Permalink
Browse files

refactor(compose): unify flask and http handler

  • Loading branch information...
hanxiao committed Aug 22, 2019
1 parent bc2e441 commit 559a997133f0f7614899ff1afe912275dd46aecf
Showing with 28 additions and 45 deletions.
  1. +1 −1 README.md
  2. +21 −0 gnes/composer/base.py
  3. +2 −19 gnes/composer/flask.py
  4. +2 −22 gnes/composer/http.py
  5. +1 −2 setup.py
  6. +1 −1 tests/test_compose.py
@@ -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.

@@ -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
@@ -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

@@ -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

@@ -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

@@ -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)
@@ -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
@@ -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()

0 comments on commit 559a997

Please sign in to comment.
You can’t perform that action at this time.