/example/src/main/scala/com/softwaremill/example/session/VariousSessionsScala.scala

https://github.com/softwaremill/akka-http-session · Scala · 106 lines · 90 code · 12 blank · 4 comment · 0 complexity · b304c8127d3a5587b68872a61ed3b734 MD5 · raw file

  1. package com.softwaremill.example.session
  2. import akka.actor.ActorSystem
  3. import akka.http.scaladsl.Http
  4. import akka.http.scaladsl.server.Directives._
  5. import akka.stream.ActorMaterializer
  6. import com.softwaremill.session.SessionDirectives._
  7. import com.softwaremill.session.SessionOptions._
  8. import com.softwaremill.session.SessionResult._
  9. import com.softwaremill.session._
  10. import com.typesafe.scalalogging.StrictLogging
  11. import scala.io.StdIn
  12. object VariousSessionsScala extends App with StrictLogging {
  13. implicit val system = ActorSystem("example")
  14. implicit val materializer = ActorMaterializer()
  15. import system.dispatcher
  16. val sessionConfig = SessionConfig.default(
  17. "c05ll3lesrinf39t7mc5h6un6r0c69lgfno69dsak3vabeqamouq4328cuaekros401ajdpkh60rrtpd8ro24rbuqmgtnd1ebag6ljnb65i8a55d482ok7o0nch0bfbe")
  18. implicit val sessionManager = new SessionManager[MyScalaSession](sessionConfig)
  19. implicit val refreshTokenStorage = new InMemoryRefreshTokenStorage[MyScalaSession] {
  20. def log(msg: String) = logger.info(msg)
  21. }
  22. def mySetSession(v: MyScalaSession) = setSession(refreshable, usingCookies, v)
  23. val myRequiredSession = requiredSession(refreshable, usingCookies)
  24. val myInvalidateSession = invalidateSession(refreshable, usingCookies)
  25. val routes =
  26. path("secret") {
  27. get {
  28. // type: Long, or whatever the T parameter is
  29. requiredSession(oneOff, usingCookies) { session =>
  30. complete {
  31. "treasure"
  32. }
  33. }
  34. }
  35. } ~
  36. path("open") {
  37. get {
  38. // type: Option[Long] (Option[T])
  39. optionalSession(oneOff, usingCookies) { session =>
  40. complete {
  41. "small treasure"
  42. }
  43. }
  44. }
  45. } ~
  46. path("detail") {
  47. get {
  48. // type: SessionResult[Long] (SessionResult[T])
  49. // which can be: Decoded, DecodedLegacy, CreatedFromToken, Expired, Corrupt, NoSession
  50. session(oneOff, usingCookies) { sessionResult =>
  51. sessionResult match {
  52. case Decoded(session) =>
  53. complete {
  54. "decoded"
  55. }
  56. case DecodedLegacy(session) =>
  57. complete {
  58. "decoded legacy"
  59. }
  60. case CreatedFromToken(session) =>
  61. complete {
  62. "created from token"
  63. }
  64. case NoSession =>
  65. complete {
  66. "no session"
  67. }
  68. case TokenNotFound =>
  69. complete {
  70. "token not found"
  71. }
  72. case Expired =>
  73. complete {
  74. "expired"
  75. }
  76. case Corrupt(exc) =>
  77. complete {
  78. "corrupt"
  79. }
  80. }
  81. }
  82. }
  83. }
  84. val bindingFuture = Http().bindAndHandle(routes, "localhost", 8080)
  85. println("Server started, press enter to stop. Visit http://localhost:8080 to see the demo.")
  86. StdIn.readLine()
  87. import system.dispatcher
  88. bindingFuture
  89. .flatMap(_.unbind())
  90. .onComplete { _ =>
  91. system.terminate()
  92. println("Server stopped")
  93. }
  94. }