Skip to content

Commit

Permalink
Merge branch 'master' into new-auth-3
Browse files Browse the repository at this point in the history
# Conflicts:
#	server-core/src/main/kotlin/com/lightningkite/lightningserver/externalintegration/ExternalAsyncTaskIntegration.kt
  • Loading branch information
bjsvedin committed Sep 27, 2023
2 parents 9ed99f3 + 608f729 commit 86a7ed7
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 93 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
val kotlinVersion:String by extra
val khrysalisVersion: String by extra
repositories {
mavenLocal()
// mavenLocal()
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
google()
Expand All @@ -20,7 +20,7 @@ buildscript {
allprojects {
group = "com.lightningkite.lightningserver"
repositories {
mavenLocal()
// mavenLocal()
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
mavenCentral()
Expand Down
3 changes: 2 additions & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ repositories {
mavenCentral()
}

val ktorVersion:String by project
dependencies {
api(project(":server"))
ksp(project(":processor"))
implementation("com.lightningkite:kotliner-cli:1.0.3")
implementation("io.ktor:ktor-server-call-logging:2.1.0")
implementation("io.ktor:ktor-server-call-logging:$ktorVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
testImplementation(project(":client"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ internal fun defaultAwsHandler(project: TerraformProjectInfo) = with(project) {
appendLine("""
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "4.0.2"
name = "$namePrefix"
cidr = "${'$'}{var.ip_prefix}.0.0/16"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CloudwatchMetrics(

init {
MetricSettings.register("cloudwatch") {
Regex("""cloudwatch:\/\/((?:(?<user>[a-zA-Z0-9+\/]+):(?<password>[a-zA-Z0-9+\/]+)@)?(?<region>[a-zA-Z0-9-]+))/(?<namespace>[^?]+)""").matchEntire(
Regex("""cloudwatch://((?:(?<user>[a-zA-Z0-9+/]+):(?<password>[a-zA-Z0-9+/]+)@)?(?<region>[a-zA-Z0-9-]+))/(?<namespace>[^?]+)""").matchEntire(
it.url
)?.let { match ->
val user = match.groups["user"]?.value ?: ""
Expand Down
2 changes: 1 addition & 1 deletion server-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation("com.lightningkite.khrysalis:jvm-runtime:$khrysalisVersion")

api("io.ktor:ktor-client-content-negotiation:$ktorVersion")
api("io.ktor:ktor-client-cio:$ktorVersion")
api("io.ktor:ktor-client-cio-jvm:2.3.4")

implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ data class ExceptionSettings(
ExceptionSettings.register("debug") { DebugExceptionReporter }
ExceptionSettings.register("none") { NoExceptionReporter }
ExceptionSettings.register("grouped-db") {
val options = it.url.substringAfter("://")
val dbString = options.substringBefore('|')
val packageName = options.substringAfter('|')
val database = (Settings.requirements[dbString]?.invoke() as? Database)
?: DatabaseSettings(dbString).invoke()
GroupedDatabaseExceptionReporter(packageName, database)

Regex("""grouped-db://(?<dbString>[^|]+)\|(?<packageName>.+)""")
.matchEntire(it.url)
?.let { match ->
val dbString = match.groups["dbString"]!!.value
val packageName = match.groups["packageName"]!!.value
val database = (Settings.requirements[dbString]?.invoke() as? Database)
?: DatabaseSettings(dbString).invoke()
GroupedDatabaseExceptionReporter(packageName, database)
}
?: throw IllegalStateException("Invalid grouped-db URL. The URL should match the pattern: grouped-db://[dbString:Database Setting Name]|[packageName]")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class ExternalAsyncTaskIntegration<REQUEST, RESPONSE : HasId<String>, RESULT>(
)

init {
path.docName = path.toString().replace("/", "_")
path.docName = path.toString()
.replace(Regex("""[^0-9a-zA-Z]+(?<following>.)?""")) { match ->
match.groups["following"]?.value?.uppercase() ?: ""
}
.replaceFirstChar { it.lowercase() }
}

val rest = ModelRestEndpoints(path("rest").apply { docName = this@ExternalAsyncTaskIntegration.path.docName }, info)
Expand Down Expand Up @@ -186,7 +190,7 @@ class ExternalAsyncTaskIntegration<REQUEST, RESPONSE : HasId<String>, RESULT>(
}
)
val manualRecheckSingle = path("recheck").arg<String>("id").post.api(
summary = "Manually recheck tasks",
summary = "Manually recheck a task",
errorCases = listOf(),
authOptions = authOptions,
inputType = Unit.serializer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class HttpHeaderValue(
raw.substringAfter(';').split(';').associate { it.substringBefore('=').trim() to it.substringAfter('=').trim() }
)

override fun toString(): String = root + (parameters.entries.takeUnless { it.isEmpty() }?.joinToString("; ") {
override fun toString(): String = "$root; " + (parameters.entries.takeUnless { it.isEmpty() }?.joinToString("; ") {
"${it.key}=${it.value}"
} ?: "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ object Settings {

fun populate(map: Map<String, Any?>) {
if(sealed) throw IllegalStateException("Settings have already been populated.")
sealed = true
values.putAll(map.mapValues { Box(it.value) })
val missing = requirements.keys - values.keys
if (requirements.filter { it.key in missing }.any { !it.value.optional }) {
Expand All @@ -32,6 +31,7 @@ object Settings {
logger.debug("Loading setting ${it.name}...")
it()
}
sealed = true
}

private data class Box<T>(val item: T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ interface Documentable {

val Documentable.docGroup: String? get() = generateSequence(path.path) { it.parent }.mapNotNull { it.docName }.firstOrNull()
val Documentable.functionName: String
get() = summary.split(' ').joinToString("") { it.replaceFirstChar { it.uppercase() } }
get() = summary
.replace(Regex("""[^0-9a-zA-Z]+(?<following>.)?""")) { match ->
match.groups["following"]?.value?.uppercase() ?: ""
}
.replaceFirstChar { it.lowercase() }

internal fun KSerializer<*>.subSerializers(): Array<KSerializer<*>> = listElement()?.let { arrayOf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.google.firebase.FirebaseOptions
import com.google.firebase.messaging.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.google.firebase.messaging.Notification as FCMNotification
import java.io.File
import com.google.firebase.messaging.Notification as FCMNotification


/**
Expand All @@ -17,10 +17,14 @@ import java.io.File
object FcmNotificationClient : NotificationClient {
init {
NotificationSettings.register("fcm") {
var creds = it.credentials?.trim() ?: throw IllegalStateException(
"FCM was selected for notification implementation, but no credential file was provided."
)
if(!creds.startsWith('{')) {

var creds = it.credentials?.trim()
?: it.implementation.substringAfter("://", "").takeIf { it.isNotBlank() }
?: throw IllegalStateException(
"FCM was selected for notifications, but no credentials were provided."
)

if (!creds.startsWith('{')) {
val file = File(creds)
assert(file.exists()) { "FCM credentials file not found at '$file'" }
creds = file.readText()
Expand Down Expand Up @@ -88,7 +92,7 @@ object FcmNotificationClient : NotificationClient {
setAndroidConfig(
with(AndroidConfig.builder()) {
setPriority(android.priority.toAndroid())
data.timeToLive?.let{
data.timeToLive?.let {
setTtl(it.seconds)
}
setNotification(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lightningkite.lightningdb

import com.github.jershell.kbson.*
import com.lightningkite.lightningserver.core.Disconnectable
import com.lightningkite.lightningserver.db.DatabaseSettings
import com.lightningkite.lightningserver.serialization.Serialization
Expand All @@ -9,23 +8,13 @@ import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.kotlin.client.coroutine.MongoClient
import com.mongodb.kotlin.client.coroutine.MongoCollection
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.KSerializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.overwriteWith
import kotlinx.serialization.serializer
import org.bson.BsonDocument
import org.bson.BsonTimestamp
import org.bson.UuidRepresentation
import org.bson.types.Binary
import org.bson.types.ObjectId
import java.io.File
import java.math.BigDecimal
import java.time.*
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass
import kotlin.reflect.KType

class MongoDatabase(val databaseName: String, private val makeClient: () -> MongoClient) : Database, Disconnectable {
Expand Down Expand Up @@ -53,46 +42,78 @@ class MongoDatabase(val databaseName: String, private val makeClient: () -> Mong
companion object {
init {
DatabaseSettings.register("mongodb") {
val databaseName: String =
it.url.substringAfter("://").substringAfter('@').substringAfter('/', "").substringBefore('?')
MongoDatabase(databaseName = databaseName) {
MongoClient.create(MongoClientSettings.builder()
.applyConnectionString(ConnectionString(it.url))
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyToConnectionPoolSettings {
if (Settings.isServerless) {
it.maxSize(4)
it.maxConnectionIdleTime(15, TimeUnit.SECONDS)
it.maxConnectionLifeTime(1L, TimeUnit.MINUTES)
}
Regex("""mongodb://.*/(?<databaseName>[^?]+)(?:\?.*)?""")
.matchEntire(it.url)
?.let { match ->
MongoDatabase(databaseName = match.groups["databaseName"]!!.value) {
MongoClient.create(MongoClientSettings.builder()
.applyConnectionString(ConnectionString(it.url))
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyToConnectionPoolSettings {
if (Settings.isServerless) {
it.maxSize(4)
it.maxConnectionIdleTime(15, TimeUnit.SECONDS)
it.maxConnectionLifeTime(1L, TimeUnit.MINUTES)
}
}
.build()
)
}
.build()
)
}
}
?: throw IllegalStateException("Invalid mongodb URL. The URL should match the pattern: mongodb://[credentials and host information]/[databaseName]?[params]")
}
DatabaseSettings.register("mongodb+srv") {
val databaseName: String =
it.url.substringAfter("://").substringAfter('@').substringAfter('/', "").substringBefore('?')
MongoDatabase(databaseName = databaseName) {
MongoClient.create(MongoClientSettings.builder()
.applyConnectionString(ConnectionString(it.url))
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyToConnectionPoolSettings {
if (Settings.isServerless) {
it.maxSize(4)
it.maxConnectionIdleTime(15, TimeUnit.SECONDS)
it.maxConnectionLifeTime(1L, TimeUnit.MINUTES)
}
Regex("""mongodb\+srv://.*/(?<databaseName>[^?]+)(?:\?.*)?""")
.matchEntire(it.url)
?.let { match ->
MongoDatabase(databaseName = match.groups["databaseName"]!!.value) {
MongoClient.create(MongoClientSettings.builder()
.applyConnectionString(ConnectionString(it.url))
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyToConnectionPoolSettings {
if (Settings.isServerless) {
it.maxSize(4)
it.maxConnectionIdleTime(15, TimeUnit.SECONDS)
it.maxConnectionLifeTime(1L, TimeUnit.MINUTES)
}
}
.build()
)
}
.build()
)
}
}
?: throw IllegalStateException("Invalid mongodb URL. The URL should match the pattern: mongodb+srv://[credentials and host information]/[databaseName]?[params]")
}
DatabaseSettings.register("mongodb-test") {
MongoDatabase(databaseName = "default") { testMongo() }
Regex("""mongodb-test(?:://(?:\?(?<params>.*))?)?""")
.matchEntire(it.url)
?.let { match ->
val params: Map<String, List<String>>? = match.groups["params"]?.value?.let { params ->
DatabaseSettings.parseParameterString(params)
}
MongoDatabase(databaseName = "default") {
testMongo(version = params?.get("mongoVersion")?.firstOrNull())
}
}
?: throw IllegalStateException("Invalid mongodb-test URL. The URL should match the pattern: mongodb-test://?[params]\nAvailable params are: mongoVersion")
}
DatabaseSettings.register("mongodb-file") {
MongoDatabase(databaseName = "default") { embeddedMongo(File(it.url.removePrefix("mongodb-file://"))) }
Regex("""mongodb-file://(?<folder>[^?]+)(?:\?(?<params>.*))?""")
.matchEntire(it.url)
?.let { match ->
val folder = match.groups["folder"]!!.value
val params: Map<String, List<String>>? = match.groups["params"]?.value?.let { params ->
DatabaseSettings.parseParameterString(params)
}
MongoDatabase(databaseName = params?.get("databaseName")?.firstOrNull() ?: "default") {
embeddedMongo(
replFile = File(folder),
port = params?.get("port")?.firstOrNull()?.toIntOrNull(),
version = params?.get("mongoVersion")?.firstOrNull()
)
}

}
?: throw IllegalStateException("Invalid mongodb-file URL. The URL should match the pattern: mongodb-file://[FolderPath]?[params]\nAvailable params are: mongoVersion, port, databaseName")
}
}
}
Expand Down
Loading

0 comments on commit 86a7ed7

Please sign in to comment.