Skip to main content

Write a Custom Middleware

January ships with built-in middleware package, which is initialised in the init.january.go file.
Adding a middleware to your application is 2 simple steps:
  1. Write the Middleware
  2. Wire it to the January Router
For this tutorial let’s write a simple CORS middle.

1. Writing a Middleware

Create a file named cors.go in the middleware package and add a method called Cors with a receiver m *Middleware
middleware/cors.go
now, let’s add the middleware logic to our cors middleware:
middleware/cors.go

2. Wiring to the January Router

In the init.january.go file, add the middle just below the apply middleware comment:
init.january.go

Try it out

Run the application with make start
with your application running, you can test the middleware: