|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package org.modelix.modelql.server |
| 15 | + |
| 16 | +import io.ktor.http.ContentType |
| 17 | +import io.ktor.http.HttpStatusCode |
| 18 | +import io.ktor.server.application.ApplicationCall |
| 19 | +import io.ktor.server.application.call |
| 20 | +import io.ktor.server.request.receiveText |
| 21 | +import io.ktor.server.response.respondText |
| 22 | +import io.ktor.server.routing.Route |
| 23 | +import io.ktor.server.routing.post |
| 24 | +import kotlinx.coroutines.runBlocking |
| 25 | +import kotlinx.coroutines.withContext |
| 26 | +import kotlinx.serialization.KSerializer |
| 27 | +import org.modelix.model.api.INode |
| 28 | +import org.modelix.model.api.INodeResolutionScope |
| 29 | +import org.modelix.model.area.IArea |
| 30 | +import org.modelix.modelql.core.IMonoUnboundQuery |
| 31 | +import org.modelix.modelql.core.IStepOutput |
| 32 | +import org.modelix.modelql.core.QueryGraphDescriptor |
| 33 | +import org.modelix.modelql.core.VersionAndData |
| 34 | +import org.modelix.modelql.core.modelqlVersion |
| 35 | +import org.modelix.modelql.core.upcast |
| 36 | +import org.modelix.modelql.untyped.UntypedModelQL |
| 37 | +import org.modelix.modelql.untyped.createQueryExecutor |
| 38 | + |
| 39 | +class ModelQLServer private constructor(val rootNodeProvider: () -> INode?, val area: IArea? = null) { |
| 40 | + fun installHandler(route: Route) { |
| 41 | + route.post("query") { |
| 42 | + val rootNode = rootNodeProvider()!! |
| 43 | + handleCall(call, rootNode, area ?: rootNode.getArea()) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + class Builder { |
| 48 | + private var rootNodeProvider: () -> INode? = { null } |
| 49 | + private var area: IArea? = null |
| 50 | + |
| 51 | + fun rootNode(rootNode: INode): Builder { |
| 52 | + this.rootNodeProvider = { rootNode } |
| 53 | + return this |
| 54 | + } |
| 55 | + |
| 56 | + fun rootNode(rootNodeProvider: () -> INode?): Builder { |
| 57 | + this.rootNodeProvider = rootNodeProvider |
| 58 | + return this |
| 59 | + } |
| 60 | + |
| 61 | + fun area(area: IArea): Builder { |
| 62 | + this.area = area |
| 63 | + return this |
| 64 | + } |
| 65 | + |
| 66 | + fun build(): ModelQLServer { |
| 67 | + return ModelQLServer( |
| 68 | + rootNodeProvider, |
| 69 | + area |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + companion object { |
| 75 | + private val LOG = mu.KotlinLogging.logger { } |
| 76 | + |
| 77 | + fun builder(rootNode: INode): Builder = Builder().also { it.rootNode(rootNode) } |
| 78 | + fun builder(rootNodeProvider: () -> INode?): Builder = Builder().also { it.rootNode(rootNodeProvider) } |
| 79 | + |
| 80 | + suspend fun handleCall(call: ApplicationCall, rootNode: INode, area: IArea) { |
| 81 | + try { |
| 82 | + val serializedQuery = call.receiveText() |
| 83 | + val json = UntypedModelQL.json |
| 84 | + val queryDescriptor = VersionAndData.deserialize(serializedQuery, QueryGraphDescriptor.serializer(), json).data |
| 85 | + val query = queryDescriptor.createRootQuery() as IMonoUnboundQuery<INode, Any?> |
| 86 | + LOG.debug { "query: $query" } |
| 87 | + val nodeResolutionScope: INodeResolutionScope = area |
| 88 | + val transactionBody: () -> IStepOutput<Any?> = { |
| 89 | + runBlocking { |
| 90 | + withContext(nodeResolutionScope) { |
| 91 | + query.bind(rootNode.createQueryExecutor()).execute() |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + val result: IStepOutput<Any?> = if (query.requiresWriteAccess()) { |
| 96 | + area.executeWrite(transactionBody) |
| 97 | + } else { |
| 98 | + area.executeRead(transactionBody) |
| 99 | + } |
| 100 | + val serializer: KSerializer<IStepOutput<Any?>> = |
| 101 | + query.getAggregationOutputSerializer(json.serializersModule).upcast() |
| 102 | + |
| 103 | + val versionAndResult = VersionAndData(result) |
| 104 | + val serializedResult = json.encodeToString(VersionAndData.serializer(serializer), versionAndResult) |
| 105 | + call.respondText(text = serializedResult, contentType = ContentType.Application.Json) |
| 106 | + } catch (ex: Throwable) { |
| 107 | + call.respondText(text = "server version: $modelqlVersion\n" + ex.stackTraceToString(), status = HttpStatusCode.InternalServerError) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments