Skip to content

Commit 672d329

Browse files
committed
feat(model-server): use resource based paths in all model-server endpoints (except WS)
1 parent 9a095d7 commit 672d329

File tree

7 files changed

+33
-25
lines changed

7 files changed

+33
-25
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ContentExplorer.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import io.ktor.server.application.call
66
import io.ktor.server.html.respondHtml
77
import io.ktor.server.html.respondHtmlTemplate
88
import io.ktor.server.request.receive
9+
import io.ktor.server.resources.get
10+
import io.ktor.server.resources.post
911
import io.ktor.server.response.respondRedirect
1012
import io.ktor.server.response.respondText
11-
import io.ktor.server.routing.get
12-
import io.ktor.server.routing.post
1313
import io.ktor.server.routing.routing
1414
import kotlinx.html.BODY
1515
import kotlinx.html.FlowContent
@@ -36,6 +36,7 @@ import kotlinx.html.title
3636
import kotlinx.html.tr
3737
import kotlinx.html.ul
3838
import kotlinx.html.unsafe
39+
import org.modelix.api.html.Paths
3940
import org.modelix.model.ModelFacade
4041
import org.modelix.model.api.BuiltinLanguages
4142
import org.modelix.model.api.INodeResolutionScope
@@ -64,10 +65,10 @@ class ContentExplorer(private val client: IModelClient, private val repoManager:
6465

6566
fun init(application: Application) {
6667
application.routing {
67-
get("/content/") {
68+
get<Paths.contentGet> {
6869
call.respondRedirect("../repos/")
6970
}
70-
get("/content/{versionHash}/") {
71+
get<Paths.contentVersionHashGet> {
7172
val versionHash = call.parameters["versionHash"]
7273
if (versionHash.isNullOrEmpty()) {
7374
call.respondText("version not found", status = HttpStatusCode.BadRequest)
@@ -86,7 +87,7 @@ class ContentExplorer(private val client: IModelClient, private val repoManager:
8687
bodyContent { contentPageBody(rootNode, versionHash, emptySet()) }
8788
}
8889
}
89-
post("/content/{versionHash}/") {
90+
post<Paths.contentVersionHashGet> {
9091
val versionHash = call.parameters["versionHash"]
9192
if (versionHash.isNullOrEmpty()) {
9293
call.respondText("version not found", status = HttpStatusCode.BadRequest)
@@ -110,7 +111,7 @@ class ContentExplorer(private val client: IModelClient, private val repoManager:
110111
},
111112
)
112113
}
113-
get("/content/{versionHash}/{nodeId}/") {
114+
get<Paths.contentVersionHashNodeIdGet> {
114115
val id = call.parameters["nodeId"]!!.toLong()
115116
var found: PNodeAdapter? = null
116117
for (node in rootNodes) {

model-server/src/main/kotlin/org/modelix/model/server/handlers/DeprecatedLightModelServer.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import io.ktor.server.application.Application
1919
import io.ktor.server.application.call
2020
import io.ktor.server.html.respondHtmlTemplate
2121
import io.ktor.server.request.receiveText
22+
import io.ktor.server.resources.get
23+
import io.ktor.server.resources.post
2224
import io.ktor.server.response.respond
2325
import io.ktor.server.response.respondText
2426
import io.ktor.server.routing.Route
25-
import io.ktor.server.routing.get
26-
import io.ktor.server.routing.post
2727
import io.ktor.server.routing.route
2828
import io.ktor.server.routing.routing
2929
import io.ktor.server.websocket.webSocket
@@ -43,6 +43,7 @@ import kotlinx.html.title
4343
import kotlinx.html.tr
4444
import org.json.JSONArray
4545
import org.json.JSONObject
46+
import org.modelix.api.deprecated.Paths
4647
import org.modelix.authorization.KeycloakScope
4748
import org.modelix.authorization.asResource
4849
import org.modelix.authorization.getUserName
@@ -78,7 +79,7 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
7879
application.apply {
7980
routing {
8081
requiresPermission("model-json-api".asResource(), KeycloakScope.READ) {
81-
route("/json") {
82+
route("/") {
8283
initRouting()
8384
}
8485
}
@@ -92,7 +93,7 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
9293
}
9394

9495
private fun Route.initRouting() {
95-
get("/") {
96+
get<Paths.jsonGet> {
9697
call.respondHtmlTemplate(PageWithMenuBar("json/", ".."), status = HttpStatusCode.OK) {
9798
headContent {
9899
title("JSON API")
@@ -141,20 +142,20 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
141142
}
142143
}
143144
}
144-
get("/{repositoryId}/") {
145+
get<Paths.jsonRepositoryIdGet> {
145146
val repositoryId = RepositoryId(call.parameters["repositoryId"]!!)
146147
val versionHash = client.asyncStore?.get(repositoryId.getBranchKey())!!
147148
// TODO 404 if it doesn't exist
148149
val version = CLVersion.loadFromHash(versionHash, getStore())
149150
respondVersion(version)
150151
}
151-
get("/{repositoryId}/{versionHash}/") {
152+
get<Paths.jsonRepositoryIdVersionHashGet> {
152153
val versionHash = call.parameters["versionHash"]!!
153154
// TODO 404 if it doesn't exist
154155
val version = CLVersion.loadFromHash(versionHash, getStore())
155156
respondVersion(version)
156157
}
157-
get("/{repositoryId}/{versionHash}/poll") {
158+
get<Paths.jsonRepositoryIdVersionHashPollGet> {
158159
val repositoryId = RepositoryId(call.parameters["repositoryId"]!!)
159160
val versionHash = call.parameters["versionHash"]!!
160161
val newValue = pollEntry(client.store, repositoryId.getBranchKey(), versionHash)
@@ -204,7 +205,7 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
204205
client.removeListener(repositoryId.getBranchKey(), listener)
205206
}
206207
}
207-
post("/{repositoryId}/init") {
208+
post<Paths.jsonRepositoryIdInitPost> {
208209
// TODO error if it already exists
209210
val repositoryId = RepositoryId(call.parameters["repositoryId"]!!)
210211
val newTree = CLTree.builder(getStore()).repositoryId(repositoryId).build()
@@ -220,7 +221,7 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
220221
client.asyncStore!!.put(repositoryId.getBranchKey(), newVersion.hash)
221222
respondVersion(newVersion)
222223
}
223-
post("/{repositoryId}/{versionHash}/update") {
224+
post<Paths.jsonRepositoryIdVersionHashUpdatePost> {
224225
val updateData = JSONArray(call.receiveText())
225226
val repositoryId = RepositoryId(call.parameters["repositoryId"]!!)
226227
val baseVersionHash = call.parameters["versionHash"]!!
@@ -233,7 +234,7 @@ class DeprecatedLightModelServer(val client: LocalModelClient) {
233234
val mergedVersion = applyUpdate(baseVersion, updateData, repositoryId, getUserName())
234235
respondVersion(mergedVersion, baseVersion)
235236
}
236-
post("/generate-ids") {
237+
post<Paths.jsonGenerateIdsPost> {
237238
val quantity = call.request.queryParameters["quantity"]?.toInt() ?: 1000
238239
val ids = (client.idGenerator as IdGenerator).generate(quantity)
239240
respondJson(

model-server/src/main/kotlin/org/modelix/model/server/handlers/HistoryHandler.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import io.ktor.server.application.Application
44
import io.ktor.server.application.call
55
import io.ktor.server.html.respondHtmlTemplate
66
import io.ktor.server.request.receiveParameters
7+
import io.ktor.server.resources.get
8+
import io.ktor.server.resources.post
79
import io.ktor.server.response.respondRedirect
8-
import io.ktor.server.routing.get
9-
import io.ktor.server.routing.post
1010
import io.ktor.server.routing.routing
1111
import kotlinx.datetime.Instant
1212
import kotlinx.datetime.TimeZone
@@ -33,6 +33,7 @@ import kotlinx.html.th
3333
import kotlinx.html.thead
3434
import kotlinx.html.tr
3535
import kotlinx.html.ul
36+
import org.modelix.api.html.Paths
3637
import org.modelix.authorization.KeycloakScope
3738
import org.modelix.authorization.asResource
3839
import org.modelix.authorization.getUserName
@@ -58,10 +59,10 @@ class HistoryHandler(val client: IModelClient, private val repositoriesManager:
5859

5960
fun init(application: Application) {
6061
application.routing {
61-
get("/history/") {
62+
get<Paths.historyGet> {
6263
call.respondRedirect("../repos/")
6364
}
64-
get("/history/{repoId}/{branch}/") {
65+
get<Paths.historyRepoIdBranchGet> {
6566
val repositoryId = RepositoryId(call.parameters["repoId"]!!)
6667
val branch = repositoryId.getBranchReference(call.parameters["branch"]!!)
6768
val params = call.request.queryParameters
@@ -84,7 +85,7 @@ class HistoryHandler(val client: IModelClient, private val repositoriesManager:
8485
}
8586
}
8687
requiresPermission("history".asResource(), KeycloakScope.WRITE) {
87-
post("/history/{repoId}/{branch}/revert") {
88+
post<Paths.historyRepoIdBranchRevertPost> {
8889
val repositoryId = RepositoryId(call.parameters["repoId"]!!)
8990
val branch = repositoryId.getBranchReference(call.parameters["branch"]!!)
9091
val params = call.receiveParameters()

model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import kotlinx.html.h1
3333
import kotlinx.html.span
3434
import org.json.JSONArray
3535
import org.json.JSONObject
36-
import org.modelix.Paths
36+
import org.modelix.api.public.Paths
3737
import org.modelix.authorization.EPermissionType
3838
import org.modelix.authorization.KeycloakResourceType
3939
import org.modelix.authorization.KeycloakScope

model-server/src/main/kotlin/org/modelix/model/server/handlers/ModelReplicationServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import io.ktor.websocket.send
3434
import kotlinx.coroutines.Job
3535
import kotlinx.serialization.encodeToString
3636
import kotlinx.serialization.json.Json
37-
import org.modelix.Paths
37+
import org.modelix.api.public.Paths
3838
import org.modelix.authorization.getUserName
3939
import org.modelix.model.api.PBranch
4040
import org.modelix.model.api.TreePointer

model-server/src/main/kotlin/org/modelix/model/server/handlers/RepositoryOverview.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.ktor.http.encodeURLPathPart
44
import io.ktor.server.application.Application
55
import io.ktor.server.application.call
66
import io.ktor.server.html.respondHtmlTemplate
7-
import io.ktor.server.routing.get
7+
import io.ktor.server.resources.get
88
import io.ktor.server.routing.routing
99
import kotlinx.html.FlowContent
1010
import kotlinx.html.FlowOrInteractiveOrPhrasingContent
@@ -20,13 +20,14 @@ import kotlinx.html.th
2020
import kotlinx.html.thead
2121
import kotlinx.html.title
2222
import kotlinx.html.tr
23+
import org.modelix.api.html.Paths
2324
import org.modelix.model.server.templates.PageWithMenuBar
2425

2526
class RepositoryOverview(private val repoManager: RepositoriesManager) {
2627

2728
fun init(application: Application) {
2829
application.routing {
29-
get("/repos/") {
30+
get<Paths.reposGet> {
3031
call.respondHtmlTemplate(PageWithMenuBar("repos/", "..")) {
3132
headContent { title("Repositories") }
3233
bodyContent { buildMainPage() }

model-server/src/test/kotlin/org/modelix/model/server/JsonAPITest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import io.ktor.http.ContentType
2323
import io.ktor.http.HttpStatusCode
2424
import io.ktor.http.contentType
2525
import io.ktor.server.application.install
26+
import io.ktor.server.resources.Resources
27+
import io.ktor.server.routing.IgnoreTrailingSlash
2628
import io.ktor.server.testing.ApplicationTestBuilder
2729
import io.ktor.server.testing.testApplication
2830
import io.ktor.server.websocket.WebSockets
@@ -44,6 +46,8 @@ class JsonAPITest {
4446
application {
4547
installAuthentication(unitTestMode = true)
4648
install(WebSockets)
49+
install(Resources)
50+
install(IgnoreTrailingSlash)
4751
DeprecatedLightModelServer(LocalModelClient(InMemoryStoreClient())).init(this)
4852
}
4953
block()

0 commit comments

Comments
 (0)