Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
petrpavlik committed Oct 1, 2024
1 parent 92c90d1 commit 3b7008e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "ccf44bad52148231675746a56d946f4944d1d8d5acc1692883c6ef52f4e834a6",
"originHash" : "e5b1417db34b184e39104553b14beb06f1ef3f093d6ce0a51281f9af210b8286",
"pins" : [
{
"identity" : "async-http-client",
Expand All @@ -24,8 +24,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/IndiePitcher/indiepitcher-swift.git",
"state" : {
"revision" : "26417a2af06f7c7ed24e65c3d69934d678c9c193",
"version" : "1.0.1"
"revision" : "6ffaacb99800a448653d7537d6e57cf27c70525c",
"version" : "1.1.0"
}
},
{
Expand Down
22 changes: 16 additions & 6 deletions Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hummingbird
import IndiePitcherSwift
import Logging

/// Application arguments protocol. We use a protocol so we can call
Expand All @@ -19,7 +20,8 @@ typealias AppRequestContext = BasicRequestContext
public func buildApplication(_ arguments: some AppArguments) async throws
-> some ApplicationProtocol
{
let environment = Environment()
let environment = try await Environment().merging(with: .dotEnv())

let logger = {
var logger = Logger(label: "HummingbirdExample")
logger.logLevel =
Expand All @@ -28,7 +30,14 @@ public func buildApplication(_ arguments: some AppArguments) async throws
} ?? .info
return logger
}()
let router = buildRouter()

guard let apiKey = environment.get("INDIEPITCHER_SECRET_KEY") else {
preconditionFailure("Requires \"INDIEPITCHER_SECRET_KEY\" environment variable")
}

let indiePitcher = IndiePitcher(client: .shared, apiKey: apiKey)

let router = buildRouter(indiePitcher: indiePitcher)
let app = Application(
router: router,
configuration: .init(
Expand All @@ -41,13 +50,14 @@ public func buildApplication(_ arguments: some AppArguments) async throws
}

/// Build router
func buildRouter() -> Router<AppRequestContext> {
func buildRouter(indiePitcher: IndiePitcher) -> Router<AppRequestContext> {
let router = Router(context: AppRequestContext.self)
// Add middleware
router.addMiddleware {
// logging middleware
LogRequestsMiddleware(.info)
}

// Add default endpoint
router.get("/") { _, _ in
return "Hello! Trigger an email by visiting /send_email endpoint!"
Expand All @@ -56,10 +66,10 @@ func buildRouter() -> Router<AppRequestContext> {
router.get("/send_email") { _, context in

let emailBody = """
This is an email sent from a **hummingbird 2 backend**!
"""
This is an email sent from a **hummingbird 2 backend**!
"""

try await context.indiePitcher.sendEmail(
try await indiePitcher.sendEmail(
data: .init(
to: "[email protected]",
subject: "This is an email sent from a hummingbird 2 backend", body: emailBody,
Expand Down
22 changes: 0 additions & 22 deletions Sources/App/IndiePitcher.swift

This file was deleted.

0 comments on commit 3b7008e

Please sign in to comment.