Middleware for flags

fao @commuterjoy @wheresrhys