Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"swift.sourcekit-lsp.backgroundIndexing": "off"
"swift.sourcekit-lsp.backgroundIndexing": "off",
"[swift]": {
"editor.formatOnSave": false
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// swift-tools-version:5.9
// swift-tools-version:6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Todos",
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
products: [
.executable(name: "App", targets: ["App"]),
.executable(name: "App", targets: ["App"])
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-configuration.git", from: "1.0.0", traits: [.defaults, "CommandLineArguments"]),
],
targets: [
.executableTarget(
name: "App",
.executableTarget(name: "App",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Configuration", package: "swift-configuration"),
.product(name: "Hummingbird", package: "hummingbird"),
]
],
path: "Sources/App"
),
.testTarget(
name: "AppTests",
.testTarget(name: "AppTests",
dependencies: [
.byName(name: "App"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
.product(name: "HummingbirdTesting", package: "hummingbird")
],
path: "Tests/AppTests"
)
]
)
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// swift-tools-version:5.9
// swift-tools-version:6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Todos",
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
products: [
.executable(name: "App", targets: ["App"]),
.executable(name: "App", targets: ["App"])
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-configuration.git", from: "1.0.0", traits: [.defaults, "CommandLineArguments"]),
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.21.0"),
],
targets: [
.executableTarget(
name: "App",
.executableTarget(name: "App",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Configuration", package: "swift-configuration"),
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "PostgresNIO", package: "postgres-nio"),
]
],
path: "Sources/App"
),
.testTarget(
name: "AppTests",
.testTarget(name: "AppTests",
dependencies: [
.byName(name: "App"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
.product(name: "HummingbirdTesting", package: "hummingbird")
],
path: "Tests/AppTests"
)
]
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import Hummingbird
import Logging

/// Application arguments protocol. We use a protocol so we can call
/// `buildApplication` inside Tests as well as in the App executable.
/// Any variables added here also have to be added to `App` in App.swift and
/// `TestArguments` in AppTest.swift
public protocol AppArguments {
var hostname: String { get }
var port: Int { get }
var logLevel: Logger.Level? { get }
}

// Request context used by application
typealias AppRequestContext = BasicRequestContext

/// Build application
/// - Parameter arguments: application arguments
public func buildApplication(_ arguments: some AppArguments) async throws -> some ApplicationProtocol {
/// - Parameter reader: configuration reader
func buildApplication(reader: ConfigReader) async throws -> some ApplicationProtocol {
let logger = {
var logger = Logger(label: "Todos")
logger.logLevel = reader.string(forKey: "log.level", as: Logger.Level.self, default: .info)
return logger
}()
let router = try buildRouter()
let app = Application(
router: router,
configuration: ApplicationConfiguration(reader: reader.scoped(to: "http")),
logger: logger
)
return app
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
/// Build application
/// - Parameter arguments: application arguments
public func buildApplication(_ arguments: some AppArguments) async throws -> some ApplicationProtocol {
let environment = Environment()
/// - Parameter reader: configuration reader
func buildApplication(reader: ConfigReader) async throws -> some ApplicationProtocol {
let logger = {
var logger = Logger(label: "todos-postgres-tutorial")
logger.logLevel =
arguments.logLevel ??
environment.get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } ??
.info
var logger = Logger(label: "Todos")
logger.logLevel = reader.string(forKey: "log.level", as: Logger.Level.self, default: .info)
return logger
}()
if !arguments.inMemoryTesting {
let inMemoryTesting = reader.bool(forKey: "db.inMemoryTesting", default: false)
if !inMemoryTesting {
let client = PostgresClient(
configuration: .init(host: "localhost", username: "todos", password: "todos", database: "hummingbird", tls: .disable),
backgroundLogger: logger
)
}
let router = buildRouter()
var app = Application(
let app = Application(
router: router,
configuration: .init(
address: .hostname(arguments.hostname, port: arguments.port),
serverName: "todos-postgres-tutorial"
),
configuration: ApplicationConfiguration(reader: reader.scoped(to: "http")),
logger: logger
)
return app
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/// Build application
/// - Parameter arguments: application arguments
public func buildApplication(_ arguments: some AppArguments) async throws -> some ApplicationProtocol {
let environment = Environment()
/// - Parameter reader: configuration reader
func buildApplication(reader: ConfigReader) async throws -> some ApplicationProtocol {
let logger = {
var logger = Logger(label: "todos-postgres-tutorial")
logger.logLevel =
arguments.logLevel ??
environment.get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } ??
.info
var logger = Logger(label: "Todos")
logger.logLevel = reader.string(forKey: "log.level", as: Logger.Level.self, default: .info)
return logger
}()
let inMemoryTesting = reader.bool(forKey: "db.inMemoryTesting", default: false)
var postgresClient: PostgresClient?
if !arguments.inMemoryTesting {
if !inMemoryTesting {
let client = PostgresClient(
configuration: .init(host: "localhost", username: "todos", password: "todos", database: "hummingbird", tls: .disable),
backgroundLogger: logger
Expand All @@ -21,10 +18,7 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
let router = buildRouter()
var app = Application(
router: router,
configuration: .init(
address: .hostname(arguments.hostname, port: arguments.port),
serverName: "todos-postgres-tutorial"
),
configuration: ApplicationConfiguration(reader: reader.scoped(to: "http")),
logger: logger
)
if let postgresClient {
Expand All @@ -33,3 +27,4 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
return app
}


Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/// Build application
/// - Parameter arguments: application arguments
public func buildApplication(_ arguments: some AppArguments) async throws -> some ApplicationProtocol {
let environment = Environment()
/// - Parameter reader: configuration reader
func buildApplication(reader: ConfigReader) async throws -> some ApplicationProtocol {
let logger = {
var logger = Logger(label: "todos-postgres-tutorial")
logger.logLevel =
arguments.logLevel ??
environment.get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } ??
.info
var logger = Logger(label: "Todos")
logger.logLevel = reader.string(forKey: "log.level", as: Logger.Level.self, default: .info)
return logger
}()
var postgresRepository: TodoPostgresRepository?
let inMemoryTesting = reader.bool(forKey: "db.inMemoryTesting", default: false)
var postgresClient: PostgresClient?
let router: Router<AppRequestContext>
if !arguments.inMemoryTesting {
if !inMemoryTesting {
let client = PostgresClient(
configuration: .init(host: "localhost", username: "todos", password: "todos", database: "hummingbird", tls: .disable),
backgroundLogger: logger
Expand All @@ -23,12 +20,10 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
} else {
router = buildRouter(TodoMemoryRepository())
}
let router = buildRouter()
var app = Application(
router: router,
configuration: .init(
address: .hostname(arguments.hostname, port: arguments.port),
serverName: "todos-postgres-tutorial"
),
configuration: ApplicationConfiguration(reader: reader.scoped(to: "http")),
logger: logger
)
// if we setup a postgres service then add as a service and run createTable before
Expand All @@ -41,3 +36,5 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
}
return app
}


Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Configuration
import Foundation
import Hummingbird
import HummingbirdTesting
import Logging
import XCTest
import Testing

@testable import App

final class AppTests: XCTestCase {
struct TestArguments: AppArguments {
let hostname = "127.0.0.1"
let port = 8080
let logLevel: Logger.Level? = nil
let inMemoryTesting = true
}

private let reader = ConfigReader(providers: [
InMemoryProvider(values: [
"host": "127.0.0.1",
"port": "0",
"log.level": "trace"
])
])
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Configuration
import Foundation
import Hummingbird
import HummingbirdTesting
import Logging
import XCTest
import Testing

@testable import App

final class AppTests: XCTestCase {
struct TestArguments: AppArguments {
let hostname = "127.0.0.1"
let port = 8080
let logLevel: Logger.Level? = nil
let inMemoryTesting = false
}

private let reader = ConfigReader(providers: [
InMemoryProvider(values: [
"host": "127.0.0.1",
"port": "0",
"log.level": "trace",
"db.inMemoryTesting": false
])
])
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
> ./template/configure.sh Todos
Enter your package name: [Todos] >
Do you want to build an AWS Lambda function? [y/N] >
Enter your executable name: [App] >
Include Visual Studio Code snippets: [Y/n] >
Do you want to use the OpenAPI generator? [y/N] >
Include Visual Studio Code snippets: [y/N] >
Loading