Mandarine.TS
Latest
Latest
  • Mandarine.TS
  • Getting started
    • Installing Deno Runtime
    • Setting up Environment
    • Quick Example
  • Concepts
  • Mandarine Project Structure
  • Mandarine CLI
    • CLI Reference
      • mandarine new
      • mandarine generate
      • mandarine run
  • Mandarine Core
    • Core Initialization
    • Mandarine.json
    • Dot env file
    • Properties
      • The @Value Decorator
    • Components
      • Controller
      • Middleware
      • Repository
      • Component
      • Service
      • Configuration
      • Manual Component
    • Dependency Injection
      • Accessing DI container
    • Resource Handlers
      • Resource Handler Registry
      • Resource Handler
      • Resource Resolver
  • Mandarine MVC
    • Web MVC
    • Controllers
      • Routes
        • Parameterized Routes
      • HTTP Handlers
      • @ResponseStatus
    • Custom Middleware
    • Session Middleware
    • CORS Middleware
    • Template Engine
      • @Render Decorator
      • Accessing data from template
    • Launching web-app
    • Serving Static Content
    • Multipart Form Data
  • Mandarine Security
    • Sessions
  • Mandarine Data
    • ORM
      • Data source
      • Models
      • Repositories
        • Interacting with data
        • Updating data
    • Mandarine Query Language
  • Resources
    • Changelog
  • Plugins
    • Optional
    • Promise Repeater
Powered by GitBook
On this page
  • Main
  • Overriding Behavior

Was this helpful?

  1. Mandarine Core

Resource Handlers

PreviousAccessing DI containerNextResource Handler Registry

Last updated 4 years ago

Was this helpful?

This article requires extensive knowledge of &

Main

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 . Internally, Mandarine has a Resource Handler that will get the requested file based on the URL of the request.

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 ( is recommended). This component must implement Mandarine.MandarineMVC.Configurers.WebMVCConfigurer .

import { Mandarine, Configuration } from "https://deno.land/x/mandarinets/mod.ts";

@Configuration()
export class WebMVCConfigurer implements Mandarine.MandarineMVC.Configurers.WebMVCConfigurer {

}

Then, we need to override the method addResourceHandlers , which we will also need to decorate with @Injectable() .

import { Mandarine, Configuration } from "https://deno.land/x/mandarinets/mod.ts";

@Configuration()
export class WebMVCConfigurer implements Mandarine.MandarineMVC.Configurers.WebMVCConfigurer {

    @Injectable()
    public addResourceHandlers(): Mandarine.MandarineCore.IResourceHandlerRegistry {
    }
    
}

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()
export class WebMVCConfigurer implements Mandarine.MandarineMVC.Configurers.WebMVCConfigurer {

    @Injectable()
    public addResourceHandlers(): 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(
            new ResourceHandler()
            .addResourceHandler(new RegExp("/css/(.*)"))
            .addResourceHandlerLocation("./src/main/resources/static/css")
            .addResourceCors({
                origin: "https://stackoverflow.com"
            })
            .addResourceResolver(new MandarineResourceResolver())
        ).addResourceHandler(
            new ResourceHandler()
            .addResourceHandler(new RegExp("/js/(.*)"))
            .addResourceHandlerLocation("./src/main/resources/static/js")
            .addResourceResolver(new MandarineResourceResolver())
        );

        return resourceHandlerRegistry;
    }

}

Please refer to & before continuing.

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 .

Components
Manual Components
static content
(See source code here)
Configuration component
Resource Handler
Resource Handler Registry
here