整理專案目錄並加上ResponseExtension
This commit is contained in:
parent
1d6cca58c2
commit
0516a20351
@ -1,7 +1,6 @@
|
|||||||
package com.ray650128
|
package com.ray650128
|
||||||
|
|
||||||
import com.ray650128.dto.UserDto
|
import com.ray650128.model.dto.UserDto
|
||||||
import com.ray650128.model.User
|
|
||||||
import com.ray650128.plugins.configureRouting
|
import com.ray650128.plugins.configureRouting
|
||||||
import com.ray650128.plugins.configureSerialization
|
import com.ray650128.plugins.configureSerialization
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package com.ray650128
|
|||||||
import com.auth0.jwt.JWT
|
import com.auth0.jwt.JWT
|
||||||
import com.auth0.jwt.JWTVerifier
|
import com.auth0.jwt.JWTVerifier
|
||||||
import com.auth0.jwt.algorithms.Algorithm
|
import com.auth0.jwt.algorithms.Algorithm
|
||||||
import com.ray650128.dto.UserDto
|
import com.ray650128.model.dto.UserDto
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object JwtConfig {
|
object JwtConfig {
|
||||||
|
|||||||
42
src/main/kotlin/com/ray650128/extension/ResponseExtension.kt
Normal file
42
src/main/kotlin/com/ray650128/extension/ResponseExtension.kt
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package com.ray650128.extension
|
||||||
|
|
||||||
|
import com.ray650128.model.ErrorResponse
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
|
||||||
|
|
||||||
|
suspend fun <T> ApplicationCall.sendCreated(data: T?) {
|
||||||
|
this.respond(
|
||||||
|
status = HttpStatusCode.Created,
|
||||||
|
message = data ?: mapOf("message" to "success.")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T : Any> ApplicationCall.sendSuccess(data: T?) {
|
||||||
|
this.respond(
|
||||||
|
status = HttpStatusCode.OK,
|
||||||
|
message = data ?: mapOf("message" to "success.")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun ApplicationCall.sendUnauthorized(errorResponse: ErrorResponse) {
|
||||||
|
this.respond(
|
||||||
|
status = HttpStatusCode.Unauthorized,
|
||||||
|
message = errorResponse
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun ApplicationCall.sendBadRequest(errorResponse: ErrorResponse) {
|
||||||
|
this.respond(
|
||||||
|
status = HttpStatusCode.BadRequest,
|
||||||
|
message = errorResponse
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun ApplicationCall.sendNotFound(errorResponse: ErrorResponse) {
|
||||||
|
this.respond(
|
||||||
|
status = HttpStatusCode.NotFound,
|
||||||
|
message = errorResponse
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package com.ray650128.extension
|
package com.ray650128.extension
|
||||||
|
|
||||||
import com.ray650128.dto.UserDto
|
import com.ray650128.model.dto.UserDto
|
||||||
import com.ray650128.model.User
|
import com.ray650128.model.pojo.User
|
||||||
|
|
||||||
fun User.toDto(): UserDto =
|
fun User.toDto(): UserDto =
|
||||||
UserDto(
|
UserDto(
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.dto
|
package com.ray650128.model.dto
|
||||||
|
|
||||||
import io.ktor.server.auth.*
|
import io.ktor.server.auth.*
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.model
|
package com.ray650128.model.pojo
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.model
|
package com.ray650128.model.pojo
|
||||||
|
|
||||||
import org.bson.codecs.pojo.annotations.BsonId
|
import org.bson.codecs.pojo.annotations.BsonId
|
||||||
import org.litote.kmongo.Id
|
import org.litote.kmongo.Id
|
||||||
@ -1,12 +1,11 @@
|
|||||||
package com.ray650128.plugins
|
package com.ray650128.plugins
|
||||||
|
|
||||||
import com.ray650128.JwtConfig
|
import com.ray650128.JwtConfig
|
||||||
import com.ray650128.dto.UserDto
|
import com.ray650128.extension.*
|
||||||
import com.ray650128.extension.toDto
|
import com.ray650128.model.dto.UserDto
|
||||||
import com.ray650128.extension.toUser
|
|
||||||
import com.ray650128.model.ErrorResponse
|
import com.ray650128.model.ErrorResponse
|
||||||
import com.ray650128.model.LoginResult
|
import com.ray650128.model.pojo.LoginResult
|
||||||
import com.ray650128.model.User
|
import com.ray650128.model.pojo.User
|
||||||
import com.ray650128.service.UserService
|
import com.ray650128.service.UserService
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
@ -19,24 +18,23 @@ fun Application.configureRouting() {
|
|||||||
val service = UserService()
|
val service = UserService()
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/") {
|
|
||||||
call.respondText("Hello World!")
|
|
||||||
}
|
|
||||||
|
|
||||||
route("/api") {
|
route("/api") {
|
||||||
route("/v1") {
|
route("/v1") {
|
||||||
post("/register") {
|
post("/register") {
|
||||||
val request = call.receive<UserDto>()
|
val request = call.receive<UserDto>()
|
||||||
|
if (service.findByAccount(request.account) != null) {
|
||||||
|
call.sendBadRequest(ErrorResponse("User has existed."))
|
||||||
|
return@post
|
||||||
|
}
|
||||||
val newToken = JwtConfig.generateToken(request)
|
val newToken = JwtConfig.generateToken(request)
|
||||||
val user = request.toUser().apply {
|
val user = request.toUser().apply {
|
||||||
token = newToken
|
token = newToken
|
||||||
createAt = System.currentTimeMillis()
|
createAt = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
service.create(user)
|
service.create(user)?.let { userId ->
|
||||||
?.let { userId ->
|
|
||||||
call.response.headers.append("My-User-Id-Header", userId.toString())
|
call.response.headers.append("My-User-Id-Header", userId.toString())
|
||||||
call.respond(HttpStatusCode.Created, LoginResult(request.account, newToken))
|
call.sendCreated(LoginResult(request.account, newToken))
|
||||||
} ?: call.respond(HttpStatusCode.BadRequest, ErrorResponse.BAD_REQUEST_RESPONSE)
|
} ?: call.sendBadRequest(ErrorResponse.BAD_REQUEST_RESPONSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
post("/login") {
|
post("/login") {
|
||||||
@ -50,29 +48,20 @@ fun Application.configureRouting() {
|
|||||||
user.updatedAt = System.currentTimeMillis()
|
user.updatedAt = System.currentTimeMillis()
|
||||||
service.updateById(user.id.toString(), user)
|
service.updateById(user.id.toString(), user)
|
||||||
}
|
}
|
||||||
call.respond(HttpStatusCode.OK, LoginResult(request.account, token!!))
|
call.sendSuccess(LoginResult(request.account, token!!))
|
||||||
} else {
|
} else {
|
||||||
call.respond(
|
call.sendUnauthorized(ErrorResponse("Account or Password wrong."))
|
||||||
status = HttpStatusCode.Unauthorized,
|
|
||||||
message = mapOf("message" to "Account or Password wrong.")
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate {
|
authenticate {
|
||||||
post("/logout") {
|
post("/logout") {
|
||||||
val account = call.authentication.principal<UserDto>()?.account ?: run {
|
val account = call.authentication.principal<UserDto>()?.account ?: run {
|
||||||
call.respond(
|
call.sendUnauthorized(ErrorResponse("Unauthorized"))
|
||||||
status = HttpStatusCode.Unauthorized,
|
|
||||||
message = mapOf("message" to "token wrong")
|
|
||||||
)
|
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
val user = service.findByAccount(account) ?: run {
|
val user = service.findByAccount(account) ?: run {
|
||||||
call.respond(
|
call.sendUnauthorized(ErrorResponse("Unauthorized"))
|
||||||
status = HttpStatusCode.Unauthorized,
|
|
||||||
message = mapOf("message" to "token wrong")
|
|
||||||
)
|
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
user.apply {
|
user.apply {
|
||||||
@ -80,7 +69,7 @@ fun Application.configureRouting() {
|
|||||||
updatedAt = System.currentTimeMillis()
|
updatedAt = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
service.updateById(user.id.toString(), user)
|
service.updateById(user.id.toString(), user)
|
||||||
call.respond(HttpStatusCode.OK, "User has logged out")
|
call.sendSuccess(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +83,7 @@ fun Application.configureRouting() {
|
|||||||
val id = call.parameters["id"].toString()
|
val id = call.parameters["id"].toString()
|
||||||
service.findById(id)
|
service.findById(id)
|
||||||
?.let { foundPerson -> call.respond(foundPerson.toDto()) }
|
?.let { foundPerson -> call.respond(foundPerson.toDto()) }
|
||||||
?: call.respond(HttpStatusCode.NotFound, ErrorResponse.NOT_FOUND_RESPONSE)
|
?: call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
get("/search") {
|
get("/search") {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package com.ray650128.service
|
package com.ray650128.service
|
||||||
|
|
||||||
import com.ray650128.model.User
|
import com.ray650128.model.pojo.User
|
||||||
import org.bson.types.ObjectId
|
import org.bson.types.ObjectId
|
||||||
import org.litote.kmongo.*
|
import org.litote.kmongo.*
|
||||||
import org.litote.kmongo.id.toId
|
import org.litote.kmongo.id.toId
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user