Skip to content

Commit b001220

Browse files
committed
将配置文件移动到外部
1 parent 6d87578 commit b001220

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
/paintBoard/board | GET | 获取绘板内容
1212
/paintBoard/paint | POST(x: Int, y: Int, color: Int) | 进行绘画
1313
/paintBoard/ws | WebSocket | 监听绘板变化的 WebSocket
14-
/paintBoard/save | POST(password: String, path: String) | 保存绘板到本地
15-
/paintBoard/load | POST(password: String, path: String) | 从本地加载绘板(不会发送 WebSocket 信息)
14+
/paintBoard/save | POST(password: String, path: String) | 保存绘板到数据库
15+
/paintBoard/load | POST(password: String, path: String) | 从数据库加载绘板(不会发送 WebSocket 信息)
1616

1717
## How to RUN it
1818

File renamed without changes.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
kotlin.code.style=official
22
kotlinVersion=1.3.61
3-
ktorVersion=1.2.4
3+
ktorVersion=1.3.0

src/main/kotlin/org/hoshino9/luogu/paintboard/server/Application.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import io.ktor.websocket.webSocket
1717
import kotlinx.coroutines.delay
1818
import kotlinx.coroutines.launch
1919
import redis.clients.jedis.Jedis
20+
import java.io.File
2021
import java.util.*
2122

2223
data class PaintRequest(val x: Int, val y: Int, val color: String)
@@ -30,7 +31,7 @@ val sessions: MutableList<WebSocketSession> = Collections.synchronizedList(Linke
3031

3132
fun loadConfig() {
3233
config = Properties().apply {
33-
load(Unknown::class.java.getResourceAsStream("/config.properties"))
34+
load(File("config.properties").inputStream())
3435
}
3536
}
3637

@@ -42,7 +43,6 @@ fun connectRedis() {
4243
println("Connected redis server: $host:$port")
4344
}
4445

45-
4646
suspend fun onPaint(req: PaintRequest) {
4747
sessions.forEach {
4848
try {
@@ -59,13 +59,13 @@ fun main() {
5959
loadConfig()
6060
connectRedis()
6161

62-
embeddedServer(Netty, 8080) {
63-
try {
64-
load()
65-
} catch (e: Exception) {
66-
e.printStackTrace()
67-
}
62+
try {
63+
load()
64+
} catch (e: Exception) {
65+
e.printStackTrace()
66+
}
6867

68+
embeddedServer(Netty, 8080) {
6969
launch {
7070
while (true) {
7171
println("Saving board...")
@@ -86,10 +86,7 @@ fun main() {
8686
html = html.replace("\${wsurl}", config.getProperty("wsurl"))
8787
}
8888

89-
call.respondText(
90-
html,
91-
ContentType.Text.Html
92-
)
89+
call.respondText(html, ContentType.Text.Html)
9390
}
9491

9592
webSocket("/paintBoard/ws") {

src/main/kotlin/org/hoshino9/luogu/paintboard/server/Board.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ import io.ktor.routing.Routing
1010
import io.ktor.routing.get
1111
import io.ktor.routing.post
1212
import kotlinx.coroutines.launch
13+
import java.util.*
1314

14-
val board: Array<IntArray> = Array(800) {
15-
IntArray(400) {
16-
0x000000
15+
val board: MutableList<MutableList<Int>> = Collections.synchronizedList(
16+
ArrayList<MutableList<Int>>().apply {
17+
repeat(800) {
18+
ArrayList<Int>().apply {
19+
repeat(400) {
20+
add(0x000000)
21+
}
22+
}.let {
23+
Collections.synchronizedList(it).run(::add)
24+
}
25+
}
1726
}
18-
}
27+
)
1928

2029
var boardText: String
2130
get() {

0 commit comments

Comments
 (0)