Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ktor+netty-based API #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ dependencies {
implementation("org.apache.commons", "commons-math3", "3.6.1")
implementation("org.jetbrains:annotations:23.0.0")

implementation("com.google.code.gson:gson:2.9.0")

implementation("io.ktor:ktor-server-core:2.0.2")
implementation("io.ktor:ktor-server-netty:2.0.2")

testImplementation("org.assertj", "assertj-core", "3.22.0")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.8.2")
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.8.2")
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/org/ageseries/libage/api/LibageApiResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.ageseries.libage.api

import com.google.gson.Gson
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*

val gson = Gson()

data class LibageApiResponse(val crashed: Boolean, val why: String?, val json: String?, val code: Int) {
suspend fun respond(call: ApplicationCall) {
call.respondText(ContentType.Application.Json, HttpStatusCode.fromValue(code)) {
gson.toJson(
mapOf(
Pair("crashed", crashed),
Pair("why", why),
Pair("result", json)
)
)
}
}
}

suspend fun ApplicationCall.libageApiResponse(provider: suspend () -> String?) {
try {
val output = provider.invoke()
LibageApiResponse(false, null, output, 200).respond(this)
} catch (e: Exception) {
LibageApiResponse(true, e.stackTraceToString(), null, 500).respond(this)
}
}
56 changes: 56 additions & 0 deletions src/main/kotlin/org/ageseries/libage/api/Server.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.ageseries.libage.api

import com.google.gson.Gson
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.ageseries.libage.sim.electrical.mna.Circuit
import org.ageseries.libage.sim.electrical.mna.component.Component
import org.ageseries.libage.sim.electrical.mna.component.Resistor
import org.ageseries.libage.sim.electrical.mna.component.VoltageSource
import java.util.*
import kotlin.collections.MutableList
import kotlin.collections.mutableListOf
import kotlin.collections.mutableMapOf
import kotlin.collections.set

data class CircuitBundle(var circuit: Circuit, var components: MutableList<Component>)

val circuits = mutableMapOf<UUID, CircuitBundle>()

fun loadTestCircuits() {
Copy link
Contributor Author

@jrddunbr jrddunbr Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just the first test from the MNATests.kt file. It doesn't need to be added, it's here for debugging.

val c = Circuit()
val r = Resistor()
val vs = VoltageSource()

c.add(vs, r)

vs.connect(0, r, 0)
vs.connect(1, r, 1)
vs.ground(0)

vs.potential = 10.0
r.resistance = 10.0

circuits[UUID.randomUUID()] = CircuitBundle(c, mutableListOf(r, vs))
}

fun main() {
loadTestCircuits()

embeddedServer(Netty, port = 8080) {
routing {
get("/") {
call.respondText("libage api endpoint")
}
get("/circuits.json") {

call.libageApiResponse {
gson.toJson(circuits)
}
}
}
}.start(wait = true)
}