<moya xmlns="http://moyaproject.com"
	  xmlns:moya="http://moyaproject.com"
	  xmlns:let="http://moyaproject.com/let">

	<mountpoint name="main">
		<url route="/" view="serve" name="root" let:path="'/'"/>
		<url route="{*path}" view="serve" name="serve"/>
	</mountpoint>

	<view docname="serve">
		<!-- if the path is not a directory then fall through to serving a file -->
        <let fsname=".app.settings.fs" />
		<throw exception="moya.static.nofs" msg="No filesystem called '${fsname}'" if="fsname not in .fs"/>
		<call macro="servedir" let:path="url.path" let:fsname="fsname"/>
		<call macro="servefile" let:path="url.path" let:fsname="fsname" />
	</view>

	<view docname="servedir">
		<!-- Call our Python macro to get the directory listing -->
        <let fs=".fs[fsname]"
        	show_permissions=".debug or .app.settings.show_permissions.bool"
        	show_debug=".debug or .app.settings.show_debug.bool"/>
		<call macro="#read_directory" dst="dir" let:permissions="show_permissions">
			<var>fs</var>
			<var>path</var>
		</call>
		<!-- read_directory returns None if it is not a directory-->
		<return if="isnone:dir" />

		<!-- 404 if directory listing is disabled -->
		<not-found if="not .app.settings.auto_index.bool" />
		<!-- Redirect to a canonical url ending with / -->
		<redirect-to url="${.request.path}/" if="not .request.path $= '/'" />
		<!-- Serve an index file if enabled in settings -->
		<for dst="indexfile" src=".app.settings.index.list">
			<serve-file fs="${fsname}" path="${path:path + indexfile}" ifexists="yes" />
		</for>

        <!-- Serve the directory listing-->
		<serve-template template="directory.html" withscope="yes"
			let:directory="dir.directory" let:app=".app"/>
	</view>

	<view docname="servefile">
		<!-- 404 if the path matches hidden wildcards -->
		<let hide_wildcards=".app.settings.hide.list"
			serve_templates=".app.settings.serve_templates.bool"/>

		<!-- 404 if file is a hidden wildcard -->
		<not-found if="hide_wildcards and basename:path fnmatches hide_wildcards"/>

		<!-- Serve as template if serve_template setting is True -->
        <serve-template-fs fs=".app.settings.fs" path="${path}"
        	if="serve_templates and ext:path=='html'"/>
        <catch exception="render-template-fs.read-fail" />

		<!-- Serve file if it exists -->
		<serve-file fs="${fsname}" path="${path}" ifexists="yes"/>
		<not-found/>
	</view>

	<check xmlns="http://moyaproject.com/preflight">
        <fail if="not strip:.app.settings.fs">
            A value for 'fs' is required -- check your settings
        </fail>
		<fail if="not .fs[.app.settings.fs]">
			No filesystem called '${.app.settings.fs}' -- check your settings
		</fail>
	</check>

</moya>