# Path: gunstar/app.py
# class Application(object):
# 
#     def __init__(self, routes=None, config=None):
#         # set router
#         self.router = Router(routes)
# 
#         # set config
#         self.config = Config()
#         self.config.load_from_object(DefaultConfig)
#         self.config.load_from_object(config)
# 
#         # add static dir route
#         self.set_static_route()
# 
#         # handlers for 404 and 500
#         self.handler404 = import_object(
#             self.config.get('HANDLER_FOR_404')
#         )
#         self.handler500 = import_object(
#             self.config.get('HANDLER_FOR_500')
#         )
# 
#     def set_static_route(self):
#         # add static dir route
#         self.static_root = self.config.get('STATIC_ROOT')
#         self.static_path = self.config.get('STATIC_PATH')
#         if self.static_root and self.static_path:
#             self.router.add_route(
#                 self.static_path + '{file:path}',
#                 DirectoryApp,
#                 'static'
#             )
# 
#     def load_config(self, config):
#         self.config.load_from_object(config)
#         self.set_static_route()
# 
#     def add_route(self, pattern, handler, name):
#         self.router.add_route(pattern, handler, name)
# 
#     def __call__(self, environ, start_response):
#         self.request = Request(environ)
#         request_started_signal.send(self, request=self.request)
#         self.response = Response()
#         path = self.request.path_info
#         route = self.router.find_route(path)
#         if route:
#             try:
#                 func = route.resolve_func()
#                 # check is route for static files
#                 if route.name == 'static':
#                     static_handler = func(self.static_root)
#                     self.request.path_info = self.request.path_info.replace(
#                         self.static_path, ''
#                     )
#                     resp = self.request.get_response(static_handler)
#                     return resp(environ, start_response)
#                 args = route.get_args(path)
#                 handler = func(self, self.request, self.response)
#                 handler.dispatch(*args)
#                 request_finished_signal.send(self, response=handler.response)
#                 return handler.response(environ, start_response)
#             except Exception:
#                 exc_info = sys.exc_info()
#                 request_exception_signal.send(
#                     self, request=self.request, exc_info=exc_info
#                 )
#                 handler_500 = self.handler500(
#                     self, self.request, self.response
#                 )
#                 handler_500.dispatch(exc_info)
#                 return handler_500.response(environ, start_response)
#         else:
#             handler_404 = self.handler404(self, self.request, self.response)
#             handler_404.dispatch()
#             return handler_404.response(environ, start_response)


from gunstar.app import Application
    from wsgiref.simple_server import make_server
import os
# -*- coding: utf-8 -*-


PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))


class ConfigSettings(object):
    
    TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
    SECRET_KEY = 'my-secret-key'
    STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
    STATIC_PATH = '/static/'


routes = (
    ('/', 'handlers.IndexHandler', 'index'),
    ('/name/{name}/', 'handlers.OtherHandler', 'other'),
)


app = Application(routes=routes, config=ConfigSettings)


if __name__ == '__main__':
    server = make_server('127.0.0.1', 8080, app)

# Path: gunstar/app.py
# class Application(object):
# 
#     def __init__(self, routes=None, config=None):
#         # set router
#         self.router = Router(routes)
# 
#         # set config
#         self.config = Config()
#         self.config.load_from_object(DefaultConfig)
#         self.config.load_from_object(config)
# 
#         # add static dir route
#         self.set_static_route()
# 
#         # handlers for 404 and 500
#         self.handler404 = import_object(
#             self.config.get('HANDLER_FOR_404')
#         )
#         self.handler500 = import_object(
#             self.config.get('HANDLER_FOR_500')
#         )
# 
#     def set_static_route(self):
#         # add static dir route
#         self.static_root = self.config.get('STATIC_ROOT')
#         self.static_path = self.config.get('STATIC_PATH')
#         if self.static_root and self.static_path:
#             self.router.add_route(
#                 self.static_path + '{file:path}',
#                 DirectoryApp,
#                 'static'
#             )
# 
#     def load_config(self, config):
#         self.config.load_from_object(config)
#         self.set_static_route()
# 
#     def add_route(self, pattern, handler, name):
#         self.router.add_route(pattern, handler, name)
# 
#     def __call__(self, environ, start_response):
#         self.request = Request(environ)
#         request_started_signal.send(self, request=self.request)
#         self.response = Response()
#         path = self.request.path_info
#         route = self.router.find_route(path)
#         if route:
#             try:
#                 func = route.resolve_func()
#                 # check is route for static files
#                 if route.name == 'static':
#                     static_handler = func(self.static_root)
#                     self.request.path_info = self.request.path_info.replace(
#                         self.static_path, ''
#                     )
#                     resp = self.request.get_response(static_handler)
#                     return resp(environ, start_response)
#                 args = route.get_args(path)
#                 handler = func(self, self.request, self.response)
#                 handler.dispatch(*args)
#                 request_finished_signal.send(self, response=handler.response)
#                 return handler.response(environ, start_response)
#             except Exception:
#                 exc_info = sys.exc_info()
#                 request_exception_signal.send(
#                     self, request=self.request, exc_info=exc_info
#                 )
#                 handler_500 = self.handler500(
#                     self, self.request, self.response
#                 )
#                 handler_500.dispatch(exc_info)
#                 return handler_500.response(environ, start_response)
#         else:
#             handler_404 = self.handler404(self, self.request, self.response)
#             handler_404.dispatch()
#             return handler_404.response(environ, start_response)


# Path: examples/hello_world/app.py
from gunstar.app import Application
    from wsgiref.simple_server import make_server
import os

# -*- coding: utf-8 -*-


PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))


class ConfigSettings(object):
    
    TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
    SECRET_KEY = 'my-secret-key'
    STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
    STATIC_PATH = '/static/'


routes = (
    ('/', 'handlers.IndexHandler', 'index'),
    ('/name/{name}/', 'handlers.OtherHandler', 'other'),
)


app = Application(routes=routes, config=ConfigSettings)


if __name__ == '__main__':
    server = make_server('127.0.0.1', 8080, app)

    server.serve_forever()