nova_router

Nova router is in charge of all routing in Nova. When a nova application is started its routing-file is read and processed by the router. You should not use this module directly if you're not certain of what you are doing.

A basic routing file could look something like this;


   #{prefix => "",
     security => false,
     routes => [
                {"/", {hello_world_controller, index}},
                {"/blog", {my_blog_controller, index}},
                {404, {error_controller, not_found}}
               ],
     statics => [
                 {"/assets/[...]", "assets"}
                ]
    }.
   
Lets go through it in sections.

prefix - This tells us if the routes should have a common prefix (Eg "/v1" or similar)

security - If this is a tuple of type {Module, Function} Nova will try and call that function each time a page from the routes-section is visited. That function could return either true or false, telling nova if the user should be allowed to visit the page or not.

routes - Describes the routes inside this block. (An application can have several of these maps defined)

statics - Routes for all the static assets

Routing more in detail

There's three different ways of describing a route, depending on what kind of protocol (http, websocket) or what options you have.

{Route, {Module, Function}} - This is a regular route used for HTTP.

Extend the introduction to how routing works.

Types

app_info/0
route/0
route_info/0

Functions

status_page(Status, Req) ->
Checks if there is a route for a special "status page" in the route table. This is done outside of cowboy since they does not support having custom pages for status pages (Eg 404)
add_route(RouteInfo, Route) ->
Add a single route to nova.
get_all_routes() ->
    Returns all the routes for this node. The RouteTable contains all routes injected into cowboy while the StaticRouteTable contains route information about status pages (eg 404).
    get_app(App) ->
    • App = atom()
    Returns all the information about a certain app and all of it's routes.
    get_main_app() ->
      Returns the name of the application that intiated the start.
      set_main_app(App) ->
      • App = atom()
      Sets the main app. This is the app that will be used when returning configuration and that have the highest priority when it comes to routing.
      process_routefile(AppInfo) ->
      • AppInfo = map()
      Process the routefile for the specified application and injects the resulting route-table into cowboy.

      We need this to work in a recursive manner.

      apply_routes() ->
        Takes all the routes in nova_router and applies them in cowboy_router. The changes should be instant.