feat: Adds browser-sync / hot reload

This commit is contained in:
2025-01-09 08:15:48 -05:00
parent e414afd95b
commit da5fec4a94
4 changed files with 49 additions and 22 deletions

View File

@@ -0,0 +1,17 @@
import Foundation
import Vapor
#if DEBUG
struct BrowserSyncHandler: LifecycleHandler {
func didBoot(_ application: Application) throws {
let process = Process()
process.executableURL = URL(filePath: "/bin/sh")
process.arguments = ["-c", "browser-sync reload"]
do {
try process.run()
} catch {
print("Could not auto-reload: \(error)")
}
}
}
#endif

View File

@@ -11,6 +11,10 @@ public func configure(_ app: Application) async throws {
app.middleware.use(app.sessions.middleware) app.middleware.use(app.sessions.middleware)
app.middleware.use(User.sessionAuthenticator()) app.middleware.use(User.sessionAuthenticator())
#if DEBUG
app.lifecycle.use(BrowserSyncHandler())
#endif
switch app.environment { switch app.environment {
case .production, .development: case .production, .development:
app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite) app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite)

View File

@@ -1,31 +1,31 @@
import Vapor
import Logging import Logging
import NIOCore import NIOCore
import NIOPosix import NIOPosix
import Vapor
@main @main
enum Entrypoint { enum Entrypoint {
static func main() async throws { static func main() async throws {
var env = try Environment.detect() var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env) try LoggingSystem.bootstrap(from: &env)
let app = try await Application.make(env)
// This attempts to install NIO as the Swift Concurrency global executor. let app = try await Application.make(env)
// You can enable it if you'd like to reduce the amount of context switching between NIO and Swift Concurrency.
// Note: this has caused issues with some libraries that use `.wait()` and cleanly shutting down. // This attempts to install NIO as the Swift Concurrency global executor.
// If enabled, you should be careful about calling async functions before this point as it can cause assertion failures. // You can enable it if you'd like to reduce the amount of context switching between NIO and Swift Concurrency.
// let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor() // Note: this has caused issues with some libraries that use `.wait()` and cleanly shutting down.
// app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)]) // If enabled, you should be careful about calling async functions before this point as it can cause assertion failures.
// let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
do { // app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)])
try await configure(app)
} catch { do {
app.logger.report(error: error) try await configure(app)
try? await app.asyncShutdown() } catch {
throw error app.logger.report(error: error)
} try? await app.asyncShutdown()
try await app.execute() throw error
try await app.asyncShutdown()
} }
try await app.execute()
try await app.asyncShutdown()
}
} }

6
swift-dev Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env zsh
touch .build/browser-dev-sync
browser-sync start -p localhost:8080 --ws &
watchexec -w Sources -e .swift -r 'swift build --product App && touch .build/browser-dev-sync' &
watchexec -w .build/browser-dev-sync --ignore-nothing -r '.build/debug/App'