2015-08-19 21:53:46 +00:00
|
|
|
import logging
|
|
|
|
import signal
|
2021-09-18 12:38:51 +00:00
|
|
|
import typing
|
2015-08-19 21:53:46 +00:00
|
|
|
|
2016-01-10 19:46:27 +00:00
|
|
|
from sprockets.mixins.mediatype import content, transcoders
|
2015-08-19 21:53:46 +00:00
|
|
|
from tornado import ioloop, web
|
|
|
|
|
|
|
|
|
2015-11-03 13:10:54 +00:00
|
|
|
class SimpleHandler(content.ContentMixin, web.RequestHandler):
|
2021-09-18 12:38:51 +00:00
|
|
|
def post(self) -> None:
|
2015-08-19 21:53:46 +00:00
|
|
|
body = self.get_request_body()
|
|
|
|
self.set_status(200)
|
|
|
|
self.send_response(body)
|
|
|
|
|
|
|
|
|
2021-09-18 12:38:51 +00:00
|
|
|
def make_application(**settings: typing.Any) -> web.Application:
|
2018-12-05 04:08:05 +00:00
|
|
|
application = web.Application([('/', SimpleHandler)], **settings)
|
2021-09-18 12:38:51 +00:00
|
|
|
content.set_default_content_type(application,
|
|
|
|
'application/json',
|
2015-11-03 13:10:54 +00:00
|
|
|
encoding='utf-8')
|
2016-02-01 15:06:46 +00:00
|
|
|
content.add_transcoder(application, transcoders.MsgPackTranscoder())
|
|
|
|
content.add_transcoder(application, transcoders.JSONTranscoder())
|
2015-08-19 21:53:46 +00:00
|
|
|
return application
|
|
|
|
|
|
|
|
|
2021-09-18 12:38:51 +00:00
|
|
|
def _signal_handler(signo: int, _: typing.Any) -> None:
|
2015-08-19 21:53:46 +00:00
|
|
|
logging.info('received signal %d, stopping application', signo)
|
|
|
|
iol = ioloop.IOLoop.instance()
|
|
|
|
iol.add_callback_from_signal(iol.stop)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(levelname)1.1s - %(name)s: %(message)s')
|
|
|
|
application = make_application(debug=True)
|
|
|
|
application.listen(8000)
|
|
|
|
signal.signal(signal.SIGINT, _signal_handler)
|
|
|
|
signal.signal(signal.SIGTERM, _signal_handler)
|
|
|
|
ioloop.IOLoop.instance().start()
|