-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update s3-upload example for HB 2.0 (#75)
* Update S3 upload for Hummingbird 2.0 * Update README * Update upload-s3/Package.swift Co-authored-by: Joannis Orlandos <[email protected]> * Updates from review --------- Co-authored-by: Joannis Orlandos <[email protected]>
- Loading branch information
1 parent
9eeceaf
commit 8689ee8
Showing
8 changed files
with
163 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
import ArgumentParser | ||
import Hummingbird | ||
import Logging | ||
|
||
@main | ||
struct App: ParsableCommand, AppArguments { | ||
struct App: AsyncParsableCommand, AppArguments { | ||
@Option(name: .shortAndLong) | ||
var hostname: String = "127.0.0.1" | ||
|
||
@Option(name: .shortAndLong) | ||
var port: Int = 8080 | ||
|
||
func run() throws { | ||
let app = HBApplication( | ||
configuration: .init( | ||
address: .hostname(self.hostname, port: self.port), | ||
serverName: "Hummingbird" | ||
) | ||
) | ||
try app.configure(self) | ||
try app.start() | ||
app.wait() | ||
@Option(name: .shortAndLong) | ||
var logLevel: Logger.Level? | ||
|
||
func run() async throws { | ||
let app = buildApplication(self) | ||
try await app.runService() | ||
} | ||
} | ||
|
||
/// Extend `Logger.Level` so it can be used as an argument | ||
#if compiler(>=6.0) | ||
extension Logger.Level: @retroactive ExpressibleByArgument {} | ||
#else | ||
extension Logger.Level: ExpressibleByArgument {} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
import Hummingbird | ||
import Logging | ||
import ServiceLifecycle | ||
import SotoS3 | ||
|
||
/// Application arguments protocol. We use a protocol so we can call | ||
/// `HBApplication.configure` 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 } | ||
} | ||
|
||
struct AWSClientService: Service { | ||
let client: AWSClient | ||
|
||
func run() async throws { | ||
// Ignore cancellation error | ||
try? await gracefulShutdown() | ||
try await self.client.shutdown() | ||
} | ||
} | ||
|
||
func buildApplication(_ args: some AppArguments) -> some ApplicationProtocol { | ||
let logger = { | ||
var logger = Logger(label: "html-form") | ||
logger.logLevel = args.logLevel ?? .info | ||
return logger | ||
}() | ||
let env = Environment() | ||
guard let bucket = env.get("s3_upload_bucket") else { | ||
preconditionFailure("Requires \"s3_upload_bucket\" environment variable") | ||
} | ||
|
||
let awsClient = AWSClient() | ||
let s3 = S3(client: awsClient, region: .euwest1) | ||
|
||
let router = Router() | ||
router.middlewares.add(LogRequestsMiddleware(.info)) | ||
|
||
router.addRoutes( | ||
S3FileController( | ||
s3: s3, | ||
bucket: bucket, | ||
folder: env.get("s3_upload_folder") ?? "hb-upload-s3" | ||
).getRoutes(), | ||
atPath: "files" | ||
) | ||
router.get("/health") { request, context in | ||
return HTTPResponse.Status.ok | ||
} | ||
var app = Application( | ||
router: router, | ||
configuration: .init(address: .hostname(args.hostname, port: args.port)), | ||
logger: logger | ||
) | ||
app.addServices(AWSClientService(client: awsClient)) | ||
return app | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.