Skip to content
Rodrigo Fernandes edited this page Nov 6, 2013 · 1 revision

Modules are a convenient way to extend functionality to all the webservices you develop. Answer provides entry points for modules at specific points in the request life cycle. This can be used for providing Authentication, Authorization, Auditing, etc.

Typical request life cycle:

Request -> [InFlow:OK] -> ServiceCall (ok) -> [OutFlow]
                                  (error) -> [OutFlowFault]

The modules can control whether or not to call the service, by returning OK, SKIP or ERROR status in their Inflow implementation.

Request -> [Inflow: SKIP] -> [OutFlow]
Request -> [Inflow: ERROR] -> [OutFlowFault]

Building

Add a dependency on lib answer-module and compile your target as a shared library

Deploying

Please refer to each adapter's configuration for details on how to deploy the newly created module.

Example - Performance Logger

The following example prints out the time it took to from inflow to outflow, which corresponds to the time service execution time.

class LogModule: public answer::Module{
  std::ofstream _log;
  std::chrono::time_point<std::chrono::system_clock> _start;

public:
  LogModule():_log("/tmp/logExample"){
  }
  
  virtual FlowStatus inFlow ( answer::Context& context ){
    log << "Requesting: "
        << context.operationInfo().service()
        << "/" << context.operationInfo().operation() << std::endl;
    _start = std::chrono::system_clock::now();
    return OK;
  }
  virtual FlowStatus outFlow ( answer::Context& context ){
    std::time_t end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-_start;
    log << "Requesting[OK]: "
        << context.operationInfo().service()
        << "/" << context.operationInfo().operation()
        << " took: " << elapsed_seconds.count()
        << std::endl;
    
    return OK;
  }
  virtual FlowStatus outFlowFault ( answer::Context& context ){
    std::time_t end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-_start;
    log << "Requesting[FAULT]: "
        << context.operationInfo().service()
        << "/" << context.operationInfo().operation()
        << " took: " << elapsed_seconds.count()
        << std::endl;
    return OK;
  }
};

ANSWER_REGISTER_MODULE(LogModule)

Other examples are available in the source code's example directory.

Clone this wiki locally