Giter Club home page Giter Club logo

Comments (5)

davedelong avatar davedelong commented on May 24, 2024

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.

adam-fowler avatar adam-fowler commented on May 24, 2024

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.

adam-fowler avatar adam-fowler commented on May 24, 2024

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.

adam-fowler avatar adam-fowler commented on May 24, 2024

#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.

adam-fowler avatar adam-fowler commented on May 24, 2024

Closing as #421 is now out.

from hummingbird.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.