Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 the original author or authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import os |
| 19 | |
| 20 | from klein import Klein |
| 21 | from simplejson import dumps, load |
| 22 | from structlog import get_logger |
| 23 | from twisted.internet import reactor, endpoints |
| 24 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 25 | from twisted.internet.tcp import Port |
| 26 | from twisted.web.server import Site |
| 27 | from twisted.web.static import File |
Zsolt Haraszti | d4226ed | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 28 | from werkzeug.exceptions import BadRequest |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 29 | |
| 30 | log = get_logger() |
| 31 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 32 | |
| 33 | class WebServer(object): |
| 34 | |
| 35 | app = Klein() |
| 36 | |
| 37 | def __init__(self, port, work_dir, grpc_client): |
| 38 | self.port = port |
| 39 | self.site = None |
| 40 | self.work_dir = work_dir |
| 41 | self.grpc_client = grpc_client |
| 42 | |
| 43 | self.swagger_ui_root_dir = os.path.abspath( |
| 44 | os.path.join(os.path.dirname(__file__), '../swagger_ui')) |
| 45 | |
| 46 | self.tcp_port = None |
Zsolt Haraszti | d4226ed | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 47 | self.shutting_down = False |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 48 | |
| 49 | @inlineCallbacks |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 50 | def start(self): |
| 51 | log.debug('starting') |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 52 | yield self._open_endpoint() |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 53 | log.info('started') |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 54 | returnValue(self) |
| 55 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 56 | @inlineCallbacks |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 57 | def stop(self): |
| 58 | log.debug('stopping') |
| 59 | self.shutting_down = True |
| 60 | if self.tcp_port is not None: |
| 61 | assert isinstance(self.tcp_port, Port) |
| 62 | yield self.tcp_port.socket.close() |
| 63 | log.info('stopped') |
| 64 | |
| 65 | @inlineCallbacks |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 66 | def _open_endpoint(self): |
| 67 | endpoint = endpoints.TCP4ServerEndpoint(reactor, self.port) |
| 68 | self.site = Site(self.app.resource()) |
| 69 | self.tcp_port = yield endpoint.listen(self.site) |
| 70 | log.info('web-server-started', port=self.port) |
| 71 | self.endpoint = endpoint |
| 72 | |
Zsolt Haraszti | d4226ed | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 73 | def reload_generated_routes(self): |
| 74 | for fname in os.listdir(self.work_dir): |
| 75 | if fname.endswith('_gw.py'): |
| 76 | module_name = fname.replace('.py', '') |
| 77 | m = __import__(module_name) |
| 78 | assert hasattr(m, 'add_routes') |
| 79 | m.add_routes(self.app, self.grpc_client) |
| 80 | log.info('routes-loaded', module=module_name) |
| 81 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 82 | # static swagger_ui website as landing page (for now) |
| 83 | |
| 84 | @app.route('/', branch=True) |
| 85 | def static(self, request): |
| 86 | try: |
| 87 | log.debug(request=request) |
| 88 | return File(self.swagger_ui_root_dir) |
| 89 | except Exception, e: |
| 90 | log.exception('file-not-found', request=request) |
| 91 | |
| 92 | # static swagger.json file to serve the schema |
| 93 | |
| 94 | @app.route('/v1/swagger.json') |
| 95 | def swagger_json(self, request): |
| 96 | try: |
| 97 | return File(os.path.join(self.work_dir, 'swagger.json')) |
| 98 | except Exception, e: |
| 99 | log.exception('file-not-found', request=request) |