diff --git a/app/__init__.py b/app/__init__.py index cd87405..f2f46db 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,4 +1,33 @@ -from flask import Flask -app = Flask(__name__) +import os +import tornado.web +import tornado.options +import tornado.web -from . import routes + +application = tornado.web.Application( + debug=tornado.options.options.debug, + cookie_secret=tornado.options.options.secret, + static_path=os.path.join(os.path.dirname(__file__), "static"), + template_path=os.path.join(os.path.dirname(__file__), "templates") +) + + +class url(object): + def __init__(self, url): + self.url = url + + def __call__(self, cls): + application.add_handlers( + r'.*$', + (tornado.web.url(self.url, cls, name=cls.__name__),) + ) + return cls + + +class Route(tornado.web.RequestHandler): + @property + def log(self): + return log + + +import app.routes diff --git a/app/routes.py b/app/routes.py index f8f01c4..ef2dc82 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,7 +1,22 @@ -from . import app -from flask import render_template +import tornado.websocket +from app import url, Route + +@url(r'/') +class Index(Route): + def get(self): + return self.render('index.html') + + +@url(r'/ws') +class EchoWebSocket(Route, tornado.websocket.WebSocketHandler): + + def open(self): + log.info('Websocket opened') + + def on_message(self, message): + self.write_message(message) + + def on_close(self): + log.info('Websocket closed') -@app.route("/") -def index(): - return render_template('index.jinja2') diff --git a/app/templates/_base.html b/app/templates/_base.html new file mode 100644 index 0000000..dc91669 --- /dev/null +++ b/app/templates/_base.html @@ -0,0 +1,91 @@ + + + + + + + + + + {% include "_ie.html" %} + + Apparatus + + + + + + {% include "_nav.html" %} + + + +
+
+ {% for i in range(3) %} +
+

Title

+

Lorem ipsum dolor sit amet, aliquet porttitor tortor mattis nec mauris, duis nunc accumsan vel. Eget neque suspendisse nonummy turpis, tempus urna cum vestibulum lectus. Ut elit lectus neque, elementum nulla curabitur faucibus, qui augue cubilia. Neque erat nullam ullamco massa habitasse dolor, erat at sed donec vulputate sodales. Et elementum arcu vel blandit ultrices, eu quam, eget nunc ultricies quam tincidunt aenean. Et sem massa ac, egestas justo tempus nam nulla ac mauris, est neque maecenas imperdiet per ipsum. A beatae neque et incidunt mollis ipsum. Nulla lorem. Ullamcorper quisque commodo elit a elementum, suscipit etiam faucibus ante, suspendisse aliquet sed non, nec tellus mauris. Purus urna euismod, viverra etiam dis elit, phasellus quam wisi posuere lorem porttitor, pulvinar consequat nec eu fringilla malesuada. Nec leo dignissim lacus egestas nunc, lorem magna, sodales laoreet dignissim. A facilisis, quisque tristique lorem, gravida risus etiam, in lacinia, congue et nunc at.

+

View details »

+
+ {% end %} +
+ +
+ {% for i in range(3) %} +
+ {% if i % 2 %} +
+ +
+ {% end %} +
+

Title Subtitle

+

Lead

+
+ {% if i % 2 == 0 %} +
+ +
+ {% end %} +
+
+ {% end %} + + +
+ + + + + + diff --git a/app/templates/_base.jinja2 b/app/templates/_base.jinja2 deleted file mode 100644 index 0f18cc6..0000000 --- a/app/templates/_base.jinja2 +++ /dev/null @@ -1,93 +0,0 @@ -{%- from "_utils.jinja2" import static -%} - - - - - - - - - - {%- include "_ie.jinja2" -%} - - Apparatus - - - - - - - {%- include "_nav.jinja2" -%} - - - -
-
- {% for i in range(3) %} -
-

{{ lipsum(1, html=False, min=5, max=10) }}

- {{ lipsum(1, max=40) }} -

View details »

-
- {% endfor %} -
- -
- {% macro item(i) %} - {% if i % 2 %} -
- -
- {% else %} -
-

