mirror of
https://github.com/apple/swift-nio-extras.git
synced 2025-05-14 17:02:43 +08:00
bring in swift-algorithms, scrap NIOHTTPResponsivenessServer
This commit is contained in:
parent
b18f2a66d8
commit
2f9db74829
@ -203,6 +203,7 @@ var targets: [PackageDescription.Target] = [
|
||||
"NIOHTTPTypes",
|
||||
.product(name: "NIOCore", package: "swift-nio"),
|
||||
.product(name: "HTTPTypes", package: "swift-http-types"),
|
||||
.product(name: "Algorithms", package: "swift-algorithms"),
|
||||
],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency")
|
||||
@ -221,20 +222,6 @@ var targets: [PackageDescription.Target] = [
|
||||
.enableExperimentalFeature("StrictConcurrency")
|
||||
]
|
||||
),
|
||||
.executableTarget(
|
||||
name: "NIOHTTPResponsivenessServer",
|
||||
dependencies: [
|
||||
"NIOHTTPResponsiveness",
|
||||
"NIOHTTPTypesHTTP1",
|
||||
.product(name: "NIOCore", package: "swift-nio"),
|
||||
.product(name: "NIOPosix", package: "swift-nio"),
|
||||
.product(name: "NIOHTTP1", package: "swift-nio"),
|
||||
.product(name: "ArgumentParser", package: "swift-argument-parser"),
|
||||
],
|
||||
swiftSettings: [
|
||||
.enableExperimentalFeature("StrictConcurrency")
|
||||
]
|
||||
),
|
||||
]
|
||||
|
||||
let package = Package(
|
||||
@ -255,8 +242,8 @@ let package = Package(
|
||||
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"),
|
||||
.package(url: "https://github.com/apple/swift-http-structured-headers.git", from: "1.1.0"),
|
||||
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"),
|
||||
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.27.0"),
|
||||
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
|
||||
.package(url: "https://github.com/apple/swift-algorithms.git", from: "1.2.0"),
|
||||
|
||||
],
|
||||
targets: targets
|
||||
|
@ -12,6 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Algorithms
|
||||
import HTTPTypes
|
||||
import NIOCore
|
||||
import NIOHTTPTypes
|
||||
@ -48,7 +49,7 @@ public final class SimpleResponsivenessRequestMux: ChannelInboundHandler {
|
||||
return
|
||||
}
|
||||
|
||||
var pathComponents = path.utf8.split(separator: "?".utf8, maxSplits: 1).makeIterator()
|
||||
var pathComponents = path.utf8.lazy.split(separator: UInt8(ascii: "?"), maxSplits: 1).makeIterator()
|
||||
let firstPathComponent = pathComponents.next()!
|
||||
let queryArgsString = pathComponents.next()
|
||||
|
||||
|
@ -1,68 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
import NIOCore
|
||||
import NIOHTTP1
|
||||
import NIOHTTPResponsiveness
|
||||
import NIOHTTPTypesHTTP1
|
||||
import NIOPosix
|
||||
|
||||
func responsivenessConfigBuffer(scheme: String, host: String, port: Int) throws -> ByteBuffer {
|
||||
let cfg = ResponsivenessConfig(
|
||||
version: 1,
|
||||
urls: ResponsivenessConfigURLs(scheme: scheme, authority: "\(host):\(port)")
|
||||
)
|
||||
let encoded = try JSONEncoder().encode(cfg)
|
||||
return ByteBuffer(bytes: encoded)
|
||||
}
|
||||
|
||||
@main
|
||||
private struct HTTPResponsivenessServer: ParsableCommand {
|
||||
@Option(help: "Which host to bind to")
|
||||
var host: String
|
||||
|
||||
@Option(help: "Which port to bind to")
|
||||
var port: Int
|
||||
|
||||
func run() throws {
|
||||
let config = try responsivenessConfigBuffer(scheme: "http", host: host, port: port)
|
||||
|
||||
let socketBootstrap = ServerBootstrap(group: MultiThreadedEventLoopGroup.singleton)
|
||||
// Specify backlog and enable SO_REUSEADDR for the server itself
|
||||
.serverChannelOption(ChannelOptions.backlog, value: 256)
|
||||
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
||||
|
||||
// Set the handlers that are applied to the accepted Channels
|
||||
.childChannelInitializer({ channel in
|
||||
channel.pipeline.configureHTTPServerPipeline().flatMapThrowing {
|
||||
let mux = SimpleResponsivenessRequestMux(responsivenessConfigBuffer: config)
|
||||
return try channel.pipeline.syncOperations.addHandlers([
|
||||
HTTP1ToHTTPServerCodec(secure: false),
|
||||
mux,
|
||||
])
|
||||
}
|
||||
})
|
||||
|
||||
// Enable SO_REUSEADDR for the accepted Channels
|
||||
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
||||
.childChannelOption(ChannelOptions.tcpOption(.tcp_nodelay), value: 1)
|
||||
|
||||
let insecureChannel = try socketBootstrap.bind(host: host, port: port).wait()
|
||||
print("Listening on http://\(host):\(port)")
|
||||
|
||||
let _ = try insecureChannel.closeFuture.wait()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user