Application Framework Reference

Module API

@route()

The @route() decorator permits URI targets to be routed to specific controller methods. The @route() decorator is often used with the @expose_page() decorator.

Note: Every controller method which needs to be exposed by the Splunk Web hierarchy at a target, other than the method name itself, should be preceded by an @route() decorator.

A route is specified using the following syntax:

  <controller_name>/
    The implied base path.
        e.g. my_controller/

@route('/:var')
    Any value for <controller_name> within 1 path segment
        e.g. my_controller/setup
            my_controller/foobar
            my_controller/*

@route('/:action=setup', methods='GET')
    Only requests that begin with /setup - the action kwarg will always be 'setup'
        e.g. my_controller/setup?action=foobar 
            unittest.assertEqual(action, 'setup')
    
@route('/:var/*category/:action', methods='POST')
    Adds a keyword argument called category that greedily matches path segments, but only for the POST verb
        e.g my_controller/setup/step1/confirm/areusure/doublecheck
            unittest.assertEqual(var, 'setup')
            unittest.assertEqual(category, '/step1/confirm/areusure')
            unittest.assertEqual(action, 'doublecheck')
 

Synopsis

@route(route, methods, leave_exposed)

Parameters

route

String

Route to expose for this method. Default= None .

methods

String

Comma-separated string of HTTP methods/verbs to route for. Default= None .

leave_exposed

Boolean

Method exposure control:

True = Leave method exposed at method name.

False = Do not leave method exposed at method name.

Example

The controller config.py has several methods, but only two are accessible using SplunkWeb:

  • http://<splunk-server>:<splunk-port>/config
  • http://<splunk-server>:<splunk-port>/config/<VAR>

where VAR is a valid key exposed by the config endpoint.

This is because of the use of the @route() decorator in the controller:

... elided ...
  
@route('/')
@expose_page(must_login=False, methods='GET')
def index(self, autoload=False, namespace=None, asDict=False):

... elided ...

@route('/:var')
@expose_page(methods='GET')
def getvar(self, var, **kw):  
 
... elided ...
 

The @route() decoration that precedes index() routes the root endpoint of the controller toindex(), while the @route() decorator that precedes getvar() routes any target below the root endpoint to the getvar(). Note that the var variable in the later decorator is passed to getvar() as a keyword argument.

Thus, navigating to /en-US/config/VERSION_LABEL results in the getvar() method being called with the parameter var='VERSION_LABEL' passed to it.

See Also

@expose_page()