Coder Social home page Coder Social logo

Comments (2)

seanmonstar avatar seanmonstar commented on May 4, 2024

This is really cool! (And perhaps somewhat related to #12 and #26).

Something I didn't want to replicate from Akka is the very easy-to-trigger ignored directive since its valid syntax to forget ~. It looks like this doesn't suffer from that.

I haven't built a large app with Akka, but when looking at the examples, it felt like the nested directive style would get very unwieldy with larger and larger apps. Have you noticed that?

from warp.

ayosec avatar ayosec commented on May 4, 2024

Something I didn't want to replicate from Akka is the very easy-to-trigger ignored directive since its valid syntax to forget ~. It looks like this doesn't suffer from that.

Yep. In Scala it is easier to miss the separator because the ; is optional, so you don't get any help from the compiler. The issue is easier to detect with tests, but still a bit unfortunate.

I haven't built a large app with Akka, but when looking at the examples, it felt like the nested directive style would get very unwieldy with larger and larger apps. Have you noticed that?

You can split the route in multiple functions/files, and put part of its logic in small functions.

Thus, the route tree contains only the declarations to “interpret” the request. The code is also much easier to understand and to maintain.

For example:

// Auth.scala

case class Auth(...) {

  def userAuthenticator(creds: Credentials): Future[Option[User]] = ???

  // Directive to authenticate users
  def requireUser = authenticateBasicAsync(realm = "API", userAuthenticator)

}


// ProjecsAPI.scala

case class ProjecsAPI(...) {

  val auth = Auth()

  def listProjects(user: User) = ???

  def findProject(user: User, projectId: Int): Future[Option[Project]] = ???

  def deleteProject(project: Project): Future[Unit] = ???

  // Routes for /projects

  def routes = pathPrefix("projects") {
    auth.requireUser { user =>
      pathEnd {
        complete(listProjects(user))
      } ~
      path(IntNumber) { projectId =>
        onSuccess(findProject(user, projectId)) {
          case None => 
            complete(NotFound)

          case Some(project) =>
            get {
              complete(project)
            } ~
            delete {
              onSuccess(deleteProject(project)) {
                complete(NoContent)
              }
            }
        }
      }
    }
  }

}


// UsersAPI.scala

case class UsersAPI(...) {

  val auth = Auth()

  // Routes for /users

  def routes = pathPrefix("users") {
    auth.requireUser { user =>
      path("current") {
        complete(user)
      }
    }
  }

}


// Main.scala

val routes = (
  ProjecsAPI().routes ~
  UsersAPI().routes
  // ~ More().routes
)
Http().bindAndHandle(routes, "0.0.0.0", 8080)

(Full example)

from warp.

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.