dispatchSubsection

Allows for a subsection of your dispatched urls to be passed through other a pre-request filter, optionally pick up an new presenter class, and then be dispatched to their own handlers.

/+
// a long-form filter function
bool permissionCheck(DispatcherData)(DispatcherData dd) {
	// you are permitted to call mutable methods on the Cgi object
	// Note if you use a Cgi subclass, you can try dynamic casting it back to your custom type to attach per-request data
	// though much of the request is immutable so there's only so much you're allowed to do to modify it.

	if(checkPermissionOnRequest(dd.cgi)) {
		return true; // OK, allow processing to continue
	} else {
		dd.presenter.renderBasicError(dd.cgi, 403); // reply forbidden to the requester
		return false; // and stop further processing into this subsection
	}
}
+/

// but you can also do short-form filters:

bool permissionCheck(Cgi cgi) {
	return ("ok" in cgi.get) !is null;
}

// handler for the subsection
class AdminClass : WebObject {
	int foo() { return 5; }
}

// handler for the main site
class TheMainSite : WebObject {}

mixin DispatcherMain!(
	"/admin".dispatchSubsection!(
		// converts our short-form filter into a long-form filter
		passFilterOrIssueError!(permissionCheck, 403),
		// can use a new presenter if wanted for the subsection
		KeepExistingPresenter,
		// and then provide child route dispatchers
		"/".serveApi!AdminClass
	),
	// and back to the top level
	"/".serveApi!TheMainSite
);

Note you can encapsulate sections in files like this:

auto adminDispatcher(string urlPrefix) {
	return urlPrefix.dispatchSubsection!(
		....
	);
}

mixin DispatcherMain!(
	"/admin".adminDispatcher,
	// and so on
)

If you want no filter, you can pass (cgi) => true as the filter to approve all requests.

If you want to keep the same presenter as the parent, use KeepExistingPresenter as the presenter argument.

version(with_breaking_cgi_features)
dispatchSubsection
(
alias PreRequestFilter
NewPresenter
definitions...
)
(
string urlPrefix
)

Meta

History

Added January 28, 2023 (dub v11.0)