Repository navigation
v0.4.0
v0.4.0 - Major redesign
With the recent changes to reuse an existing logger, rather than creating a new one for each use of the middleware, I realized that the new usage would look like this:
http.Handle("/" log.logAll(logger, auth.Auth(logger, someHandlerFunc(logger)))Instead of having to pass in the shared logger to each method, I made the Middleware into an object that contains a Logger (and a DB connection while I was at it). That means the usage would like this:
// Initialize middleware, in this case with db=false and log=true, since the auth middleware makes use of the logger
// A Middleware object just consists of a DB connection and a zap Logger.
// The _ is an ignored error for if initializing failed, you can also save this as err and handle it
mw, _ := middlware.Init(false, true)
http.Handle("/" mw.logAll(mw.Auth(someHandlerFunc(mw.logger)))This is much better, but having to call logAll and Auth on every endpoint could get annoying, so I also added an mw.All() function that applies both of those, as well as an additional middleware to redirect HTTP requests to HTTPS, as well as a mw.AllNoAuth() for if you want all the middleware without the requirement for the user to be logged in. This leads to usage like this:
mw, _ := middlware.Init(false, true)
http.Handle("/" mw.All(someHandlerFunc(mw.logger)))