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 --- build.gradle | 27 +++++++++++++--- 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 +++++++++ 6 files changed, 89 insertions(+), 28 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 diff --git a/build.gradle b/build.gradle index 7269324..70d4365 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id 'org.jetbrains.kotlin.jvm' version '1.3.11' - id "com.fizzed.rocker" version "1.2.1" + // id "com.fizzed.rocker" version "1.2.1" + id 'nu.studer.rocker' version '0.4' id 'java' } @@ -9,17 +10,31 @@ version "1.0-SNAPSHOT" repositories { jcenter() - mavenCentral() + // mavenCentral() } +/* sourceSets { main { rocker { - srcDir('src/main/kotlin') + srcDir("src/main/resources") } } } +*/ +rockerVersion = '0.20.0' + +rocker { + main { + optimize = true + templateDir = file('src/resources/views') + outputDir = file('src/resources/compiled-views') + } +} + + +/* buildscript { repositories { mavenLocal() @@ -33,13 +48,17 @@ buildscript { classpath "gradle.plugin.com.fizzed:rocker-gradle-plugin:1.2.1" } } +*/ dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" compile "io.javalin:javalin:2.8.0" compile "org.slf4j:slf4j-simple:1.7.26" compile "com.fasterxml.jackson.core:jackson-databind:2.9.8" - compile "com.fizzed:rocker-compiler:0.14.0" + // compile "com.fizzed:rocker-compiler:0.14.0" + compile 'org.jetbrains.exposed:exposed:0.13.2' + compile "org.xerial:sqlite-jdbc:3.21.0.1" + // compile group: 'com.fizzed', name: 'rocker-runtime', version: '1.2.1' } compileKotlin { 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