Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active August 17, 2025 23:14
Show Gist options
  • Select an option

  • Save carlynorama/c9ec1d4dc46b6080ee6dafed32e355fb to your computer and use it in GitHub Desktop.

Select an option

Save carlynorama/c9ec1d4dc46b6080ee6dafed32e355fb to your computer and use it in GitHub Desktop.
Handy Router Extensions, Hummingbird
import Hummingbird
extension Router {
/// Motivation: the below won't take .redirect without `Response.` and I'm that lazy.
/// router.get("c") { _, _ in Response.redirect(to: "test.html", type: .normal) }
/// usage:
/// router.autoReply("a", response: .init(status: .ok))
/// router.autoReply("b", response: .redirect(to: "test.html", type: .normal))
func autoReply(_ path:RouterPath, response:Response) {
self.get(path, use: { _, _ in response})
}
// Response.RedirectType isn't Sendable.
// func redirect(path:RouterPath, to newPath:String, type:Response.RedirectType = .normal) {
// self.get(path, use: { _, _ in Response.redirect(to: newPath, type: type)})
// }
/// usage:
/// router.redirect(path: "d", to: "test.html")
func redirect(path:RouterPath, to newPath:String) {
self.get(path, use: { _, _ in Response.redirect(to: newPath)})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment