sisioh / trinity

Trinity is a lightweight MVC framework based on Finagle

Version Matrix

Trinity

Join the chat at https://gitter.im/sisioh/trinity

Build Status Maven Central Scaladoc Reference Status

Trinity is a lightweight MVC framework based on Finagle, written in Scala.
Since May 2013

Concepts

  • Provide MVC functions not supported by Finagle.
  • Support Domain Driven-Design by non CoC(Convention over Configuration).

Features

  • You can use Actions as Controller, instead of Finagle Service.
    • URLs can be mapped to action methods, like Scalatra.
    • Otherwise, The Routing information can be outside of the Controller, like Play2.
  • You can use Template Engine (e.g. Scalatra) with Trinity.

Functions

Supported Functions

  • Routing request to action. return's type is scala.concurrent.Future
  • Finagle's Request/Response Enhance
    • Multi-part file upload
    • JSON format reponse
    • File resouce support
  • Binding to Template Engine
  • Testing
    • Unit testing
    • Integration testing
  • JRebel support

Unsupported Functions

  • Form
  • Validation
  • Persistence(e.g. RDBMS/NoSQL)

License

Apache License, Version 2.0

Setup

Build Configuration

Add the following to your sbt build (Scala 2.10.x, and Scala 2.11.x):

Release Version

resolvers += "Sonatype Release Repository" at "http://oss.sonatype.org/content/repositories/releases/"

libraryDependencies += "org.sisioh" %% "trinity" % "1.0.12"

Snapshot Version

resolvers += "Sonatype Snapshot Repository" at "http://oss.sonatype.org/content/repositories/snapshots/"

libraryDependencies += "org.sisioh" %% "trinity" % "1.0.13-SNAPSHOT"

Controller code

Scalatra like controller

object ScalatraLikeControllerApplication
  extends ConsoleApplication {

  get("/hello") {
    request =>
      responseBuilder.withTextPlain("Hello World!!").toFuture
  }

  get("/json") {
    request =>
      val jValue = JObject(
        JField("name", JString("value"))
      )
      responseBuilder.withJson(jValue).toFuture
  }

  get("/user/:userId") {
    request =>
      responseBuilder.withTextPlain("userId = " + request.routeParams("userId")).toFuture
  }

  get( """/group/(.*)""".r, Seq("name")) {
    request =>
      ResponseBuilder().withTextPlain("name = " + request.routeParams("name")).toFuture
  }

  startWithAwait()

}

Play2 like controller

object PlayLikeApplicationForController extends ConsoleApplication {

  case class MainController() extends ControllerSupport {

    def helloWorld = SimpleAction {
      request =>
        responseBuilder.withTextPlain("Hello World!!").toFuture
    }

    def getUser = SimpleAction {
      request =>
        responseBuilder.withTextPlain("userId = " + request.routeParams("userId")).toFuture
    }

    def getGroup(name: String) = SimpleAction {
      request =>
        responseBuilder.withTextPlain("name = " + name).toFuture
    }
  }

  val mainController = MainController()

  override protected val routingFilter = Some(RoutingFilter.createForActions {
    implicit pathPatternParser =>
      Seq(
        Get % "/hello" -> mainController.helloWorld,
        Get % "/user/:userId" -> mainController.getUser,
        Get % ("""/group/(.*)""".r -> Seq("name")) -> {
          request =>
            mainController.getGroup(request.routeParams("name"))(request)
        }
      )
  })

  startWithAwait()

}

Build

$ sbt clean compile

Run

$ sbt run

Test

$ curl -X GET http://localhost:7070/hello
Hello!