Handler Interceptors in Procyon

Burak Köken
3 min readDec 22, 2020

If you haven’t read my previous article on Building Rest API in Procyon- Framework, do check it out.

In this article, we will be learning all the basics about the handler interceptors and how to use them in your Procyon application.

Let’s get started!

What are handler interceptors?

Handler interceptors let us perform some operations such as logging the request, capturing metrics, etc. before and after processing our HTTP requests.

How to use Handler Interceptors?

Now, that you understand what a handler interceptor is and why it is used, it’s time to learn how to use it.

Procyon provides the following three interceptor interfaces. Depending on your needs, you should implement them.

  • HandleBefore is invoked before the handler method is executed, but the response is not generated yet.
  • HandlerAfter is invoked after the handler method is executed.
  • AfterComletion is invoked after the request processing is completed and response is generated.

Custom Interceptor

In this example, we will focus on logging the request in our web application. First of all, Our struct needs to implement the interfaces above. We are defining the CustomInterceptor consisting of a field of type context.Logger as follows, and declaring a function taking a parameter of type context.Logger.

Note: The parameter of type Logger is passed to the NewCustomInterceptor function by the framework automatically.

interceptor.go

Of course, as you know from my previous article, you need to register the NewCustomInterceptor function in the init function as shown below.

Next, let’s focus on custom interceptor implementations:

Method HandleBefore

This method is called before handling a request.

Note: With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response. To abort the execution chain, the Cancel method needs to be called as follows.

Aborting the execution chain

In our example, we are using it to log information about the request using the logger.

interceptor.go

As we can see, we’re logging some basic information about the request.

Method HandleAfter

This method is invoked after the handler method is executed but the response is not generated yet.

We can use this method to determine the time taken by the handler method to process a request.

interceptor.go

In our case, we simply log a message.

Method AfterCompletion

This method is invoked when a request is finished and the response is written. We may obtain request and response data, and information about errors if any occurred.

interceptor.go

We assume we have an implementation of Controller as follows.

controller.go

Finally, run your application and hit the below URL in the Postman application and you can see the output as shown under.

Get API: http://localhost:8080/api/user

Postman

You can see the logs printed in the screenshot given below.

Console

Hopefully, you got a better understanding and idea of what Procyon Handler Interceptors are, what they can offer, and when to use them.

HAPPY CODING…

You can find the code on Github. And if you would like to contribute to the library or you find a bug, please report it.

--

--