From f8f09b016bdd6c93195e87a65afb2d154e399d0f Mon Sep 17 00:00:00 2001 From: climb Date: Wed, 3 Apr 2019 21:12:37 +0200 Subject: added sqlite database and tried to get rocker templating to work --- src/main/kotlin/App.kt | 54 +++++++++++++++++++++++++++---- src/main/resources/test.rocker.html | 2 -- src/main/resources/upload.html | 16 --------- src/main/resources/views/test.rocker.html | 2 ++ src/main/resources/views/upload.html | 16 +++++++++ 5 files changed, 66 insertions(+), 24 deletions(-) delete mode 100644 src/main/resources/test.rocker.html delete mode 100644 src/main/resources/upload.html create mode 100644 src/main/resources/views/test.rocker.html create mode 100644 src/main/resources/views/upload.html (limited to 'src/main') diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index b5d6042..fe1abef 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -1,23 +1,44 @@ package space.anity -import com.fizzed.rocker.* +import com.fizzed.rocker.Rocker import io.javalin.* import io.javalin.core.util.* import io.javalin.rendering.* import io.javalin.rendering.template.TemplateUtil.model +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.SchemaUtils +import org.jetbrains.exposed.sql.Table +import org.jetbrains.exposed.sql.transactions.TransactionManager +import org.jetbrains.exposed.sql.transactions.transaction import java.io.* import java.nio.file.* +import java.sql.Connection + fun main(args: Array) { + + // TODO outsource to class + val db : Database = Database.connect("jdbc:sqlite:main.db", "org.sqlite.JDBC") + TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE + + transaction { + SchemaUtils.createMissingTablesAndColumns(FileLocation, UserData, General) + } + + val fileHome = "files" - JavalinRenderer.register( + /*JavalinRenderer.register( FileRenderer { filepath, model -> Rocker.template(filepath).bind(model).render().toString() }, ".rocker.html" - ) + )*/ + val app = Javalin.create().enableStaticFiles("../resources/").start(7000) + // TODO: Fix rocker templating app.get("/test") { ctx -> - ctx.render("test.rocker.html", model("message", "Testy testy!")) + val templateTest = Rocker.template("test.rocker.html").bind("message", "Testy testy!").render().toString() + println(templateTest) + // ctx.render("views/test.rocker.html", model("message", "Testy testy!")) } // TODO: Fix possible security issue with "../" @@ -36,16 +57,37 @@ fun main(args: Array) { } } - app.get("/upload") { ctx -> ctx.redirect("/upload.html") } + app.get("/upload") { ctx -> ctx.redirect("/views/upload.html") } // TODO: Fix possible security issue with "../" app.post("/api/upload") { ctx -> ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) -> if (ctx.queryParam("dir") !== null) { FileUtil.streamToFile(content, "files/${ctx.queryParam("dir")}/$name") - ctx.redirect("/upload.html") + ctx.redirect("/views/upload.html") } else throw BadRequestResponse("Error: Please enter a filename.") } } } + +// DB tables +object FileLocation : Table() { + val id = integer("id").autoIncrement().primaryKey() + val location = text("location") +} + +object UserData : Table() { + // only for multiple users: val id = integer("id").autoIncrement().primaryKey() + val uname = varchar("uname", 24).primaryKey() // remove if ID + val pwd = varchar("pwd", 64) +} + +object General : Table() { + // redundant: val id = integer("id").autoIncrement().primaryKey() + val isSetup = integer("isSetup").primaryKey() // remove pKey if ID // boolean -> 0:1 + // TODO if not isSetup show other front page +} + + + diff --git a/src/main/resources/test.rocker.html b/src/main/resources/test.rocker.html deleted file mode 100644 index 8deedcd..0000000 --- a/src/main/resources/test.rocker.html +++ /dev/null @@ -1,2 +0,0 @@ -@args (String message) -

@message

diff --git a/src/main/resources/upload.html b/src/main/resources/upload.html deleted file mode 100644 index 16c8a94..0000000 --- a/src/main/resources/upload.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - Upload Testing - - -
- - -
- - diff --git a/src/main/resources/views/test.rocker.html b/src/main/resources/views/test.rocker.html new file mode 100644 index 0000000..8deedcd --- /dev/null +++ b/src/main/resources/views/test.rocker.html @@ -0,0 +1,2 @@ +@args (String message) +

@message

diff --git a/src/main/resources/views/upload.html b/src/main/resources/views/upload.html new file mode 100644 index 0000000..16c8a94 --- /dev/null +++ b/src/main/resources/views/upload.html @@ -0,0 +1,16 @@ + + + + + + + Upload Testing + + +
+ + +
+ + -- cgit v1.2.3