# Path: web/app/static.py
# def static(base, mapping=None, far=('js', 'css', 'gif', 'jpg', 'jpeg', 'png', 'ttf', 'woff')):
# 	"""Serve files from disk.
# 	
# 	This utility endpoint factory is meant primarily for use in development environments; in production environments
# 	it is better (more efficient, secure, etc.) to serve your static content using a front end load balancer such as
# 	Nginx.
# 	
# 	The first argument, `base`, represents the base path to serve files from. Paths below the attachment point for
# 	the generated endpoint will combine this base path with the remaining path elements to determine the file to
# 	serve.
# 	
# 	The second argument is an optional dictionary mapping filename extensions to template engines, for cooperation
# 	with the TemplateExtension.  (See: https://github.com/marrow/template)  The result of attempting to serve a
# 	mapped path is a 2-tuple of `("{mapping}:{path}", dict())`. For example, to render all `.html` files as Mako
# 	templates, you would attach something like the following:
# 	
# 		class Root:
# 			page = static('/path/to/static/pages', dict(html='mako'))
# 	
# 	By default the "usual culprits" are served with far-futures cache expiry headers. If you wish to change the
# 	extensions searched just assign a new `far` iterable.  To disable, assign any falsy value.
# 	"""
# 	
# 	base = abspath(base)
# 	
# 	@staticmethod
# 	def static_handler(context, *parts, **kw):
# 		path = normpath(pathjoin(base, *parts))
# 		
# 		if __debug__:
# 			log.debug("Attempting to serve static file.", extra=dict(
# 					request = id(context),
# 					base = base,
# 					path = path
# 				))
# 		
# 		if not path.startswith(base):  # Ensure we only serve files from the allowed path.
# 			raise HTTPForbidden("Cowardly refusing to violate base path policy." if __debug__ else None)
# 		
# 		if not exists(path):  # Do the right thing if the file doesn't actually exist.
# 			raise HTTPNotFound()
# 		
# 		if not isfile(path):  # Only serve normal files; no UNIX domain sockets, FIFOs, etc., etc.
# 			raise HTTPForbidden("Cowardly refusing to open a non-file." if __debug__ else None)
# 		
# 		if far and path.rpartition('.')[2] in far:
# 			context.response.cache_expires = 60*60*24*365
# 		
# 		if mapping:  # Handle the mapping of filename extensions to 2-tuples. 'Cause why not?
# 			_, _, extension = basename(path).partition('.')
# 			if extension in mapping:
# 				return mapping[extension] + ':' + path, dict()
# 		
# 		return open(path, 'rb')
# 	
# 	return static_handler


import os
from web.app.static import static
	from web.core.application import Application
		"""Initialize our controller, either saving the context or getting anything we need from it."""
		self._ctx = context
	
	def __call__(self):
		"""Handle "index" lookups.
		
		This is analogous to an `index.html` file, accessible as the default representation of a folder, that is
		otherwise not accessible as `index.html`. The presence of a `__call__` method makes the controller instance
		itself a valid callable endpoint.
		"""
		
		return "Path: /"
	
	def index(self):
		"""Handle calls to /index.
		
		WebCore 1 treated "index" lookups and the `index` method specially. WebCore 2 does not, freeing the name to be
		used for user-defined endpoints.
		"""
		return "Path: /index"
	
	# Attributes may be assigned statically; these are not callable endpoints but rather static endpoints.
	# WebCore will treat access to these (via /foo, /bar, etc.) as if a callable endpoint was called and the static
	# value returned. This can serve any type of object there is a view registered for.
	string = "Static string!"
	source = open(__file__, 'rb')
	
	# This is a reusable endpoint (like an endpoint factory) serving static files from disk. Serving static files this
	# way is only really useful in development; in production serve them from a production quality front-end server,
	# load balancer, or CDN, such as via Nginx.

