aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/App.kt
blob: 993ac9d1c8f4f9bb194b15183da96e3e8039678c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package space.anity

import io.javalin.Javalin
import io.javalin.NotFoundResponse
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

fun main(args: Array<String>) {
    val app = Javalin.create().start(7000)
    val fileHome = "files"

    app.get("/") { ctx ->
        ctx.result("Hello World")
    }

    app.get("/files/*") { ctx ->
        var files = ""
        try {
            Files.list(Paths.get("$fileHome/${ctx.splats()[0]}/")).forEach {
                val fileName = it.toString()
                    .drop(fileHome.length + (if (ctx.splats()[0].isNotEmpty()) ctx.splats()[0].length + 1 else 0))
                val filePath = "$fileHome${it.toString().drop(fileHome.length)}"
                files += if (File(filePath).isDirectory) "$fileName/\n" else "$fileName\n"
            }
            ctx.result(files)
        } catch (_: java.nio.file.NoSuchFileException) {
            throw NotFoundResponse("File or directory does not exist")
        }

        //File("test").writeText(ctx.splat(0)!!)
    }
}