@@ -40,11 +40,15 @@ public final actor HTTPServer {
4040 private let config : Configuration
4141 private var handlers : RoutedHTTPHandler
4242
43- public init ( config: Configuration , handler: ( any HTTPHandler ) ) {
43+ init ( config: Configuration , handler: RoutedHTTPHandler ) {
4444 self . config = config
4545 self . handlers = Self . makeRootHandler ( to: handler)
4646 }
4747
48+ public init ( config: Configuration , handler: ( any HTTPHandler ) ? = nil ) {
49+ self . init ( config: config, handler: HTTPServer . makeRootHandler ( to: handler) )
50+ }
51+
4852 public init ( address: some SocketAddress ,
4953 timeout: TimeInterval = 15 ,
5054 pool: some AsyncSocketPool = defaultPool ( ) ,
@@ -255,6 +259,12 @@ public final actor HTTPServer {
255259 return root
256260 }
257261
262+ private static func makeRootHandler( to closure: @Sendable @escaping ( HTTPRequest) async throws -> HTTPResponse ) -> RoutedHTTPHandler {
263+ var root = RoutedHTTPHandler ( )
264+ root. appendRoute ( " * " , to: ClosureHTTPHandler ( closure) )
265+ return root
266+ }
267+
258268 public static func defaultPool( logger: some Logging = . disabled) -> some AsyncSocketPool {
259269#if canImport(Darwin)
260270 return . kQueue( logger: logger)
@@ -270,31 +280,26 @@ public extension HTTPServer {
270280
271281 init ( port: UInt16 ,
272282 timeout: TimeInterval = 15 ,
273- pool: some AsyncSocketPool = defaultPool ( ) ,
274283 logger: any Logging = defaultLogger ( ) ,
275284 handler: ( any HTTPHandler ) ? = nil ) {
276- #if canImport(WinSDK)
277- let address = sockaddr_in. inet ( port: port)
278- #else
279- let address = sockaddr_in6. inet6 ( port: port)
280- #endif
281- self . init ( address: address,
282- timeout: timeout,
283- pool: pool,
284- logger: logger,
285- handler: handler)
285+ let config = Configuration (
286+ port: port,
287+ timeout: timeout,
288+ logger: logger
289+ )
290+ self . init ( config: config, handler: handler)
286291 }
287292
288293 init ( port: UInt16 ,
289294 timeout: TimeInterval = 15 ,
290- pool: some AsyncSocketPool = defaultPool ( ) ,
291295 logger: any Logging = defaultLogger ( ) ,
292296 handler: @Sendable @escaping ( HTTPRequest) async throws -> HTTPResponse ) {
293- self . init ( port: port,
294- timeout: timeout,
295- pool: pool,
296- logger: logger,
297- handler: ClosureHTTPHandler ( handler) )
297+ let config = Configuration (
298+ port: port,
299+ timeout: timeout,
300+ logger: logger
301+ )
302+ self . init ( config: config, handler: Self . makeRootHandler ( to: handler) )
298303 }
299304}
300305
0 commit comments