diff --git a/Package.resolved b/Package.resolved index 75f1088..8120162 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,6 +1,69 @@ { - "originHash" : "4d3727268d48cbcdf607ca88aa6e0586c837b2ad667cdfddb7b76bb80a408d38", + "originHash" : "639a3281cdbe594ea840e9eecb77397301dda47161e6d2bf39148b1a0933701b", "pins" : [ + { + "identity" : "async-http-client", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/async-http-client.git", + "state" : { + "revision" : "0a9b72369b9d87ab155ef585ef50700a34abf070", + "version" : "1.23.1" + } + }, + { + "identity" : "async-kit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/async-kit.git", + "state" : { + "revision" : "e048c8ee94967e8d8a1c2ec0e1156d6f7fa34d31", + "version" : "1.20.0" + } + }, + { + "identity" : "console-kit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/console-kit.git", + "state" : { + "revision" : "966d89ae64cd71c652a1e981bc971de59d64f13d", + "version" : "4.15.1" + } + }, + { + "identity" : "multipart-kit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/multipart-kit.git", + "state" : { + "revision" : "a31236f24bfd2ea2f520a74575881f6731d7ae68", + "version" : "4.7.0" + } + }, + { + "identity" : "routing-kit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/routing-kit.git", + "state" : { + "revision" : "8c9a227476555c55837e569be71944e02a056b72", + "version" : "4.9.1" + } + }, + { + "identity" : "swift-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-algorithms.git", + "state" : { + "revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42", + "version" : "1.2.0" + } + }, + { + "identity" : "swift-asn1", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-asn1.git", + "state" : { + "revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6", + "version" : "1.3.0" + } + }, { "identity" : "swift-atomics", "kind" : "remoteSourceControl", @@ -19,6 +82,42 @@ "version" : "1.1.4" } }, + { + "identity" : "swift-crypto", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-crypto.git", + "state" : { + "revision" : "06dc63c6d8da54ee11ceb268cde1fa68161afc96", + "version" : "3.9.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ef18d829e8b92d731ad27bb81583edd2094d1ce3", + "version" : "1.3.1" + } + }, + { + "identity" : "swift-log", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-log.git", + "state" : { + "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", + "version" : "1.6.1" + } + }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, { "identity" : "swift-nio", "kind" : "remoteSourceControl", @@ -28,6 +127,51 @@ "version" : "2.76.1" } }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "2e9746cfc57554f70b650b021b6ae4738abef3e6", + "version" : "1.24.1" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "eaa71bb6ae082eee5a07407b1ad0cbd8f48f9dca", + "version" : "1.34.1" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "c7e95421334b1068490b5d41314a50e70bab23d1", + "version" : "2.29.0" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "bbd5e63cf949b7db0c9edaf7a21e141c52afe214", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-numerics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-numerics.git", + "state" : { + "revision" : "0a5bc04095a675662cf24757cc0640aa2204253b", + "version" : "1.0.2" + } + }, { "identity" : "swift-system", "kind" : "remoteSourceControl", @@ -36,6 +180,24 @@ "revision" : "c8a44d836fe7913603e246acab7c528c2e780168", "version" : "1.4.0" } + }, + { + "identity" : "vapor", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/vapor.git", + "state" : { + "revision" : "fb619ab6485a88787ef9c78ba70e7415f8ebf981", + "version" : "4.106.4" + } + }, + { + "identity" : "websocket-kit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/vapor/websocket-kit.git", + "state" : { + "revision" : "4232d34efa49f633ba61afde365d3896fc7f8740", + "version" : "2.15.0" + } } ], "version" : 3 diff --git a/Package.swift b/Package.swift index 7e227cc..6d741b6 100644 --- a/Package.swift +++ b/Package.swift @@ -3,15 +3,15 @@ import PackageDescription let package = Package( name: "http-server", + platforms: [.macOS(.v10_15)], dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), + .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), ], targets: [ .executableTarget( name: "http-server", dependencies: [ - .product(name: "NIO", package: "swift-nio"), - .product(name: "NIOHTTP1", package: "swift-nio"), + .product(name: "Vapor", package: "vapor"), ], path: ".", sources: ["main.swift"] diff --git a/main.swift b/main.swift index ac82913..00528d9 100644 --- a/main.swift +++ b/main.swift @@ -1,69 +1,25 @@ -// swift-tools-version:5.2 import Foundation -import NIO -import NIOHTTP1 -struct Version: Codable { - let version: String -} - -func JSON(_ value: T) -> Data { - return (try? JSONEncoder().encode(value)) ?? Data() -} - -final class HTTPHandler: ChannelInboundHandler { - typealias InboundIn = HTTPServerRequestPart - typealias OutboundOut = HTTPServerResponsePart - - func channelRead(context: ChannelHandlerContext, data: NIOAny) { - let req = self.unwrapInboundIn(data) +import Vapor - switch req { - case .head(let requestHead): - if requestHead.uri == "/version" && requestHead.method == .GET { - let version = Version(version: "1.0.0") - let response = JSON(version) - var headers = HTTPHeaders() - headers.add(name: "Content-Type", value: "application/json") - headers.add(name: "Content-Length", value: "\(response.count)") - let responseHead = HTTPResponseHead( - version: requestHead.version, - status: .ok, - headers: headers) - _ = context.write(self.wrapOutboundOut(.head(responseHead))) - var buffer = context.channel.allocator.buffer(capacity: response.count) - buffer.writeBytes(response) - _ = context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer)))) - _ = context.writeAndFlush(self.wrapOutboundOut(.end(nil))) - } else { - let responseHead = HTTPResponseHead(version: requestHead.version, status: .notFound) - _ = context.write(self.wrapOutboundOut(.head(responseHead))) - _ = context.writeAndFlush(self.wrapOutboundOut(.end(nil))) - } - - case .body: - break - - case .end: - break - } - } +struct Version : Content { + var version: String = "1.0.0" } -let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) -let bootstrap = ServerBootstrap(group: group) - .childChannelInitializer { channel in - channel.pipeline.configureHTTPServerPipeline().flatMap { - channel.pipeline.addHandler(HTTPHandler()) - } - } +struct Main { -defer { - try? group.syncShutdownGracefully() -} + static func main() async { + do { + let app = try await Application.make() + app.get("version", use: { _ in Version() }) + try await app.execute() + try await app.asyncShutdown() + } catch { + print("got error: \(error)") + } + } -let serverChannel = try bootstrap.bind(host: "127.0.0.1", port: 8000).wait() -print("listening on http://127.0.0.1:8000") -try serverChannel.closeFuture.wait() +} +_ = await Task{ await Main.main() }.value