Skip to content

Instantly share code, notes, and snippets.

@globulon
Created December 3, 2018 09:48
Show Gist options
  • Save globulon/e69502defe53e3bfa54e35eb900618a1 to your computer and use it in GitHub Desktop.
Save globulon/e69502defe53e3bfa54e35eb900618a1 to your computer and use it in GitHub Desktop.
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import com.graboids.alpha.service.users.app.Application
import com.graboids.alpha.service.users.config.resolveConfig
import com.graboids.config.hocon._
import com.graboids.fp._
import scalaz.NonEmptyList._
import scalaz.ioeffect.console._
import scalaz.ioeffect.{IO, SafeApp, Void}
import scala.concurrent.Future
object Users extends Routes with Environment with SafeApp {
def run(args: List[String]): IO[Void, ExitStatus] =
process.attempt.map(_.fold(_ => 1, _ => 0)).map {
case code if code > 0 ⇒ ExitStatus.ExitNow(code)
case _ ⇒ ExitStatus.DoNotExit
}
private def process: IO[AppErrs, Unit] = for {
_ ← putStrLn(line = "Starting Server").leftMap[AppErrs](ex ⇒ nels(ThrownError(ex.getMessage)))
app ← resolveApplication.io
_ ← startHttp(app)
_ ← putStrLn(line = "Server Started on port 8080").leftMap[AppErrs](ex ⇒ nels(ThrownError(ex.getMessage)))
} yield ()
private def startHttp[A](app: Application): IO[AppErrs, Future[ServerBinding]] =
IO.sync(Http().bindAndHandle(routes(app), "0.0.0.0", 8080))
private def resolveApplication: Safe[Application] = (resolveConfig map Application) =<< fromEnv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment