Created
September 13, 2012 21:06
-
-
Save alejandrobernardis/3717644 to your computer and use it in GitHub Desktop.
Tornado App, Base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import os, sys | |
| pkg = '/src' | |
| root_path = os.path.dirname(__file__) | |
| sys.path.insert(0, root_path.replace(pkg, '/src')) | |
| sys.path.insert(0, root_path.replace(pkg, '/lib')) | |
| sys.path.insert(0, root_path.replace(pkg, '/libs')) | |
| sys.path.insert(0, '/Volumes/Amelie/alejandro/development/library/python') | |
| # Copyright (c) 2012 Asumi Kamikaze Inc. | |
| # Copyright (c) 2012 The Octopus Apps Inc. | |
| # Licensed under the Apache License, Version 2.0 (the "License") | |
| # | |
| # Author: Alejandro M. Bernardis | |
| # Email: alejandro.m.bernardis at gmail.com | |
| # Created: Sep 12, 2012 1:31:12 PM | |
| __project_name__ = u'AKIMS' | |
| __project_full_name__ = u'Asumi Kamikaze, Image Management System' | |
| __project_owner__ = u'Asumi Kamikaze' | |
| __project_author__ = u'Alejandro M. Bernardis' | |
| __project_version__ = (1,0,0,'alpha',0) | |
| #: imports | |
| import settings | |
| from com.ak.common.security import secret_key | |
| from pymongo import Connection as mongodb_connection | |
| from mongoengine import connect as mongodb_connect | |
| from tornado.httpserver import HTTPServer | |
| from tornado.ioloop import IOLoop | |
| from tornado.web import Application, StaticFileHandler | |
| from tornado.options import define, options, parse_command_line | |
| #: server | |
| define('debug', default=settings.DEBUG) | |
| define('port', default=settings.PORT) | |
| define('ssl', default=settings.SSL) | |
| define('cdn', default=settings.CDN) | |
| define('prefork', default=settings.PREFORK) | |
| #: database | |
| define('database_ddbb', default=settings.DATABASE_NAME) | |
| define('database_user', default=settings.DATABASE_USER) | |
| define('database_pass', default=settings.DATABASE_PASS) | |
| define('database_host', default=settings.DATABASE_HOST) | |
| define('database_port', default=settings.DATABASE_PORT) | |
| define('database_conn', default=settings.DATABASE_CONN) | |
| define('database_areq', default=settings.DATABASE_AREQ) | |
| define('database_ugls', default=settings.DATABASE_UGLS) | |
| #: database async | |
| define('database_pool', default='%s_pool' % settings.DATABASE_NAME) | |
| define('database_cach', default=settings.DATABASE_CACH) | |
| #: application | |
| class MainApplication(Application): | |
| def __init__(self): | |
| _static_path = os.path.join(root_path, 'static') | |
| _static_path_ditc = dict(path=_static_path) | |
| _handlers = [ | |
| (r"/(humans\.txt)", StaticFileHandler, _static_path_ditc), | |
| (r"/(robots\.txt)", StaticFileHandler, _static_path_ditc), | |
| (r"/(crossdomain\.xml)", StaticFileHandler, _static_path_ditc), | |
| (r"/(xd_receiver\.html)", StaticFileHandler, _static_path_ditc), | |
| (r"/(channel\.html)", StaticFileHandler, _static_path_ditc) | |
| ] | |
| for a in settings.HANDLERS_LIST: | |
| _name = '%s.%s' % (settings.PKG_HANDLERS, a) | |
| _module = __import__(_name, globals(), locals(), [a], -1) | |
| _handlers.extend(_module.handlers_list) | |
| try: | |
| _cdn_file = os.path.join(root_path, 'cdn_prefix.conf') | |
| _cdn_prefix = [x.strip() for x in file(_cdn_file) | |
| if x.strip() and not x.strip().startswith('#')][0] | |
| except (IOError, IndexError): | |
| _cdn_prefix = None | |
| _settings = dict( | |
| debug=options.debug, | |
| ssl= options.ssl, | |
| cdn=options.cdn, | |
| cdn_prefix=_cdn_prefix, | |
| xsrf_cookies=True, | |
| cookie_secret=secret_key(64), | |
| cookie_user_session=settings.COOKIE_USER_SESSION, | |
| login_url=settings.LOGIN_URL, | |
| path=root_path, | |
| static_path=_static_path, | |
| templates_path=os.path.join(root_path, 'templates'), | |
| temp_path=os.path.join(root_path, 'temp'), | |
| upload_path=os.path.join(root_path, 'upload'), | |
| download_path=os.path.join(root_path, 'download'), | |
| backup_path=os.path.join(root_path, 'backup'),) | |
| self._database_name = options.database_ddbb | |
| self._database_settings = dict( | |
| host=options.database_host, | |
| port=options.database_port, | |
| username=options.database_user, | |
| password=options.database_pass, | |
| max_pool_size=options.database_conn, | |
| auto_start_request=options.database_areq, | |
| use_greenlets=options.database_ugls,) | |
| if settings.DATABASE_ORM: | |
| mongodb_connect(self._database_name, **self._database_settings) | |
| Application.__init__(self, _handlers, **_settings) | |
| @property | |
| def db_conn(self): | |
| try: | |
| if not hasattr(self, '_database_connection'): | |
| self._database_connection = None if settings.DATABASE_ORM \ | |
| else mongodb_connection(**self._database_settings) | |
| return self._database_connection | |
| except: | |
| return None | |
| @property | |
| def db(self): | |
| try: | |
| if not hasattr(self, '_database'): | |
| self._database = self.db_conn[self._database_name] | |
| return self._database | |
| except: | |
| return None | |
| def db_by_name(self, name=None): | |
| try: | |
| name = name or 'test' | |
| return self.db_conn[name] | |
| except: | |
| return None | |
| @property | |
| def ssl_support(self): | |
| return self.settings.get('ssl') | |
| def ssl_config(self): | |
| if not self.ssl_support: | |
| return None | |
| CA_dir = os.path.join(self.settings.get('path'), 'CA') | |
| return dict( | |
| certfile=os.path.join(CA_dir, 'server.crt'), | |
| keyfile=os.path.join(CA_dir, 'server.key'),) | |
| @property | |
| def cdn_support(self): | |
| return self.settings.get('cdn') | |
| @property | |
| def cdn_prefix(self): | |
| return self.settings.get('cdn_prefix') or None | |
| #: main | |
| if __name__ == '__main__': | |
| try: | |
| parse_command_line() | |
| app = MainApplication() | |
| cfg = dict(xheaders=True, ssl_options=app.ssl_config()) | |
| http_server = HTTPServer(app, **cfg) | |
| if options.prefork: | |
| http_server.bind(options.port) | |
| http_server.start() | |
| else: | |
| http_server.listen(options.port) | |
| IOLoop.instance().start() | |
| except KeyboardInterrupt: | |
| exit(u'\n\x1b[1;31m[kill]\x1b[0m => %s' % __project_full_name__) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| + peterbe / tornado_gists (https://github.com/peterbe/tornado_gists) | |
| Collaborative collection of Tornado related Github gists - http://tornadogists.org |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # | |
| # Copyright (c) 2012 Asumi Kamikaze Inc. | |
| # Copyright (c) 2012 The Octopus Apps Inc. | |
| # Licensed under the Apache License, Version 2.0 (the "License") | |
| # | |
| # Author: Alejandro M. Bernardis | |
| # Email: alejandro.m.bernardis at gmail.com | |
| # Created: Sep 12, 2012 1:41:15 PM | |
| import os | |
| #: root | |
| ROOT_PATH = os.path.dirname(__file__) | |
| #: port | |
| DEBUG = True | |
| PORT = 8000 | |
| SSL = False | |
| CDN = False | |
| PREFORK = False | |
| PKG = 'com.ak' | |
| PKG_HANDLERS = 'com.ak.handlers' | |
| PKG_MODELS = 'com.ak.models' | |
| PKG_MODULES = 'com.ak.modules' | |
| COOKIE_USER_SESSION = 'user_session' | |
| #: handlers | |
| LOGIN_URL = '/auth/login' | |
| HANDLERS_LIST = ['common', 'admin'] | |
| #: database | |
| DATABASE_ORM = True | |
| DATABASE_NAME = 'com-ak-local' | |
| DATABASE_USER = 'root' | |
| DATABASE_PASS = 'root' | |
| DATABASE_HOST = '127.0.0.1' | |
| DATABASE_PORT = 27017 | |
| DATABASE_CONN = 100 # poll_size | |
| DATABASE_AREQ = True # auto_start_request | |
| DATABASE_UGLS = False # greenlets | |
| #: database async | |
| DATABASE_CACH = 10 | |
| EMAIL_ACCOUNT = 'username@domain.com' | |
| EMAIL_USER = 'username@domain.com' | |
| EMAIL_PASS = '*******' | |
| EMAIL_HOST = 'smtp.google.com' | |
| EMAIL_PORT = 587 | |
| EMAIL_TLS = True | |
| #: site | |
| SITE_TITLE = 'Site Title' | |
| SITE_DOMAIN = 'localhost' | |
| SITE_ROOT = '/' | |
| FACEBOOK = dict( | |
| uid = '', | |
| app_id = '', | |
| api_key = '' | |
| ) | |
| try: | |
| FACEBOOK['secret_key'] = open(os.path.join(ROOT_PATH, '.facebook')).read().strip() | |
| except: | |
| pass | |
| TWITTER = dict( | |
| uid = '', | |
| app_id = '', | |
| api_key = '' | |
| ) | |
| try: | |
| TWITTER['secret_key'] = open(os.path.join(ROOT_PATH, '.twitter')).read().strip() | |
| except: | |
| pass | |
| #: openid | |
| OPENID = dict( | |
| uid = '', | |
| app_id = '', | |
| api_key = '' | |
| ) | |
| try: | |
| OPENID['secret_key'] = open(os.path.join(ROOT_PATH, '.openid')).read().strip() | |
| except: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment