-
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.
Make own application type TodosApp, add tests
- Loading branch information
1 parent
9df82dc
commit ef116d4
Showing
6 changed files
with
180 additions
and
95 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Hummingbird | ||
import HummingbirdFoundation | ||
import NIOCore | ||
import NIOPosix | ||
import ServiceLifecycle | ||
import SotoDynamoDB | ||
|
||
struct TodosApp: HBApplicationProtocol { | ||
/// Request context which default to using JSONDecoder/Encoder | ||
struct Context: HBRequestContext { | ||
init(eventLoop: EventLoop, allocator: ByteBufferAllocator, logger: Logger) { | ||
self.coreContext = .init( | ||
requestDecoder: JSONDecoder(), | ||
responseEncoder: JSONEncoder(), | ||
eventLoop: eventLoop, | ||
allocator: allocator, | ||
logger: logger | ||
) | ||
} | ||
|
||
var coreContext: HBCoreRequestContext | ||
} | ||
|
||
init(configuration: HBApplicationConfiguration, eventLoopGroupProvider: EventLoopGroupProvider = .singleton) { | ||
self.configuration = configuration | ||
self.eventLoopGroup = eventLoopGroupProvider.eventLoopGroup | ||
self.awsClient = AWSClient(httpClientProvider: .createNewWithEventLoopGroup(self.eventLoopGroup)) | ||
} | ||
|
||
var responder: some HBResponder<Context> { | ||
let router = HBRouter(context: Context.self) | ||
// middleware | ||
router.middlewares.add(HBLogRequestsMiddleware(.debug)) | ||
router.middlewares.add(HBCORSMiddleware( | ||
allowOrigin: .originBased, | ||
allowHeaders: [.contentType], | ||
allowMethods: [.get, .options, .post, .delete, .patch] | ||
)) | ||
router.get("/") { _, _ in | ||
return "Hello" | ||
} | ||
|
||
let dynamoDB = DynamoDB(client: awsClient, region: .euwest1) | ||
|
||
let todoController = TodoController(dynamoDB: dynamoDB) | ||
todoController.addRoutes(to: router.group("todos")) | ||
|
||
return router.buildResponder() | ||
} | ||
|
||
let awsClient: AWSClient | ||
let configuration: HBApplicationConfiguration | ||
let eventLoopGroup: EventLoopGroup | ||
var services: [any Service] { [self.awsClient] } | ||
} |
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