Routing component
Installation
Swift
Download and install Swift
Swift Package
mkdir MyApp
cd MyApp
swift package init --type executable // Creates an executable app named "MyApp"
Package.swift
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(name: "chaqmoq-routing", url: "https://github.com/chaqmoq/routing.git", .branch("master"))
],
targets: [
.target(name: "MyApp", dependencies: [
.product(name: "Routing", package: "chaqmoq-routing"),
]),
.testTarget(name: "MyAppTests", dependencies: [
.target(name: "MyApp")
])
]
)
Build
swift build -c release
Usage
main.swift
import Routing
// RouteCollection
let routes = RouteCollection([
Route(method: .GET, path: "/posts", name: "post_list") { _ in Response() }!,
Route(method: .POST, path: "/posts", name: "post_create") { _ in Response() }!
])
let posts = routes.builder.grouped("/posts", name: "post_")!
posts.group("/{id<\\d+>}") { post in
post.delete(name: "delete") { _ in Response() }
post.get(name: "get") { _ in Response() }
post.put(name: "update") { _ in Response() }
}
print(routes.count) // 4
print(routes[.DELETE].count) // 1
print(routes[.GET].count) // 2
print(routes[.POST].count) // 1
print(routes[.PUT].count) // 1
// Router
let router = Router(routes: routes)
// Resolving a Route
var route = router.resolveRouteBy(method: .GET, uri: "/posts")!
print(route.name) // "post_list"
route = router.resolveRoute(named: "post_get", parameters: ["id": "1"])!
print(route.name) // "post_get"
route = router.resolveRoute(named: "post_create")!
print(route.name) // "post_create"
// Generating a URL
var url = router.generateURLForRoute(named: "post_list", query: ["filter": "latest"])!
print(url.absoluteString) // "/posts?filter=latest"
url = router.generateURLForRoute(named: "post_get", parameters: ["id": "1"], query: ["shows_tags": "true"])!
print(url.absoluteString) // "/posts/1?shows_tags=true"
url = router.generateURLForRoute(named: "post_delete", parameters: ["id": "1"])!
print(url.absoluteString) // "/posts/1"
Run
swift run
Tests
swift test --enable-test-discovery --sanitize=thread