diff --git a/backend/src/main/kotlin/plugins/Databases.kt b/backend/src/main/kotlin/plugins/Databases.kt index 4befedb..23e727c 100644 --- a/backend/src/main/kotlin/plugins/Databases.kt +++ b/backend/src/main/kotlin/plugins/Databases.kt @@ -1,8 +1,8 @@ package dev.svitan.plugins -import dev.svitan.routes.ExecutorService -import dev.svitan.schemas.ActionService -import dev.svitan.schemas.AuthService +import dev.svitan.services.ExecutorService +import dev.svitan.services.ActionService +import dev.svitan.services.AuthService import io.github.cdimascio.dotenv.Dotenv import io.ktor.server.application.* import org.jetbrains.exposed.sql.Database diff --git a/backend/src/main/kotlin/plugins/Routing.kt b/backend/src/main/kotlin/plugins/Routing.kt index 6105fec..e0da31d 100644 --- a/backend/src/main/kotlin/plugins/Routing.kt +++ b/backend/src/main/kotlin/plugins/Routing.kt @@ -20,6 +20,10 @@ fun Application.configureRouting() { get("/") { call.respond("Hello World!") } + + get("/health") { + call.respond("OK") + } } routeAuth() diff --git a/backend/src/main/kotlin/routes/Action.kt b/backend/src/main/kotlin/routes/Action.kt index 11b47c4..96f18cc 100644 --- a/backend/src/main/kotlin/routes/Action.kt +++ b/backend/src/main/kotlin/routes/Action.kt @@ -1,10 +1,12 @@ package dev.svitan.routes import dev.svitan.plugins.AuthorizationException -import dev.svitan.schemas.ActionService -import dev.svitan.schemas.AuthService -import dev.svitan.schemas.NewActionDTO -import dev.svitan.schemas.RunActionDTO +import dev.svitan.services.ActionKind +import dev.svitan.services.ActionService +import dev.svitan.services.AuthService +import dev.svitan.services.ExecutorService +import dev.svitan.services.NewActionDTO +import dev.svitan.services.RunActionDTO import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -39,13 +41,17 @@ fun Application.routeAction() { val action = ActionService.read(id) ?: throw NotFoundException() val run = call.receive() - val auth = AuthService.validate( + AuthService.validate( UUID.fromString(action.authId), run.pin, run.key ) ?: throw AuthorizationException("Invalid pin or key") - // TODO: run the action + if (action.kind == ActionKind.TEXT) { + ExecutorService.executeSource(id, action.source) + } else { + ExecutorService.executeFile(id) + } call.respond(HttpStatusCode.NoContent) } diff --git a/backend/src/main/kotlin/routes/Auth.kt b/backend/src/main/kotlin/routes/Auth.kt index 7546ec1..9110160 100644 --- a/backend/src/main/kotlin/routes/Auth.kt +++ b/backend/src/main/kotlin/routes/Auth.kt @@ -1,7 +1,7 @@ package dev.svitan.routes -import dev.svitan.schemas.AuthService -import dev.svitan.schemas.NewAuthDTO +import dev.svitan.services.AuthService +import dev.svitan.services.NewAuthDTO import io.ktor.http.HttpStatusCode import io.ktor.server.application.Application import io.ktor.server.auth.authentication diff --git a/backend/src/main/kotlin/services/Action.kt b/backend/src/main/kotlin/services/Action.kt index 929e6b1..00c604c 100644 --- a/backend/src/main/kotlin/services/Action.kt +++ b/backend/src/main/kotlin/services/Action.kt @@ -1,4 +1,4 @@ -package dev.svitan.schemas +package dev.svitan.services import io.ktor.server.plugins.NotFoundException import kotlinx.serialization.Serializable @@ -49,7 +49,7 @@ class NewActionDTO( @Serializable class RunActionDTO( val pin: String, - val key: String + val key: String ) class ActionService { @@ -72,19 +72,26 @@ class ActionService { } } - fun create(action: NewActionDTO): UUID = transaction { - val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) - val authId = UUID.fromString(action.authId) - AuthService.read(authId) ?: throw NotFoundException("auth not found") + fun create(action: NewActionDTO): UUID { + val id = transaction { + val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + val authId = UUID.fromString(action.authId) + AuthService.read(authId) ?: throw NotFoundException("auth not found") - Actions.insert { - it[name] = action.name - it[kind] = ActionKind.from(action.kind).toString() - it[aSource] = action.source - it[auth] = authId - it[createdAt] = now - it[updatedAt] = now - }[Actions.id] + Actions.insert { + it[name] = action.name + it[kind] = ActionKind.from(action.kind).toString() + it[aSource] = if (action.kind == ActionKind.SCRIPT.toString()) "" else action.source + it[auth] = authId + it[createdAt] = now + it[updatedAt] = now + }[Actions.id] + } + + if (action.kind == ActionKind.SCRIPT.toString()) { + ExecutorService.writeFile(id, action.source) + } + return id } fun read(id: UUID): ActionDTO? = transaction { @@ -118,6 +125,8 @@ class ActionService { } fun update(id: UUID, action: NewActionDTO) { + val oldAction = read(id) ?: throw NotFoundException("action not found") + transaction { val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) Actions.update({ Actions.id eq id }) { @@ -127,12 +136,23 @@ class ActionService { it[updatedAt] = now } } + + if (action.kind == ActionKind.SCRIPT.toString()) { + ExecutorService.writeFile(id, action.source) + } else if (oldAction.kind == ActionKind.SCRIPT) { + ExecutorService.deleteFile(id) + } } fun delete(id: UUID) { + val action = read(id) ?: throw NotFoundException("action not found") transaction { Actions.deleteWhere { Actions.id eq id } } + + if (action.kind == ActionKind.SCRIPT) { + ExecutorService.deleteFile(id) + } } } } diff --git a/backend/src/main/kotlin/services/Auth.kt b/backend/src/main/kotlin/services/Auth.kt index be9fec6..3d587ac 100644 --- a/backend/src/main/kotlin/services/Auth.kt +++ b/backend/src/main/kotlin/services/Auth.kt @@ -1,4 +1,4 @@ -package dev.svitan.schemas +package dev.svitan.services import kotlinx.serialization.Serializable import org.jetbrains.exposed.sql.* diff --git a/backend/src/main/kotlin/routes/Executor.kt b/backend/src/main/kotlin/services/Executor.kt similarity index 98% rename from backend/src/main/kotlin/routes/Executor.kt rename to backend/src/main/kotlin/services/Executor.kt index 2b51be5..d854161 100644 --- a/backend/src/main/kotlin/routes/Executor.kt +++ b/backend/src/main/kotlin/services/Executor.kt @@ -1,4 +1,4 @@ -package dev.svitan.routes +package dev.svitan.services import io.github.cdimascio.dotenv.Dotenv import java.io.File