Skip to content

Commit 1c4617e

Browse files
committed
refactor(model-server): extract IndexPage
1 parent 549f2ec commit 1c4617e

File tree

2 files changed

+88
-52
lines changed

2 files changed

+88
-52
lines changed

model-server/src/main/kotlin/org/modelix/model/server/Main.kt

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import io.ktor.server.application.ApplicationCall
2525
import io.ktor.server.application.call
2626
import io.ktor.server.application.install
2727
import io.ktor.server.engine.embeddedServer
28-
import io.ktor.server.html.respondHtmlTemplate
2928
import io.ktor.server.http.content.staticResources
3029
import io.ktor.server.netty.Netty
3130
import io.ktor.server.netty.NettyApplicationEngine
@@ -44,12 +43,6 @@ import io.ktor.server.routing.routing
4443
import io.ktor.server.websocket.WebSockets
4544
import io.ktor.server.websocket.pingPeriod
4645
import io.ktor.server.websocket.timeout
47-
import kotlinx.html.a
48-
import kotlinx.html.h1
49-
import kotlinx.html.li
50-
import kotlinx.html.style
51-
import kotlinx.html.ul
52-
import kotlinx.html.unsafe
5346
import kotlinx.serialization.encodeToString
5447
import kotlinx.serialization.json.Json
5548
import org.apache.commons.io.FileUtils
@@ -71,6 +64,7 @@ import org.modelix.model.server.handlers.ModelReplicationServer
7164
import org.modelix.model.server.handlers.RepositoriesManager
7265
import org.modelix.model.server.handlers.ui.ContentExplorer
7366
import org.modelix.model.server.handlers.ui.HistoryHandler
67+
import org.modelix.model.server.handlers.ui.IndexPage
7468
import org.modelix.model.server.handlers.ui.RepositoryOverview
7569
import org.modelix.model.server.store.IgniteStoreClient
7670
import org.modelix.model.server.store.InMemoryStoreClient
@@ -80,7 +74,6 @@ import org.modelix.model.server.store.forContextRepository
8074
import org.modelix.model.server.store.forGlobalRepository
8175
import org.modelix.model.server.store.loadDump
8276
import org.modelix.model.server.store.writeDump
83-
import org.modelix.model.server.templates.PageWithMenuBar
8477
import org.slf4j.LoggerFactory
8578
import org.springframework.util.ResourceUtils
8679
import java.io.File
@@ -217,61 +210,20 @@ object Main {
217210
installStatusPages()
218211

219212
modelServer.init(this)
213+
IndexPage().init(this)
220214
historyHandler.init(this)
221215
repositoryOverview.init(this)
222216
contentExplorer.init(this)
223217
jsonModelServer.init(this)
224218
modelReplicationServer.init(this)
225219
metricsApi.init(this)
226220
IdsApiImpl(repositoriesManager, localModelClient).init(this)
221+
227222
routing {
228223
HealthApiImpl(repositoriesManager, globalStoreClient, inMemoryModels).installRoutes(this)
229224

230225
staticResources("/public", "public")
231-
get("/") {
232-
call.respondHtmlTemplate(PageWithMenuBar("root", ".")) {
233-
headContent {
234-
style {
235-
unsafe {
236-
raw(
237-
"""
238-
body {
239-
font-family: sans-serif;
240-
table {
241-
border-collapse: collapse;
242-
}
243-
td, th {
244-
border: 1px solid #888;
245-
padding: 3px 12px;
246-
}
247-
""".trimIndent(),
248-
)
249-
}
250-
}
251-
}
252-
bodyContent {
253-
h1 { +"Model Server" }
254-
ul {
255-
li {
256-
a("repos/") { +"View Repositories on the Model Server" }
257-
}
258-
li {
259-
a("json/") { +"JSON API for JavaScript clients" }
260-
}
261-
li {
262-
a("headers") { +"View HTTP headers" }
263-
}
264-
li {
265-
a("user") { +"View JWT token and permissions" }
266-
}
267-
li {
268-
a("swagger") { +"SwaggerUI" }
269-
}
270-
}
271-
}
272-
}
273-
call.respondText("Model Server")
274-
}
226+
275227
if (cmdLineArgs.noSwaggerUi) {
276228
get("swagger") {
277229
call.respondText("SwaggerUI is disabled")
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.server.handlers.ui
18+
19+
import io.ktor.server.application.Application
20+
import io.ktor.server.application.call
21+
import io.ktor.server.html.respondHtmlTemplate
22+
import io.ktor.server.routing.get
23+
import io.ktor.server.routing.routing
24+
import kotlinx.html.a
25+
import kotlinx.html.h1
26+
import kotlinx.html.li
27+
import kotlinx.html.style
28+
import kotlinx.html.ul
29+
import kotlinx.html.unsafe
30+
import org.modelix.model.server.templates.PageWithMenuBar
31+
32+
/**
33+
* Landing page of the model-server with links to other pages.
34+
*/
35+
class IndexPage {
36+
37+
fun init(application: Application) {
38+
application.routing {
39+
get("/") {
40+
call.respondHtmlTemplate(PageWithMenuBar("root", ".")) {
41+
headContent {
42+
style {
43+
unsafe {
44+
raw(
45+
"""
46+
body {
47+
font-family: sans-serif;
48+
table {
49+
border-collapse: collapse;
50+
}
51+
td, th {
52+
border: 1px solid #888;
53+
padding: 3px 12px;
54+
}
55+
""".trimIndent(),
56+
)
57+
}
58+
}
59+
}
60+
bodyContent {
61+
h1 { +"Model Server" }
62+
ul {
63+
li {
64+
a("repos/") { +"View Repositories on the Model Server" }
65+
}
66+
li {
67+
a("json/") { +"JSON API for JavaScript clients" }
68+
}
69+
li {
70+
a("headers") { +"View HTTP headers" }
71+
}
72+
li {
73+
a("user") { +"View JWT token and permissions" }
74+
}
75+
li {
76+
a("swagger") { +"SwaggerUI" }
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)