Minor fixes for web and mobile

This commit is contained in:
William Brawner 2021-08-16 15:25:32 -06:00
parent f2700f7148
commit ca28d32aa9
15 changed files with 121 additions and 17 deletions

View file

@ -2,9 +2,9 @@ package com.wbrawner.twigs
import com.wbrawner.twigs.model.Budget
import com.wbrawner.twigs.model.Permission
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.storage.BudgetRepository
import com.wbrawner.twigs.storage.PermissionRepository
import com.wbrawner.twigs.storage.Session
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*

View file

@ -2,10 +2,10 @@ package com.wbrawner.twigs
import com.wbrawner.twigs.model.Budget
import com.wbrawner.twigs.model.Permission
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.model.UserPermission
import com.wbrawner.twigs.storage.BudgetRepository
import com.wbrawner.twigs.storage.PermissionRepository
import com.wbrawner.twigs.storage.Session
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*

View file

@ -2,9 +2,9 @@ package com.wbrawner.twigs
import com.wbrawner.twigs.model.Category
import com.wbrawner.twigs.model.Permission
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.storage.CategoryRepository
import com.wbrawner.twigs.storage.PermissionRepository
import com.wbrawner.twigs.storage.Session
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*

View file

@ -1,9 +1,9 @@
package com.wbrawner.twigs
import com.wbrawner.twigs.model.Permission
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.model.Transaction
import com.wbrawner.twigs.storage.PermissionRepository
import com.wbrawner.twigs.storage.Session
import com.wbrawner.twigs.storage.TransactionRepository
import io.ktor.application.*
import io.ktor.auth.*

View file

@ -1,8 +1,8 @@
package com.wbrawner.twigs
import com.wbrawner.twigs.model.Permission
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.model.User
import com.wbrawner.twigs.storage.Session
import kotlinx.serialization.Serializable
import java.util.*

View file

@ -1,8 +1,8 @@
package com.wbrawner.twigs
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.model.User
import com.wbrawner.twigs.storage.PermissionRepository
import com.wbrawner.twigs.storage.Session
import com.wbrawner.twigs.storage.SessionRepository
import com.wbrawner.twigs.storage.UserRepository
import io.ktor.application.*

View file

@ -1,4 +1,5 @@
import java.net.URI
import java.util.*
plugins {
java
@ -48,3 +49,53 @@ tasks.shadowJar {
archiveVersion.set("")
}
}
val captainDefinition = File(project.buildDir, "captain-definition")
val tarFile = File(project.buildDir, "twigs.tar")
tasks.register("package") {
dependsOn(":app:shadowJar")
doLast {
captainDefinition.createNewFile()
captainDefinition.outputStream().writer().use {
it.appendLine(
"""
{
"schemaVersion": 2,
"dockerfileLines": [
"FROM adoptopenjdk:openj9",
"COPY libs/twigs.jar twigs.jar",
"CMD /opt/java/openjdk/bin/java ${'$'}JVM_ARGS -jar /twigs.jar"
]
}
""".trimIndent()
)
}
exec {
commandLine(
"tar",
"-C",
project.buildDir.absolutePath,
"-cf",
project.buildDir.name + File.separator + tarFile.name,
captainDefinition.name,
"libs/twigs.jar"
)
}
}
}
tasks.register("publish") {
dependsOn(":app:package")
doLast {
var command = listOf("caprover", "deploy", "-t", "build/${tarFile.name}", "-n", "wbrawner", "-a", "twigs")
command = if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows")) {
listOf("powershell", "-Command") + command
} else {
listOf("bash", "-c", "\"${command.joinToString(" ")}\"")
}
exec {
commandLine(command)
}
}
}

View file

