Add logs for every routes
continuous-integration/drone/push Build is passing Details

pull/17/head
luevard 8 months ago
parent 441e213d3e
commit 2b13728b9c

@ -17,4 +17,5 @@ object ApiMessage {
const val FRIENDS_ALREADY_EXISTS = "User already exists in your Friends List."
const val FRIENDS_DOESNT_EXISTS = "User doesn't exists in your Friends List."
const val FRIENDS_REQUEST_HIMSELF = "The receiver can't be the sender."
const val FILE_NOT_FOUND = "File not found."
}

@ -1,6 +1,7 @@
package allin.routing
import allin.model.ApiMessage
import allin.utils.AppConfig
import io.github.smiley4.ktorswaggerui.dsl.get
import io.ktor.http.*
import io.ktor.server.application.*
@ -9,6 +10,8 @@ import io.ktor.server.routing.*
fun Application.basicRouter() {
val logManager = AppConfig.logManager
routing {
get("/", {
description = "Hello World of Allin API"
@ -21,6 +24,7 @@ fun Application.basicRouter() {
}
}
}) {
logManager.log("Routing","Get '/'")
call.respond(ApiMessage.WELCOME)
}
}

@ -5,6 +5,7 @@ import allin.ext.hasToken
import allin.ext.verifyUserFromToken
import allin.model.ApiMessage
import allin.model.BetDetail
import allin.utils.AppConfig
import io.github.smiley4.ktorswaggerui.dsl.get
import io.ktor.http.*
import io.ktor.server.application.*
@ -18,6 +19,7 @@ import java.util.*
fun Application.betDetailRouter() {
val userDataSource = this.dataSource.userDataSource
val betDataSource = this.dataSource.betDataSource
val logManager = AppConfig.logManager
routing {
authenticate {
@ -38,16 +40,19 @@ fun Application.betDetailRouter() {
}
}
}) {
logManager.log("Routing","GET /betdetail/get/{id}")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val id = call.parameters["id"].toString()
val result = betDataSource.getBetDetailById(id, user.username)
if (result != null) {
logManager.log("Routing","ACCEPTED GET /betdetail/get/{id}\t${result}")
call.respond(
HttpStatusCode.Accepted,
result
)
} else {
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} GET /betdetail/get/{id}")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
}

