Guest User

Untitled

a guest
Jan 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. routing {
  2. static("static") {
  3. files("css")
  4. files("js")
  5. }
  6.  
  7. route("/authenticate") {
  8. authentication {
  9. formAuthentication("user", "pass", challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })) { credential: UserPasswordCredential ->
  10. when {
  11. validLogin(credential.name, credential.password) -> UserIdPrincipal(credential.name)
  12. else -> null
  13. }
  14. }
  15. }
  16.  
  17. handle {
  18. val principle = call.authentication.principal<UserIdPrincipal>()
  19. call.sessions.set(LoginSession(principle!!.name))
  20. call.respondRedirect("/postComment")
  21. }
  22. }
  23.  
  24. get("/login") {
  25. call.respondHtml {
  26. head { addStyles() }
  27. body {
  28. navBar(call)
  29. form(action = "/authenticate", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
  30. div("form-group") {
  31. p {
  32. +"user:"
  33. textInput(name = "user", classes = "form-control")
  34. }
  35.  
  36. p {
  37. +"password:"
  38. passwordInput(name = "pass", classes = "form-control")
  39. }
  40.  
  41. p {
  42. submitInput(classes = "btn btn-primary") { value = "Login" }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. get("/") {
  51. call.respondHtml {
  52. head {
  53. title { +"Testing Site" }
  54. addStyles()
  55. }
  56. body {
  57. navBar(call)
  58. h1 { +"Sample application with HTML builders" }
  59. widget {
  60. +"Widget Function"
  61. }
  62. }
  63. }
  64. }
  65.  
  66. get("/comments") {
  67. call.respondHtml {
  68. head {
  69. title { +"Comments" }
  70. addStyles()
  71. }
  72. body {
  73. navBar(call, 0)
  74. val comments = findAllComments()
  75. if (comments.isEmpty()) +"No Comments Found."
  76. else div("comments") { comments.forEach { commentWidget(it) } }
  77. }
  78. }
  79. }
  80.  
  81. get("/postComment") {
  82. if (!call.isLoggedIn()) call.respondRedirect("/login")
  83. else {
  84. call.respondHtml {
  85. head {
  86. title { +"Post Comment" }
  87. addStyles()
  88. }
  89.  
  90. body {
  91. navBar(call, 1)
  92. form(action = "/postComment", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
  93. div("form-group") {
  94. p {
  95. div("row") {
  96. div("col-1") {
  97. +"Comment:"
  98. }
  99. div("col-10") {
  100. textArea(classes = "form-control") {
  101. name = "comment"
  102. }
  103. }
  104. }
  105. }
  106.  
  107. p {
  108. submitInput(classes = "btn btn-primary") { value = "Send" }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116.  
  117. post("/postComment") {
  118. if (call.isLoggedIn()) {
  119. val name = call.sessions.get<LoginSession>()!!.name
  120. val params = call.receive<ValuesMap>()
  121.  
  122. val commentText = params["comment"]
  123.  
  124. transaction {
  125. Comments.insert {
  126. it[user] = name
  127. it[comment] = commentText!!
  128. }
  129. }
  130.  
  131. call.respondRedirect("/comments")
  132. } else call.respondRedirect("/login")
  133. }
  134.  
  135. get("/logout") {
  136. if (call.isLoggedIn()) {
  137. call.sessions.clear("SESSION")
  138. }
  139. call.respondRedirect("/comments")
  140. }
  141. }
Add Comment
Please, Sign In to add comment