Skip to content

Instantly share code, notes, and snippets.

@an01f01
Last active February 11, 2021 20:53
Show Gist options
  • Save an01f01/1a16d5033789bba538c71f5c66c0b5b9 to your computer and use it in GitHub Desktop.
Save an01f01/1a16d5033789bba538c71f5c66c0b5b9 to your computer and use it in GitHub Desktop.
import os
import json
import datetime
import psycopg2
import psycopg2.extras
from psycopg2.extras import Json
import tornado.httpserver
import tornado.options
import tornado.ioloop
import tornado.web
from tornado import gen, web
class BaseHandler(tornado.web.RequestHandler):
def prepare(self):
if self.request.protocol == 'http':
self.redirect('https://' + self.request.host, permanent=False)
"""
Base handler gonna to be used instead of RequestHandler
"""
def write_error(self, status_code, **kwargs):
if status_code in [403, 404, 500, 503]:
self.write('Error %s' % status_code)
else:
self.write('BOOM!')
class StatusHandler(BaseHandler):
def get(self):
self.set_status(200)
self.finish({'status': 'Rest API Service status is ok...'})
def main():
# Define settings
settings = dict(
cookie_secret = str(os.urandom(45)),
xsrf_cookies = False,
)
# Define web app
# (The only routes that contain "." should be static files, the "." is neccessary for the extension
# dont make new, non-static routes containing "." or else the static file handler will attempt to handle the request)
application = tornado.web.Application([
(r"/", StatusHandler),
], **settings)
application.listen(80)
http_server = tornado.httpserver.HTTPServer(
application,
ssl_options = {
"certfile": os.path.join("../certs/", "ssl_app_cert.crt"),
"keyfile": os.path.join("../certs/", "ssl_app_key.key")'
})
# Start web app
http_server.listen(443)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment