Comments (5)
I'm not entirely sure what the shape of this should be… I've been playing around with prototyping this myself and haven't found something I'm happy with. I just know that I'm not happy with having to list everything out. 😅
from hummingbird.
Up until now I've always done it as follows
let router = Router()
UserController().addRoutes(to: router.group("user"))
struct UserController {
func addRoutes(to group: RouterGroup<some BaseRequestContext>) {
group.post("signup") { ... }
group.post("login") { ... }
}
}
But I can see how people might prefer to create a RouteCollection they can pass around.
let router = Router()
router.add("user", UserController().routes)
struct UserController<Context: BaseRequestContext> {
var routes: RouteCollection<Context> {
let routes = RouteCollection<Context>
routes.post("signup") { ... }
routes.post("login") { ... }
return routes
}
}
from hummingbird.
Interestingly the experimental result based Router in HummingbirdRouter allows you to do this now. This is from the webauthn example in hummingbird-examples
let router = RouterBuilder(context: WebAuthnRequestContext.self) {
// add logging middleware
LogRequestsMiddleware(.info)
// add file middleware to server HTML files
FileMiddleware(searchForIndexHtml: true, logger: logger)
// health check endpoint
Get("/health") { _, _ -> HTTPResponse.Status in
return .ok
}
HTMLController(
mustacheLibrary: library,
fluent: fluent,
sessionStorage: sessionStorage
).endpoints
RouteGroup("api") {
HBWebAuthnController(
webauthn: .init(
config: .init(
relyingPartyID: "localhost",
relyingPartyName: "Hummingbird WebAuthn example",
relyingPartyOrigin: "http://localhost:8080"
)
),
fluent: fluent,
sessionStorage: sessionStorage
).endpoints
}
}
Notice I add the HTMLController and HBWebAuthnController endpoints directly. You can add anything that conforms to RouterMiddleware
,
from hummingbird.
#421 Solves this to some degree in that it provides an object you can pass around that holds a collection of router endpoints.
If you want to do what you have above you could add a protocol
protocol RouteCollectionBuilder {
associatedtype Context: BaseRequestContext
var routes: RouteCollection { get }
}
And extend RouterMethods
which in effect extends Router
and RouterGroup
extension RouterMethods {
public func add<Collection: RouteCollectionBuilder>(_ path: String = "", routes collection: Collection) where Collection.Context == RouterMethods.Context {
add(path, routes: collection.routes)
}
}
Then you have
struct PublicEndpoints: RouteCollectionBuilder {
var routes: RouteCollection {
let routes = RouteCollection()
routes.get { ... }
routes.put("path") { ... }
}
}
router.add(PublicEndpoints())
Then of course you can hide a load of this behind macros.
from hummingbird.
Closing as #421 is now out.
from hummingbird.
Related Issues (20)
- 2.x.x - Make XCT test setups more configurable
- Can `hb_id` always be unique? HOT 7
- 2.x.x - How to disallow `HBResponseBodyWriter` from being used outside `HBResponseBody.write`.
- 2.x.x - Bikeshedding HOT 2
- 2.x.x - Add timeout support to HTTP2 channel handler
- 2.x.x - Alpha release todo list
- HBAsyncMiddleware precondition fail HOT 7
- Server crashes when accessing body in middleware HOT 5
- 2.x.x - Investigate performance of nonisolated HBServer.serve function HOT 1
- Allow disabling `hb_id` HOT 6
- Consideration: Use `AsyncSequence<ByteBuffer>` to represent a request body HOT 1
- Prevent users from iterating a request's body after having responded to that request HOT 3
- Fix Tests when running CI in environment with locked KeyChain HOT 1
- 1.12.0 Does not contain all commits from branch 1.1.x HOT 2
- Hook to customize creation of Request Context HOT 2
- Investigate HTTP2 connection manager from gRPC
- Investigate confusing router API HOT 1
- Streaming issues HOT 2
- Missing CORS headers when request fails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hummingbird.