Resource handlers are a way to intercept and resolve a specific resources requested by a client. They work as a form of middleware interceptor.
A good example of a resource handler is the way mandarine handles static content. Internally, Mandarine has a Resource Handler that will get the requested file based on the URL of the request. (See source code here)
Overriding Behavior
It is possible to create your own resource handlers & resource resolvers. For this, Mandarine allows you to implement Mandarine.MandarineMVC.Configurers.WebMVCConfigurer which will override the default behavior established by Mandarine.
In order to override this behavior, create a component (Configuration component is recommended). This component must implement Mandarine.MandarineMVC.Configurers.WebMVCConfigurer .
Our method addResourceHandlers will return an instance of the Resource Handler Registry with our resource handlers now added.
import { Mandarine, Configuration, ResourceHandler, MandarineResourceResolver } from "https://deno.land/x/mandarinets/mod.ts";
@Configuration()exportclassWebMVCConfigurerimplementsMandarine.MandarineMVC.Configurers.WebMVCConfigurer { @Injectable()publicaddResourceHandlers():Mandarine.MandarineCore.IResourceHandlerRegistry {/** * Using `getNew()` : Mandarine.Global.getResourceHandlerRegistry().getNew() * will override the static content handler Mandarine has predefined. * This will mean you will have to add a Resource Handler to handle static content. * Do not use `getNew()` in order to keep the default static content handler. */let resourceHandlerRegistry =Mandarine.Global.getResourceHandlerRegistry();// Or let resourceHandlerRegistry = Mandarine.Global.getResourceHandlerRegistry().getNew();resourceHandlerRegistry.addResourceHandler(newResourceHandler().addResourceHandler(newRegExp("/css/(.*)")).addResourceHandlerLocation("./src/main/resources/static/css").addResourceCors({ origin:"https://stackoverflow.com" }).addResourceResolver(newMandarineResourceResolver()) ).addResourceHandler(newResourceHandler().addResourceHandler(newRegExp("/js/(.*)")).addResourceHandlerLocation("./src/main/resources/static/js").addResourceResolver(newMandarineResourceResolver()) );return resourceHandlerRegistry; }}
In the example above, we are adding a resource handler that will be executed every time our client requests "/css" or "/js". See a better explanation of this here.