Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- routing {
- static("static") {
- files("css")
- files("js")
- }
- route("/authenticate") {
- authentication {
- formAuthentication("user", "pass", challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })) { credential: UserPasswordCredential ->
- when {
- validLogin(credential.name, credential.password) -> UserIdPrincipal(credential.name)
- else -> null
- }
- }
- }
- handle {
- val principle = call.authentication.principal<UserIdPrincipal>()
- call.sessions.set(LoginSession(principle!!.name))
- call.respondRedirect("/postComment")
- }
- }
- get("/login") {
- call.respondHtml {
- head { addStyles() }
- body {
- navBar(call)
- form(action = "/authenticate", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
- div("form-group") {
- p {
- +"user:"
- textInput(name = "user", classes = "form-control")
- }
- p {
- +"password:"
- passwordInput(name = "pass", classes = "form-control")
- }
- p {
- submitInput(classes = "btn btn-primary") { value = "Login" }
- }
- }
- }
- }
- }
- }
- get("/") {
- call.respondHtml {
- head {
- title { +"Testing Site" }
- addStyles()
- }
- body {
- navBar(call)
- h1 { +"Sample application with HTML builders" }
- widget {
- +"Widget Function"
- }
- }
- }
- }
- get("/comments") {
- call.respondHtml {
- head {
- title { +"Comments" }
- addStyles()
- }
- body {
- navBar(call, 0)
- val comments = findAllComments()
- if (comments.isEmpty()) +"No Comments Found."
- else div("comments") { comments.forEach { commentWidget(it) } }
- }
- }
- }
- get("/postComment") {
- if (!call.isLoggedIn()) call.respondRedirect("/login")
- else {
- call.respondHtml {
- head {
- title { +"Post Comment" }
- addStyles()
- }
- body {
- navBar(call, 1)
- form(action = "/postComment", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
- div("form-group") {
- p {
- div("row") {
- div("col-1") {
- +"Comment:"
- }
- div("col-10") {
- textArea(classes = "form-control") {
- name = "comment"
- }
- }
- }
- }
- p {
- submitInput(classes = "btn btn-primary") { value = "Send" }
- }
- }
- }
- }
- }
- }
- }
- post("/postComment") {
- if (call.isLoggedIn()) {
- val name = call.sessions.get<LoginSession>()!!.name
- val params = call.receive<ValuesMap>()
- val commentText = params["comment"]
- transaction {
- Comments.insert {
- it[user] = name
- it[comment] = commentText!!
- }
- }
- call.respondRedirect("/comments")
- } else call.respondRedirect("/login")
- }
- get("/logout") {
- if (call.isLoggedIn()) {
- call.sessions.clear("SESSION")
- }
- call.respondRedirect("/comments")
- }
- }
Add Comment
Please, Sign In to add comment