├── app ├── __init__.py ├── urls.py └── config.py ├── requirements.txt ├── README.md ├── .gitignore └── runserver.py /app/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tornado>=4.0.0 2 | motor>=0.4.1 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tornado-template 2 | tornado project template . 3 | -------------------------------------------------------------------------------- /app/urls.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: UTF-8 -*- 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/config.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: UTF-8 -*- 3 | 4 | PORT = "8000" 5 | DEBUG = "True" 6 | SECRET = "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=" 7 | 8 | MONGO_HOST = "127.0.0.1" 9 | MONGO_PORT = 27017 10 | MONGO_NAME = "appname" 11 | MONGO_USER = "appname" 12 | MONGO_PASSWORD = "appname" 13 | MONGO_URI = "mongodb://%s:%s@%s:%d/%s" % (MONGO_USER, MONGO_PASSWORD, MONGO_HOST, MONGO_PORT, MONGO_NAME) 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask instance folder 57 | instance/ 58 | 59 | # Scrapy stuff: 60 | .scrapy 61 | 62 | # Sphinx documentation 63 | docs/_build/ 64 | 65 | # PyBuilder 66 | target/ 67 | 68 | # IPython Notebook 69 | .ipynb_checkpoints 70 | 71 | # pyenv 72 | .python-version 73 | 74 | # celery beat schedule file 75 | celerybeat-schedule 76 | 77 | # dotenv 78 | .env 79 | 80 | # virtualenv 81 | venv/ 82 | ENV/ 83 | 84 | # Spyder project settings 85 | .spyderproject 86 | 87 | # Rope project settings 88 | .ropeproject 89 | 90 | # JetBrains project settings 91 | .idea/ 92 | 93 | # Mac 94 | .DS_Store 95 | -------------------------------------------------------------------------------- /runserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: UTF-8 -*- 3 | 4 | import os 5 | import tornado.web 6 | import tornado.httpserver 7 | import tornado.ioloop 8 | import tornado.netutil 9 | import tornado.process 10 | from tornado.options import options, define 11 | import motor 12 | 13 | from app import config, urls 14 | 15 | 16 | define("PORT", default=config.PORT, help="run on the given port", type=int) 17 | define("DEBUG", default=config.DEBUG, type=bool) 18 | define("SECRET", default=config.SECRET, type=str) 19 | define("MONGO_HOST", default=config.MONGO_HOST, help="mongodb database host", type=str) 20 | define("MONGO_PORT", default=config.MONGO_PORT, help="mongodb database port", type=int) 21 | define("MONGO_NAME", default=config.MONGO_NAME, help="mongodb database name", type=str) 22 | define("MONGO_USER", default=config.MONGO_USER, help="mongodb database user name", type=str) 23 | define("MONGO_PASSWORD", default=config.MONGO_PASSWORD, help="mongodb database password", type=str) 24 | 25 | 26 | class Application(tornado.web.Application): 27 | def __init__(self): 28 | handlers = urls.urls 29 | settings = dict( 30 | template_path=os.path.join(os.path.dirname(__file__), "app/templates"), 31 | static_path=os.path.join(os.path.dirname(__file__), "app/static"), 32 | cookie_secret=options.SECRET, 33 | xsrf_cookies=False, 34 | login_url="/users/login", 35 | debug=options.DEBUG, 36 | ) 37 | 38 | tornado.web.Application.__init__(self, handlers, **settings) 39 | 40 | self.db = motor.MotorClient(config.MONGO_URI)[config.MONGO_NAME] 41 | 42 | 43 | def main(): 44 | tornado.options.parse_command_line() 45 | # tornado.web.ErrorHandler = base.ErrorHandler 46 | 47 | if options.DEBUG: 48 | app_server = tornado.httpserver.HTTPServer(Application()) 49 | print ("app App run in dev mode on %s" % options.PORT) 50 | app_server.listen(options.PORT) 51 | else: 52 | sockets = tornado.netutil.bind_sockets(options.PORT) 53 | tornado.process.fork_processes(0) 54 | app_server = tornado.httpserver.HTTPServer(Application()) 55 | app_server.add_socket(sockets) 56 | 57 | tornado.ioloop.IOLoop.instance().start() 58 | 59 | 60 | if __name__ == "__main__": 61 | main() --------------------------------------------------------------------------------