# Path: web/app/static.py
# def static(base, mapping=None, far=('js', 'css', 'gif', 'jpg', 'jpeg', 'png', 'ttf', 'woff')):
# 	"""Serve files from disk.
# 	
# 	This utility endpoint factory is meant primarily for use in development environments; in production environments
# 	it is better (more efficient, secure, etc.) to serve your static content using a front end load balancer such as
# 	Nginx.
# 	
# 	The first argument, `base`, represents the base path to serve files from. Paths below the attachment point for
# 	the generated endpoint will combine this base path with the remaining path elements to determine the file to
# 	serve.
# 	
# 	The second argument is an optional dictionary mapping filename extensions to template engines, for cooperation
# 	with the TemplateExtension.  (See: https://github.com/marrow/template)  The result of attempting to serve a
# 	mapped path is a 2-tuple of `("{mapping}:{path}", dict())`. For example, to render all `.html` files as Mako
# 	templates, you would attach something like the following:
# 	
# 		class Root:
# 			page = static('/path/to/static/pages', dict(html='mako'))
# 	
# 	By default the "usual culprits" are served with far-futures cache expiry headers. If you wish to change the
# 	extensions searched just assign a new `far` iterable.  To disable, assign any falsy value.
# 	"""
# 	
# 	base = abspath(base)
# 	
# 	@staticmethod
# 	def static_handler(context, *parts, **kw):
# 		path = normpath(pathjoin(base, *parts))
# 		
# 		if __debug__:
# 			log.debug("Attempting to serve static file.", extra=dict(
# 					request = id(context),
# 					base = base,
# 					path = path
# 				))
# 		
# 		if not path.startswith(base):  # Ensure we only serve files from the allowed path.
# 			raise HTTPForbidden("Cowardly refusing to violate base path policy." if __debug__ else None)
# 		
# 		if not exists(path):  # Do the right thing if the file doesn't actually exist.
# 			raise HTTPNotFound()
# 		
# 		if not isfile(path):  # Only serve normal files; no UNIX domain sockets, FIFOs, etc., etc.
# 			raise HTTPForbidden("Cowardly refusing to open a non-file." if __debug__ else None)
# 		
# 		if far and path.rpartition('.')[2] in far:
# 			context.response.cache_expires = 60*60*24*365
# 		
# 		if mapping:  # Handle the mapping of filename extensions to 2-tuples. 'Cause why not?
# 			_, _, extension = basename(path).partition('.')
# 			if extension in mapping:
# 				return mapping[extension] + ':' + path, dict()
# 		
# 		return open(path, 'rb')
# 	
# 	return static_handler


# Path: example/controller.py
import os
from web.app.static import static
	from web.core.application import Application

		"""Initialize our controller, either saving the context or getting anything we need from it."""
		self._ctx = context
	
	def __call__(self):
		"""Handle "index" lookups.
		
		This is analogous to an `index.html` file, accessible as the default representation of a folder, that is
		otherwise not accessible as `index.html`. The presence of a `__call__` method makes the controller instance
		itself a valid callable endpoint.
		"""
		
		return "Path: /"
	
	def index(self):
		"""Handle calls to /index.
		
		WebCore 1 treated "index" lookups and the `index` method specially. WebCore 2 does not, freeing the name to be
		used for user-defined endpoints.
		"""
		return "Path: /index"
	
	# Attributes may be assigned statically; these are not callable endpoints but rather static endpoints.
	# WebCore will treat access to these (via /foo, /bar, etc.) as if a callable endpoint was called and the static
	# value returned. This can serve any type of object there is a view registered for.
	string = "Static string!"
	source = open(__file__, 'rb')
	
	# This is a reusable endpoint (like an endpoint factory) serving static files from disk. Serving static files this
	# way is only really useful in development; in production serve them from a production quality front-end server,
	# load balancer, or CDN, such as via Nginx.

	example = static(os.path.dirname(__file__))  # Serve the directory this source file is in. (Don't do this. ;)