@ -2,6 +2,7 @@ package com.wbrawner.twigs.server
import com.wbrawner.twigs.*
import com.wbrawner.twigs.db.*
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.storage.*
import com.wbrawner.twigs.web.webRoutes
import com.zaxxer.hikari.HikariConfig
@ -27,11 +28,11 @@ private const val DATABASE_VERSION = 1
@ExperimentalTime
fun Application.module() {
val dbHost = environment.config.propertyOrNull("ktor.database.host")?.getString() ?: "localhost"
val dbPort = environment.config.propertyOrNull("ktor.database.port")?.getString() ?: "5432"
val dbName = environment.config.propertyOrNull("ktor.database.name")?.getString() ?: "twigs"
val dbUser = environment.config.propertyOrNull("ktor.database.user")?.getString() ?: "twigs"
val dbPass = environment.config.propertyOrNull("ktor.database.password")?.getString() ?: "twigs"
val dbHost = environment.config.propertyOrNull("twigs.database.host")?.getString() ?: "localhost"
val dbPort = environment.config.propertyOrNull("twigs.database.port")?.getString() ?: "5432"
val dbName = environment.config.propertyOrNull("twigs.database.name")?.getString() ?: "twigs"
val dbUser = environment.config.propertyOrNull("twigs.database.user")?.getString() ?: "twigs"
val dbPass = environment.config.propertyOrNull("twigs.database.password")?.getString() ?: "twigs"
val jdbcUrl = "jdbc:postgresql://$dbHost:$dbPort/$dbName?stringtype=unspecified"
HikariDataSource(HikariConfig().apply {
setJdbcUrl(jdbcUrl)
@ -119,7 +120,16 @@ fun Application.moduleWithDependencies(
metadataRepository.runMigration(version)
metadataRepository.save(metadata.copy(version = version))
}
salt = metadata.salt
salt = metadata.salt.ifEmpty {
metadataRepository.save(
metadata.copy(
salt = environment.config
.propertyOrNull("twigs.password.salt")
?.getString()
?: randomString(16)
)
).salt
}
while (currentCoroutineContext().isActive) {
delay(Duration.hours(24))
sessionRepository.deleteExpired()

View file

@ -6,6 +6,9 @@ ktor {
application {
modules = [ com.wbrawner.twigs.server.ApplicationKt.module ]
}
}
twigs {
database {
host = localhost
host = ${?TWIGS_DB_HOST}
@ -18,4 +21,7 @@ ktor {
password = twigs
password = ${?TWIGS_DB_PASS}
}
password {
salt = ${?TWIGS_PW_SALT}
}
}

View file

@ -1,4 +1,4 @@
package com.wbrawner.twigs.storage
package com.wbrawner.twigs.model
import com.wbrawner.twigs.Identifiable
import com.wbrawner.twigs.randomString

View file

@ -1,8 +1,6 @@
package com.wbrawner.twigs.db
import com.wbrawner.twigs.randomString
data class DatabaseMetadata(
val version: Int = 0,
val salt: String = randomString(16)
val salt: String = ""
)

View file

@ -36,6 +36,7 @@ class JdbcCategoryRepository(dataSource: DataSource) :
sql.append(" AND ${Fields.ARCHIVED.name.lowercase()} = ?")
params.add(it)
}
sql.append(" ORDER BY ${Fields.TITLE.name.lowercase()} ASC")
conn.executeQuery(sql.toString(), params)
}

View file

@ -1,6 +1,6 @@
package com.wbrawner.twigs.db
import com.wbrawner.twigs.storage.Session
import com.wbrawner.twigs.model.Session
import com.wbrawner.twigs.storage.SessionRepository
import java.sql.ResultSet
import java.time.Instant

View file

@ -1,5 +1,7 @@
package com.wbrawner.twigs.storage
import com.wbrawner.twigs.model.Session
interface SessionRepository : Repository<Session> {
fun findAll(
token: String

View file

@ -1,3 +1,5 @@
import java.util.*
plugins {
kotlin("jvm")
`java-library`
@ -15,3 +17,37 @@ dependencies {
tasks.getByName<Test>("test") {
useJUnitPlatform()
}
// TODO: Replace this hack with either a git submodule or an internal Kotlin-based UI
tasks.register("package") {
doLast {
val built = File(rootProject.rootDir.parent, "twigs-web/dist/twigs")
if (built.exists()) {
built.deleteRecursively()
}
val dest = File(project.projectDir, "src/main/resources/twigs")
if (dest.exists()) {
dest.deleteRecursively()
}
var command = listOf(
"cd", "../../twigs-web", ";",
"npm", "i", ";",
"npm", "run", "package"
)
command = if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows")) {
listOf("powershell", "-Command") + command
} else {
listOf("bash", "-c", "\"${command.joinToString(" ")}\"")
}
exec {
commandLine(command)
}
if (!built.copyRecursively(dest, true) || !dest.isDirectory) {
throw GradleException("Failed to copy files from ${built.absolutePath} to ${dest.absolutePath}")
}
}
}
tasks.getByName("processResources") {
dependsOn.add("package")
}