0%

Vapor最简单的服务器

Package.swift

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "HelloWorld",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
         .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-alpha.2"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "HelloWorld",
            dependencies: ["Vapor"]),
        .testTarget(
            name: "HelloWorldTests",
            dependencies: ["HelloWorld"]),
    ]
)

main.swift

import Vapor

let app = Application(environment: try .detect()) { (service) in
    service.extend(Routes.self) { (routes, container) in
        routes.get("hello") { (req) -> String in
            "Hello World!"
        }
    }
}
try app.run()

–EOF–