Scoop API

scoop:scoop.http.server:0.1.0

src/router.scoop

Selected production source · src/router.scoop

1package scoop.http.server
3import scoop.http.isHttpToken
5internal class GroupConfig(val prefix: String, val parent: GroupConfig?) {
6 val middleware: ArrayList<HttpMiddleware> = ArrayList<HttpMiddleware>()
7 var fallback: HttpHandler? = None
8}
10internal class RouteConfig(
11 val method: String,
12 val pattern: RoutePattern,
13 val handler: HttpHandler,
14 val group: GroupConfig,
15 val middleware: Array<HttpMiddleware>
18internal class RouterConfig {
19 val root: GroupConfig = GroupConfig("", None)
20 val groups: ArrayList<GroupConfig> = ArrayList<GroupConfig>()
21 val routes: ArrayList<RouteConfig> = ArrayList<RouteConfig>()
22 var built: Boolean = false
24 fun requireOpen() {
25 if (built)
26 throw IllegalStateException(Some("router builder has already been built"))
27 }
29 fun add(
30 group: GroupConfig,
31 method: String,
32 path: String,
33 handler: HttpHandler,
34 middleware: List<HttpMiddleware>
35 ) {
36 requireOpen()
37 if (!isHttpToken(method))
38 throw IllegalArgumentException(Some("route method must be an HTTP token"))
39 routes.add(
40 RouteConfig(
41 method,
42 routePattern(joinedRoute(group.prefix, path)),
43 handler,
44 group,
45 middleware.toArray()
46 )
47 )
48 }
51/** Configures routes and freezes their shared state before publishing the application. */
52public fun routing(configure: RouterBuilder.() -> Unit = {}): HttpApplication {
53 val builder = RouterBuilder()
54 builder.configure()
55 return builder.build()
58/** Route configuration valid only until routing publishes its application. */
59public class RouterBuilder internal constructor() {
60 private val config: RouterConfig = RouterConfig()
61 private var currentApplication: HttpApplicationOptions = httpApplicationOptions()
62 public var application: HttpApplicationOptions
63 get() = currentApplication
64 set(value) {
65 config.requireOpen();
66 currentApplication = value
67 }
68 public fun application(configure: HttpApplicationOptionsBuilder.() -> Unit): Unit {
69 config.requireOpen()
70 application = application.copy(configure)
71 }
73 private val root: RouteGroup = RouteGroup(config, config.root)
75 /** Registers a method and path with optional endpoint middleware. */
76 public fun route(
77 method: String,
78 path: String,
79 handler: HttpHandler,
80 middleware: List<HttpMiddleware> = ArrayList<HttpMiddleware>()
81 ) {
82 root.route(method, path, handler, middleware)
83 }
84 /** Registers a GET endpoint, also serving implicit HEAD when no HEAD is registered. */
85 public fun get(path: String, handler: HttpHandler) {
86 route("GET", path, handler)
87 }
88 /** Registers a POST endpoint. */
89 public fun post(path: String, handler: HttpHandler) {
90 route("POST", path, handler)
91 }
92 /** Registers a PUT endpoint. */
93 public fun put(path: String, handler: HttpHandler) {
94 route("PUT", path, handler)
95 }
96 /** Registers a PATCH endpoint. */
97 public fun patch(path: String, handler: HttpHandler) {
98 route("PATCH", path, handler)
99 }
100 /** Registers a DELETE endpoint. */
101 public fun delete(path: String, handler: HttpHandler) {
102 route("DELETE", path, handler)
103 }
104 /** Registers an explicit HEAD endpoint. */
105 public fun head(path: String, handler: HttpHandler) {
106 route("HEAD", path, handler)
107 }
108 /** Registers an explicit OPTIONS endpoint. */
109 public fun options(path: String, handler: HttpHandler) {
110 route("OPTIONS", path, handler)
111 }
112 /** Appends a global middleware, including for unmatched requests. */
113 public fun use(middleware: HttpMiddleware) {
114 root.use(middleware)
115 }
116 /** Configures a path-prefix group synchronously. */
117 public fun group(prefix: String, configure: RouteGroup.() -> Unit) {
118 root.group(prefix, configure)
119 }
120 /** Sets the root fallback for unmatched paths. */
121 public fun fallback(handler: HttpHandler) {
122 root.fallback(handler)
123 }
124 /** Freezes the routing trie and all middleware snapshots. */
125 internal fun build(): HttpApplication {
126 config.requireOpen()
127 val application = buildApplication(config, this.application)
128 config.built = true
129 return application
130 }
133/** A configuration-only group sharing its builder's freeze boundary. */
134public class RouteGroup internal constructor(
135 private val config: RouterConfig,
136 private val current: GroupConfig
137) {
138 /** Registers one method and relative path suffix in this group. */
139 public fun route(
140 method: String,
141 path: String,
142 handler: HttpHandler,
143 middleware: List<HttpMiddleware> = ArrayList<HttpMiddleware>()
144 ) {
145 config.add(current, method, path, handler, middleware)
146 }
147 /** Registers a GET endpoint in this group. */
148 public fun get(path: String, handler: HttpHandler) {
149 route("GET", path, handler)
150 }
151 /** Registers a POST endpoint in this group. */
152 public fun post(path: String, handler: HttpHandler) {
153 route("POST", path, handler)
154 }
155 /** Registers a PUT endpoint in this group. */
156 public fun put(path: String, handler: HttpHandler) {
157 route("PUT", path, handler)
158 }
159 /** Registers a PATCH endpoint in this group. */
160 public fun patch(path: String, handler: HttpHandler) {
161 route("PATCH", path, handler)
162 }
163 /** Registers a DELETE endpoint in this group. */
164 public fun delete(path: String, handler: HttpHandler) {
165 route("DELETE", path, handler)
166 }
167 /** Registers an explicit HEAD endpoint in this group. */
168 public fun head(path: String, handler: HttpHandler) {
169 route("HEAD", path, handler)
170 }
171 /** Registers an explicit OPTIONS endpoint in this group. */
172 public fun options(path: String, handler: HttpHandler) {
173 route("OPTIONS", path, handler)
174 }
175 /** Appends middleware to this group's configured endpoints and fallback. */
176 public fun use(middleware: HttpMiddleware) {
177 config.requireOpen();
178 current.middleware.add(middleware)
179 }
180 /** Overrides the fallback for this prefix and inheriting nested groups. */
181 public fun fallback(handler: HttpHandler) {
182 config.requireOpen();
183 current.fallback = Some(handler)
184 }
185 /** Adds a nested prefix group; all paths remain relative to the root application. */
186 public fun group(prefix: String, configure: RouteGroup.() -> Unit) {
187 config.requireOpen()
188 val next = GroupConfig(joinedRoute(current.prefix, prefix), Some(current))
189 routePattern(next.prefix, true)
190 config.groups.add(next)
191 RouteGroup(config, next).configure()
192 }