Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.76 KB

readme.md

File metadata and controls

54 lines (41 loc) · 1.76 KB

Playless

Playless extract the injector from play framework to make it independent of play's netty server or akka http server, which makes you harness the power of reusing code intended to play framework while you are using a new server backend which can be anything.

To enable playless, you should add it to your library dependencies (remember it's not published to any repositories, you should publish it in your local or own nexus or other):

libraryDependencies += "io.growing" %% "playless" % "1.0"

And then, all you have to do is just a piece of code as below, which I take akka http and play slick for an example:

object Main extends App with CatJsonSupport {

  val injector = Playless.startPlayAndGetInjector(args)
  implicit val system = injector.instanceOf[ActorSystem]
  implicit val materializer = ActorMaterializer()

  import system.dispatcher

  val catDao = injector.instanceOf[CatDAO]

  val defaultExceptionHandler = ExceptionHandler {
    case t: Throwable =>
      extractLog { logger =>
        logger.error(t, "error happened!")
        complete(HttpResponse(InternalServerError, entity = t.getMessage))
      }
  }

  val route = handleExceptions(defaultExceptionHandler){
    path("cats") {
      get {
        complete(catDao.all())
      } ~ post {
        entity(as[Cat]) { cat =>
          complete(catDao.insert(cat))
        }
      }
    }
  }

  val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)

  println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
  StdIn.readLine() // let it run until user presses return
  bindingFuture
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => Play.current.stop()) // and shutdown when done

}

See the complete code in sample project!