✨ Adds proper source/file handling and executing
This commit is contained in:
parent
beb37d92f6
commit
957e031073
@ -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
|
||||
|
@ -20,6 +20,10 @@ fun Application.configureRouting() {
|
||||
get("/") {
|
||||
call.respond("Hello World!")
|
||||
}
|
||||
|
||||
get("/health") {
|
||||
call.respond("OK")
|
||||
}
|
||||
}
|
||||
|
||||
routeAuth()
|
||||
|
@ -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<RunActionDTO>()
|
||||
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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dev.svitan.schemas
|
||||
package dev.svitan.services
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jetbrains.exposed.sql.*
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dev.svitan.routes
|
||||
package dev.svitan.services
|
||||
|
||||
import io.github.cdimascio.dotenv.Dotenv
|
||||
import java.io.File
|
Loading…
x
Reference in New Issue
Block a user