{{ lipsum(1, html=False, min=3, max=6) }}{{ lipsum(1, html=False, min=3, max=6) }}

-

{{ lipsum(1, html=False, max=50) }}

-
- {% endif %} - {% endmacro %} - {% for i in range(3) %} -
- {{ item(i) }} - {{ item(i + 11) }} -
-
- {% endfor %} - - -
- - - - - - diff --git a/app/templates/_ie.jinja2 b/app/templates/_ie.html similarity index 100% rename from app/templates/_ie.jinja2 rename to app/templates/_ie.html diff --git a/app/templates/_nav.jinja2 b/app/templates/_nav.html similarity index 100% rename from app/templates/_nav.jinja2 rename to app/templates/_nav.html diff --git a/app/templates/_utils.jinja2 b/app/templates/_utils.jinja2 deleted file mode 100644 index b9c1789..0000000 --- a/app/templates/_utils.jinja2 +++ /dev/null @@ -1,3 +0,0 @@ -{% macro static(fn) %} - {{ url_for('static', filename=fn) }} -{% endmacro %} diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..9789436 --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1 @@ +{% extends "_base.html" %} diff --git a/app/templates/index.jinja2 b/app/templates/index.jinja2 deleted file mode 100644 index 5a2409c..0000000 --- a/app/templates/index.jinja2 +++ /dev/null @@ -1 +0,0 @@ -{%- extends "_base.jinja2" -%} diff --git a/requirements-dev.txt b/requirements-dev.txt index ae3f09e..3644874 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,4 @@ wdb -log_colorizer wsreload cutter pytest diff --git a/requirements.txt b/requirements.txt index 7e10602..c3368df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -flask +tornado diff --git a/serve.py b/serve.py index cf69544..3e6f2f4 100644 --- a/serve.py +++ b/serve.py @@ -1,53 +1,49 @@ #!/usr/bin/env python -host = 'apparatus.l' -port = 2001 -url = "http://%s:%d/*" % (host, port) - -from log_colorizer import colorize -colorize() - try: from wdb.ext import add_w_builtin add_w_builtin() except ImportError: pass -import logging -from app import app -app.logger.setLevel(logging.DEBUG) +import tornado.options +import tornado.ioloop -del app.logger.handlers[:] +tornado.options.define("secret", default='secret', help="Secret") +tornado.options.define("debug", default=True, help="Debug mode") +tornado.options.define("host", default='apparatus.l', help="Server host") +tornado.options.define("port", default=2001, type=int, help="Server port") -logging.getLogger('werkzeug').setLevel(logging.DEBUG) +host = 'apparatus.l' +tornado.options.parse_command_line() -werkzeug_debugger = True -try: - from wdb.ext import WdbMiddleware -except ImportError: - app.logger.debug('wdb not found') -else: - app.wsgi_app = WdbMiddleware(app.wsgi_app, start_disabled=True) - werkzeug_debugger = False + +from logging import getLogger +log = getLogger('apparatus') +log.setLevel(10 if tornado.options.options.debug else 30) + +log.debug('Starting server') +ioloop = tornado.ioloop.IOLoop.instance() + + +from app import application +application.listen(tornado.options.options.port) + + +url = "http://%s:%d/*" % ( + tornado.options.options.host, tornado.options.options.port) try: - from wsreload.client import monkey_patch_http_server, watch + from wsreload.client import sporadic_reload, watch except ImportError: - app.logger.debug('wsreload not found') + log.debug('wsreload not found') else: - def log(httpserver): - app.logger.debug('WSReloaded after server restart') - monkey_patch_http_server({'url': url}, callback=log) - app.logger.debug('HTTPServer monkey patched for url %s' % url) + sporadic_reload({'url': url}) files = ['app/static/javascripts/', 'app/static/stylesheets/', 'app/templates/'] watch({'url': url}, files, unwatch_at_exit=True) -app.run( - debug=True, - host=host, - port=port, - use_debugger=werkzeug_debugger, - threaded=True) +log.debug('Starting loop') +ioloop.start()