aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorclimb2019-04-04 14:54:28 +0200
committerclimb2019-04-04 14:54:28 +0200
commitb0a2b6c5a19e0e20d50d29f52d640b5f7d19f841 (patch)
tree21c65a4d9622f7f8c202344709a659dd70c8baaa /src/main/kotlin
parent390db9229b2aaa0a6ca7a920e4901b581ba917e2 (diff)
Moved database to own class
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/App.kt40
-rw-r--r--src/main/kotlin/DB.kt48
2 files changed, 53 insertions, 35 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index 87055a1..4682e92 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -5,29 +5,23 @@ import io.javalin.*
import io.javalin.core.util.*
import io.javalin.rendering.*
import io.javalin.rendering.template.TemplateUtil.model
-import org.jetbrains.exposed.sql.*
-import org.jetbrains.exposed.sql.transactions.*
import java.io.*
import java.nio.file.*
-import java.sql.*
+
fun main() {
val app = Javalin.create().enableStaticFiles("../resources/").start(7000)
val fileHome = "files"
- // TODO: Move to own database 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)
- }
-
JavalinRenderer.register(
FileRenderer { filepath, model -> Rocker.template(filepath).bind(model).render().toString() }, ".rocker.html"
)
+ val db = DB()
+
+ // TODO: If not initialUse show setup page
+
/**
* Sends a json object of filenames in [fileHome]s
* TODO: Fix possible security issue with "../"
@@ -78,31 +72,7 @@ fun main() {
}
}
-/**
- * Database table for the file location indexing
- */
-object FileLocation : Table() {
- val id = integer("id").autoIncrement().primaryKey()
- val location = text("location")
-}
-/**
- * Database table to index the users with their regarding passwords
- */
-object UserData : Table() {
- // only for multiple users: val id = integer("id").autoIncrement().primaryKey()
- val username = varchar("username", 24).primaryKey() // remove if ID
- val password = varchar("password", 64)
-}
-
-/**
- * Database table storing general data/states
- */
-object General : Table() {
- // redundant: val id = integer("id").autoIncrement().primaryKey()
- val initialUse = integer("initialUse").primaryKey() // remove pKey if ID // boolean -> 0:1
- // TODO: If not isSetup show other front page
-}
diff --git a/src/main/kotlin/DB.kt b/src/main/kotlin/DB.kt
new file mode 100644
index 0000000..a01a1e6
--- /dev/null
+++ b/src/main/kotlin/DB.kt
@@ -0,0 +1,48 @@
+package space.anity
+
+import org.jetbrains.exposed.sql.*
+import org.jetbrains.exposed.sql.transactions.*
+import java.sql.*
+
+
+class DB(val dbFileLocation: String = "main.db") {
+ val db :Database
+
+ /**
+ * Database table for the file location indexing
+ */
+ object FileLocation : Table() {
+ val id = integer("id").autoIncrement().primaryKey()
+ val location = text("location")
+ }
+
+ /**
+ * Database table to index the users with their regarding passwords
+ */
+ object UserData : Table() {
+ // only for multiple users:
+ // val id = integer("id").autoIncrement().primaryKey()
+ val username = varchar("username", 24).primaryKey() // remove .primaryKey(), if id column is used
+ val password = varchar("password", 64)
+ }
+
+ /**
+ * Database table storing general data/states
+ */
+ object General : Table() {
+ val initialUse = integer("initialUse").default(0).primaryKey() // boolean -> 0:1
+ }
+
+ init {
+ // create connection
+ this.db = Database.connect("jdbc:sqlite:main.db", "org.sqlite.JDBC")
+ TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
+
+ // add tables
+ transaction {
+ SchemaUtils.createMissingTablesAndColumns(FileLocation, UserData, General)
+ }
+ }
+
+ // TODO add functions for database usage
+} \ No newline at end of file