@ -21,6 +21,7 @@ val tokenManagerBet = AppConfig.tokenManager
fun Application.betRouter() {
val userDataSource = this.dataSource.userDataSource
val betDataSource = this.dataSource.betDataSource
val logManager = AppConfig.logManager
routing {
authenticate {
@ -45,16 +46,19 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","POST /bets/add")
hasToken { principal ->
val bet = call.receive<Bet>()
val id = UUID.randomUUID().toString()
val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username)
betDataSource.getBetById(id)?.let {
logManager.log("Routing","${ApiMessage.BET_ALREADY_EXIST} /bets/add")
call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST)
} ?: run {
val betWithId = bet.copy(id = id, createdBy = user.first?.username.toString())
betDataSource.addBet(betWithId)
logManager.log("Routing","CREATED /bets/add\t${betWithId}")
call.respond(HttpStatusCode.Created, betWithId)
}
}
@ -78,12 +82,14 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","POST /bets/gets")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { _, _ ->
val filtersRequest =
kotlin.runCatching { call.receiveNullable<BetFiltersRequest>() }.getOrNull()
val filters =
filtersRequest?.filters ?: emptyList() // Use provided filters or empty list if null
logManager.log("Routing","ACCEPTED /bets/gets\t${filters}")
call.respond(HttpStatusCode.Accepted, betDataSource.getAllBets(filters))
}
}
@ -105,13 +111,16 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/popular")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { _, _ ->
val bet = betDataSource.getMostPopularBet()
if (bet != null) {
logManager.log("Routing","ACCEPTED /bets/popular\t${bet}")
call.respond(HttpStatusCode.Accepted, bet)
}
call.respond(HttpStatusCode.NotFound, "Aucun bet n'a pu être récupérer")
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} /bets/popular")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
}
}
@ -135,10 +144,14 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/get/{id}")
val id = call.parameters["id"] ?: ""
betDataSource.getBetById(id)?.let { bet ->
logManager.log("Routing","ACCEPTED /bets/get/{id}\t ${bet}")
call.respond(HttpStatusCode.Accepted, bet)
} ?: call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
} ?:
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} /bets/get/{id}")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
post("/bets/delete", {
@ -158,10 +171,13 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","POST /bets/delete")
val id = call.receive<Map<String, String>>()["id"] ?: ""
if (betDataSource.removeBet(id)) {
logManager.log("Routing","ACCEPTED /bets/delete")
call.respond(HttpStatusCode.Accepted)
} else {
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} /bets/delete")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
}
@ -183,10 +199,13 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","POST /bets/update")
val updatedBetData = call.receive<UpdatedBetData>()
if (betDataSource.updateBet(updatedBetData)) {
logManager.log("Routing","ACCEPTED /bets/delete")
call.respond(HttpStatusCode.Accepted)
} else {
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} /bets/delete")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
}
@ -206,9 +225,11 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/toConfirm")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val response = betDataSource.getToConfirm(user.username)
logManager.log("Routing","ACCEPTED /bets/toConfirm\t${response}")
call.respond(HttpStatusCode.Accepted, response)
}
}
@ -230,8 +251,10 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/getWon")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
logManager.log("Routing","ACCEPTED /bets/getWon")
call.respond(HttpStatusCode.Accepted, betDataSource.getWonNotifications(user.username))
}
}
@ -253,8 +276,10 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/history")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
logManager.log("Routing","ACCEPTED /bets/toConfirm\t${betDataSource.getHistory(user.username)}")
call.respond(HttpStatusCode.Accepted, betDataSource.getHistory(user.username))
}
}
@ -276,8 +301,10 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/current")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
logManager.log("Routing","ACCEPTED /bets/toConfirm\t${betDataSource.getCurrent(user.username)}")
call.respond(HttpStatusCode.Accepted, betDataSource.getCurrent(user.username))
}
}
@ -303,6 +330,7 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","GET /bets/confirm/{id}")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val betId = call.parameters["id"] ?: ""
@ -310,8 +338,10 @@ fun Application.betRouter() {
if (betDataSource.getBetById(betId)?.createdBy == user.username) {
betDataSource.confirmBet(betId, result)
logManager.log("Routing","ACCEPTED /bets/confirm/{id}")
call.respond(HttpStatusCode.OK)
} else {
logManager.log("Routing","UNAUTHORIZED /bets/confirm/{id}")
call.respond(HttpStatusCode.Unauthorized)
}

@ -5,6 +5,7 @@ import allin.dto.UserDTO
import allin.ext.hasToken
import allin.ext.verifyUserFromToken
import allin.model.ApiMessage
import allin.utils.AppConfig
import io.github.smiley4.ktorswaggerui.dsl.get
import io.github.smiley4.ktorswaggerui.dsl.post
import io.ktor.http.*
@ -19,7 +20,7 @@ fun Application.friendRouter() {
val userDataSource = this.dataSource.userDataSource
val friendDataSource = this.dataSource.friendDataSource
val logManager = AppConfig.logManager
routing {
authenticate {
@ -37,10 +38,12 @@ fun Application.friendRouter() {
}
}
}) {
logManager.log("Routing","GET /friends/gets")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { _, _ ->
val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username).first
logManager.log("Routing","ACCEPTED /friends/gets\t${friendDataSource.getFriendFromUserId(user?.id.toString())}")
call.respond(HttpStatusCode.Accepted, friendDataSource.getFriendFromUserId(user?.id.toString()))
}
}
@ -60,10 +63,13 @@ fun Application.friendRouter() {
}
}
}) {
logManager.log("Routing","GET /friends/requests")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { _, _ ->
val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username).first
logManager.log("Routing","ACCEPTED /friends/requests\t${friendDataSource.getFriendRequestsFromUserId(user?.id.toString())
}")
call.respond(
HttpStatusCode.Accepted,
friendDataSource.getFriendRequestsFromUserId(user?.id.toString())
@ -93,6 +99,7 @@ fun Application.friendRouter() {
}
}
}) {
logManager.log("Routing","POST /friends/add")
hasToken { principal ->
val requestMap = call.receive<Map<String, String>>()
val usernameFriend = requestMap["username"] ?: return@hasToken call.respond(
@ -105,14 +112,18 @@ fun Application.friendRouter() {
val userFriend = userDataSource.getUserByUsername(usernameFriend).first
if (user == null || userFriend == null) {
logManager.log("Routing","${ApiMessage.USER_NOT_FOUND} /friends/add")
call.respond(HttpStatusCode.Conflict, ApiMessage.USER_NOT_FOUND)
} else if (userFriend == user) {
logManager.log("Routing","${ApiMessage.FRIENDS_REQUEST_HIMSELF} /friends/add")
call.respond(HttpStatusCode.Conflict, ApiMessage.FRIENDS_REQUEST_HIMSELF)
} else {
val friendlist = friendDataSource.getFriendFromUserId(user.id)
if (friendlist.map { it.id }.contains(userFriend.id)) {
logManager.log("Routing","${ApiMessage.FRIENDS_ALREADY_EXISTS} /friends/add")
call.respond(HttpStatusCode.Conflict, ApiMessage.FRIENDS_ALREADY_EXISTS)
} else {
logManager.log("Routing","ACCEPTED /friends/add\t${usernameFriend}")
friendDataSource.addFriend(user.id, userFriend.id)
call.respond(HttpStatusCode.Created, usernameFriend)
}
@ -140,6 +151,7 @@ fun Application.friendRouter() {
}
}
}) {
logManager.log("Routing","POST /friends/delete")
hasToken { principal ->
val requestMap = call.receive<Map<String, String>>()
val usernameFriend = requestMap["username"] ?: return@hasToken call.respond(
@ -152,11 +164,14 @@ fun Application.friendRouter() {
val userFriend = userDataSource.getUserByUsername(usernameFriend).first
if (user == null || userFriend == null) {
logManager.log("Routing","${ApiMessage.USER_NOT_FOUND} /friends/delete")
call.respond(HttpStatusCode.Conflict, ApiMessage.USER_NOT_FOUND)
} else {
if (friendDataSource.deleteFriend(user.id, userFriend.id)) {
logManager.log("Routing","ACCEPTED /friends/delete\t${usernameFriend}")
call.respond(HttpStatusCode.Created, usernameFriend)
} else {
logManager.log("Routing","${ApiMessage.FRIENDS_DOESNT_EXISTS} /friends/delete")
call.respond(HttpStatusCode.Conflict, ApiMessage.FRIENDS_DOESNT_EXISTS)
}
}
@ -179,13 +194,14 @@ fun Application.friendRouter() {
}
}) {
logManager.log("Routing","GET /friends/search/{search}")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { userDto, _ ->
val users = friendDataSource.filterUsersByUsername(
fromUserId = userDto.id,
search = call.parameters["search"] ?: ""
)
logManager.log("Routing","ACCEPTED /friends/search/{search}\t${users}")
call.respond(HttpStatusCode.OK, users)
}
}

@ -6,6 +6,7 @@ import allin.ext.verifyUserFromToken
import allin.model.ApiMessage
import allin.model.Participation
import allin.model.ParticipationRequest
import allin.utils.AppConfig
import io.github.smiley4.ktorswaggerui.dsl.delete
import io.github.smiley4.ktorswaggerui.dsl.post
import io.ktor.http.*
@ -23,6 +24,7 @@ fun Application.participationRouter() {
val userDataSource = this.dataSource.userDataSource
val participationDataSource = this.dataSource.participationDataSource
val betDataSource = this.dataSource.betDataSource
val logManager = AppConfig.logManager
routing {
authenticate {
@ -45,11 +47,13 @@ fun Application.participationRouter() {
}
}) {
logManager.log("Routing","POST /participations/add")
hasToken { principal ->
val participation = call.receive<ParticipationRequest>()
verifyUserFromToken(userDataSource, principal) { user, _ ->
if(betDataSource.getBetById(participation.betId)== null){
logManager.log("Routing","${ApiMessage.BET_NOT_FOUND} /participations/add")
call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND)
}
@ -66,8 +70,10 @@ fun Application.participationRouter() {
userDataSource.removeCoins(username = user.username, amount = participation.stake)
betDataSource.updatePopularityScore(participation.betId)
logManager.log("Routing","CREATED /participations/add")
call.respond(HttpStatusCode.Created)
} else {
logManager.log("Routing","${ApiMessage.NOT_ENOUGH_COINS} /participations/add")
call.respond(HttpStatusCode.Forbidden, ApiMessage.NOT_ENOUGH_COINS)
}
}
@ -91,11 +97,14 @@ fun Application.participationRouter() {
}
}
}) {
logManager.log("Routing","DELETE /participations/delete")
hasToken {
val participationId = call.receive<String>()
if (participationDataSource.deleteParticipation(participationId)) {
logManager.log("Routing","ACCEPTED /participations/delete")
call.respond(HttpStatusCode.NoContent)
} else {
logManager.log("Routing","${ApiMessage.PARTICIPATION_NOT_FOUND} /participations/delete")
call.respond(HttpStatusCode.NotFound, ApiMessage.PARTICIPATION_NOT_FOUND)
}
}

@ -30,6 +30,7 @@ const val DEFAULT_COINS = 500
fun Application.userRouter() {
val userDataSource = this.dataSource.userDataSource
val logManager = AppConfig.logManager
routing {
post("/users/register", {
@ -56,12 +57,16 @@ fun Application.userRouter() {
}
}
}) {
logManager.log("Routing","POST /users/register")
val tempUser = call.receive<UserRequest>()
if (RegexCheckerUser.isEmailInvalid(tempUser.email)) {
logManager.log("Routing","${ApiMessage.INVALID_MAIL} /users/register")
call.respond(HttpStatusCode.Forbidden, ApiMessage.INVALID_MAIL)
} else if (userDataSource.userExists(tempUser.username)) {
logManager.log("Routing","${ApiMessage.USER_ALREADY_EXISTS} /users/register")
call.respond(HttpStatusCode.Conflict, ApiMessage.USER_ALREADY_EXISTS)
} else if (userDataSource.emailExists(tempUser.email)) {
logManager.log("Routing","${ApiMessage.MAIL_ALREADY_EXISTS} /users/register")
call.respond(HttpStatusCode.Conflict, ApiMessage.MAIL_ALREADY_EXISTS)
} else {
val user = User(
@ -78,6 +83,7 @@ fun Application.userRouter() {
CryptManagerUser.passwordCrypt(user)
user.token = tokenManagerUser.generateOrReplaceJWTToken(user)
userDataSource.addUser(user)
logManager.log("Routing","ACCEPTED /users/register\t${user}")
call.respond(HttpStatusCode.Created, user)
}
}
@ -100,19 +106,25 @@ fun Application.userRouter() {
}
}
}) {
logManager.log("Routing","POST /users/login")
val checkUser = call.receive<CheckUser>()
val user = userDataSource.getUserByUsername(checkUser.login)
if (CryptManagerUser.passwordDecrypt(user.second ?: "", checkUser.password)) {
user.first?.let { userDtoWithToken ->
userDtoWithToken.token = tokenManagerUser.generateOrReplaceJWTToken(userDtoWithToken)
logManager.log("Routing","ACCEPTED /users/login\t${userDtoWithToken}")
call.respond(HttpStatusCode.OK, userDtoWithToken)
} ?: call.respond(HttpStatusCode.NotFound, ApiMessage.USER_NOT_FOUND)
} ?:
logManager.log("Routing","${ApiMessage.USER_NOT_FOUND} /users/login")
call.respond(HttpStatusCode.NotFound, ApiMessage.USER_NOT_FOUND)
} else {
logManager.log("Routing","${ApiMessage.INCORRECT_LOGIN_PASSWORD} /users/login")
call.respond(HttpStatusCode.NotFound, ApiMessage.INCORRECT_LOGIN_PASSWORD)
}
}
get("/users/images/{fileName}") {
logManager.log("Routing","GET /users/images/{fileName}")
val fileName = call.parameters["fileName"]
val urlfile = "images/$fileName"
val file = File("$urlfile.png")
@ -122,9 +134,11 @@ fun Application.userRouter() {
val imageBytes = userDataSource.getImage(fileName.toString())
if (imageBytes != null) {
imageManagerUser.saveImage(urlfile, imageBytes)
logManager.log("Routing","ACCEPTED /users/images/{fileName}")
call.respondFile(file)
} else {
call.respond(HttpStatusCode.NotFound, "File not found")
logManager.log("Routing","${ApiMessage.FILE_NOT_FOUND} /users/images/{fileName}")
call.respond(HttpStatusCode.NotFound, ApiMessage.FILE_NOT_FOUND)
}
}
}
@ -156,15 +170,19 @@ fun Application.userRouter() {
}
}) {
logManager.log("Routing","POST /users/delete")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { _, password ->
val checkUser = call.receive<CheckUser>()
if (CryptManagerUser.passwordDecrypt(password, checkUser.password)) {
if (!userDataSource.deleteUser(checkUser.login)) {
logManager.log("Routing","${ApiMessage.USER_CANT_BE_DELETE} /users/delete")
call.respond(HttpStatusCode.InternalServerError, ApiMessage.USER_CANT_BE_DELETE)
}
logManager.log("Routing","ACCEPTED /users/delete")
call.respond(HttpStatusCode.Accepted, password)
} else {
logManager.log("Routing","${ApiMessage.INCORRECT_LOGIN_PASSWORD} /users/delete")
call.respond(HttpStatusCode.NotFound, ApiMessage.INCORRECT_LOGIN_PASSWORD)
}
@ -185,8 +203,10 @@ fun Application.userRouter() {
}
}
}) {
logManager.log("Routing","GET /users/token")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { userDto, _ ->
logManager.log("Routing","ACCEPTED /users/token\t${userDto}")
call.respond(HttpStatusCode.OK, userDto)
}
}
@ -210,18 +230,21 @@ fun Application.userRouter() {
}
}) {
logManager.log("Routing","GET /users/gift")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { userDto, _ ->
if (userDataSource.canHaveDailyGift(userDto.username)) {
val dailyGift = (DAILY_GIFT_MIN..DAILY_GIFT_MAX).random()
userDataSource.addCoins(userDto.username, dailyGift)
logManager.log("Routing","ACCEPTED /users/gift\t${dailyGift}")
call.respond(HttpStatusCode.OK, dailyGift)
logManager.log("Routing","${ApiMessage.NO_GIFT} /users/gift")
} else call.respond(HttpStatusCode.MethodNotAllowed, ApiMessage.NO_GIFT)
}
}
}
post("/users/image", {
post("/users/images", {
description = "Allow you to add a profil image"
request {
@ -241,6 +264,8 @@ fun Application.userRouter() {
}
}) {
logManager.log("Routing","POST /users/images")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
@ -251,9 +276,11 @@ fun Application.userRouter() {
if (imageByteArray != null && imageByteArray.isNotEmpty()) {
userDataSource.removeImage(user.id)
userDataSource.addImage(user.id, imageByteArray)
logManager.log("Routing","ACCEPTED /users/images")
call.respond(HttpStatusCode.OK, "${urlManager.getURL()}users/${urlfile}")
}
call.respond(HttpStatusCode.Conflict)
logManager.log("Routing","${ApiMessage.FILE_NOT_FOUND} /users/images")
call.respond(HttpStatusCode.Conflict,ApiMessage.FILE_NOT_FOUND)
}
}
}

@ -10,4 +10,5 @@ object AppConfig {
val cryptManager = CryptManager()
val imageManager = ImageManager()
val urlManager = URLManager()
val logManager = LogManager()
}

@ -0,0 +1,12 @@
package allin.utils
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class LogManager {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
fun log(type: String,message: String) {
println("[${LocalDateTime.now().format(formatter)}] [${type}] $message")
}
}
Loading…
Cancel
Save