vapor/vapor

`vapor new`'s template needs to be updated.

iWECon opened this issue · 3 comments

iWECon commented

Is your feature request related to a problem? Please describe.
NO.

Describe the solution you'd like
SE-0294 adds a new target option for apps using Swift Package manager, allowing us to explicitly declare an executable target.

This is particularly important for folks who want to use SE-0281 (using @main to mark your program’s entry point), because it didn’t play nicely with Swift Package Manager – it would always look for a main.swift file.

With this change, we can now remove main.swift and use @main instead. Note: You must specify // swift-tools-version:5.4 in your Package.swift file in order to get this new functionality.


As far as I know, creating an executable swift package in Xcode, now uses @main, maybe we can delete main.swift now and use @main instead?

iWECon commented

Use @main in App:

import Vapor

@main
struct Run {
    static func main() throws {
        var env = try Environment.detect()
        try LoggingSystem.bootstrap(from: &env)
        let app = Application(env)
        defer { app.shutdown() }
        try configure(app)
        try app.run()
    }
}

// No other calls are required.

When using @main, our directory structure is more concise, and it is more convenient to write multiple executable programs in one Package.

Package.swift

// swift-tools-version:5.6
import PackageDescription

let package = Package(
    name: "test",
    platforms: [
       .macOS(.v12)
    ],
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
    ],
    targets: [
        .executableTarget(
            name: "App",
            dependencies: [
                .product(name: "Vapor", package: "vapor")
            ],
            swiftSettings: [
                // Enable better optimizations when building in Release configuration. Despite the use of
                // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
                // builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
                .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
            ]
        ),
        .executableTarget(
            name: "Worker",
            dependencies: [
                .product(name: "Vapor", package: "vapor")
            ]
        ),
        .testTarget(name: "AppTests", dependencies: [
            .target(name: "App"),
            .product(name: "XCTVapor", package: "vapor"),
        ])
    ]
)

Directory structure:
image

vzsg commented

And if you change the main function a bit, you can even get the main DispatchQueue running, like the code in this comment does.

iWECon commented

resolved #98