|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +__author__ = "Jeffrey Jose" |
| 4 | + |
| 5 | +import sys, os |
| 6 | +import simplejson as json |
| 7 | + |
| 8 | +import tornado.ioloop |
| 9 | +import tornado.web |
| 10 | +import tornado.options |
| 11 | +import tornado.websocket |
| 12 | + |
| 13 | +tornado.options.define("port", default=8000, help="Run on the given port", type=int) |
| 14 | + |
| 15 | +vueAppPath = os.path.abspath(os.path.dirname(__file__)) |
| 16 | + |
| 17 | +class Default(tornado.web.RequestHandler): |
| 18 | + ''' |
| 19 | + If Tornado doesnt know what to do, let vue take care of the url. |
| 20 | + ''' |
| 21 | + |
| 22 | + def get(self, path): |
| 23 | + ''' |
| 24 | + HTTP GET Method handler |
| 25 | + ''' |
| 26 | + self.redirect('/#%s' % path, permanent = True) |
| 27 | + |
| 28 | + |
| 29 | +class IndexHandler(tornado.web.RequestHandler): |
| 30 | + ''' |
| 31 | + Handler to serve the template/index.html |
| 32 | + ''' |
| 33 | + def get(self): |
| 34 | + ''' |
| 35 | + HTTP GET Method handler |
| 36 | + ''' |
| 37 | + with open(vueAppPath + "/index.html", 'r') as file: |
| 38 | + self.write(file.read()) |
| 39 | + |
| 40 | +class StaticHandler(tornado.web.RequestHandler): |
| 41 | + ''' |
| 42 | + Handler to serve everything inside static/ directory |
| 43 | + ''' |
| 44 | + |
| 45 | + def get(self): |
| 46 | + ''' |
| 47 | + HTTP GET Method handler |
| 48 | + ''' |
| 49 | + self.set_header('Content-Type', '') |
| 50 | + with open(vueAppPath + self.request.uri, 'r') as file: |
| 51 | + self.write(file.read()) |
| 52 | + |
| 53 | +handlers = [ |
| 54 | + (r'/', IndexHandler), |
| 55 | + (r'/static/.*', StaticHandler), |
| 56 | + (r'/(.*)', Default), # When you visit a non-existant link |
| 57 | +] |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + |
| 62 | + tornado.options.parse_command_line() |
| 63 | + |
| 64 | + # Turn debug on to have Tornado restart when you change this file |
| 65 | + # Recommended when you're developing. Dont forget to remove it |
| 66 | + # when you put this in production |
| 67 | + # |
| 68 | + #app = tornado.web.Application(handlers, debug = True) |
| 69 | + # |
| 70 | + |
| 71 | + app = tornado.web.Application(handlers) |
| 72 | + app.listen(tornado.options.options.port) |
| 73 | + |
| 74 | + print 'Tornado has started at %s' % tornado.options.options.port |
| 75 | + tornado.ioloop.IOLoop.instance().start() |
0 commit comments