#coding=utf-8
import tornado.web
import tornado.wsgi
import os
import tool.config
import logging

from basehandler import BaseHandler
from login import SignupPage
from login import LoginPage
from login import LogoutPage
from home import HomePage
from message import MessagePage
from posts import PostPage
from image import ImagePage


class MainHandler(BaseHandler):
    def get(self):
        self.render('front.html')


class RemoveSlash(BaseHandler):
    def get(self,path):
        self.redirect(path)
    
    def post(self,path):
        self.redirect(path)


# class DBinit(tornado.web.RequestHandler):
#     def get(self):
#         tool.config.debug_init()


settings = {
    'template_path'  : os.path.join(os.path.dirname(__file__), 'templates'),
    'cookie_secret'  : tool.config.APP_Key.get('cookie_secret'),
    'login_url'      : '/login',
    'debug'          : False,
}


path_re = r'(?:/([a-zA-Z0-9-_]+))?'
tornado_app = tornado.web.Application([
    (r'(?:(.+)/)',  RemoveSlash),
    (r'/signup',  SignupPage),
    (r'/login',  LoginPage),
    (r'/logout',  LogoutPage),
    (r'/home',  HomePage),
    (r'/message'+path_re,  MessagePage),
    (r'/post',  PostPage),
    (r'/img/([a-zA-Z0-9_-]+)',  ImagePage),
    # (r'/dbinit/?', DBinit),
    (r'/',  MainHandler),
],**settings)


app = tornado.wsgi.WSGIAdapter(tornado_app)
