refactor: add --host option and default web host binding to 0.0.0.0

This commit is contained in:
2026-02-24 18:52:24 +08:00
parent 5af1a18a48
commit 00508e3121
4 changed files with 9 additions and 8 deletions

View File

@@ -9,7 +9,7 @@ COPY gradle gradle
COPY build.gradle.kts settings.gradle.kts ./ COPY build.gradle.kts settings.gradle.kts ./
COPY src src COPY src src
RUN ./gradlew --no-daemon clean installDist RUN gradle --no-daemon clean installDist
FROM ${RUNTIME_IMAGE} FROM ${RUNTIME_IMAGE}
WORKDIR /app WORKDIR /app
@@ -20,4 +20,4 @@ RUN mkdir -p /app/scripts
EXPOSE 8080 EXPOSE 8080
ENTRYPOINT ["/app/kotlin-scripts-host/bin/kotlin-scripts-host"] ENTRYPOINT ["/app/kotlin-scripts-host/bin/kotlin-scripts-host"]
CMD ["--port=8080", "--scripts-dir=/app/scripts"] CMD ["--host=0.0.0.0", "--port=8080", "--scripts-dir=/app/scripts"]

View File

@@ -16,7 +16,7 @@ Watch mode:
## Run Web host (Ktor) ## Run Web host (Ktor)
```bash ```bash
./gradlew runWeb --args='--port=8080 --scripts-dir=./scripts' ./gradlew runWeb --args='--host=0.0.0.0 --port=8080 --scripts-dir=./scripts'
``` ```
Auth: Auth:

View File

@@ -15,4 +15,4 @@ services:
HOST_API_TOKEN: ${HOST_API_TOKEN:-} HOST_API_TOKEN: ${HOST_API_TOKEN:-}
volumes: volumes:
- ./scripts:/app/scripts - ./scripts:/app/scripts
command: ["--port=8080", "--scripts-dir=/app/scripts"] command: ["--host=0.0.0.0", "--port=8080", "--scripts-dir=/app/scripts"]

View File

@@ -16,7 +16,7 @@ import java.io.File
private const val DEFAULT_PORT = 8080 private const val DEFAULT_PORT = 8080
private const val DEFAULT_SCRIPTS_DIR = "scripts" private const val DEFAULT_SCRIPTS_DIR = "scripts"
private const val HOST = "127.0.0.1" private const val DEFAULT_HOST = "0.0.0.0"
private fun Application.module(scriptsDir: File, apiToken: String) { private fun Application.module(scriptsDir: File, apiToken: String) {
routing { routing {
@@ -74,7 +74,7 @@ private fun usage() {
println( println(
""" """
Usage: Usage:
./gradlew runWeb --args='[--port=8080] [--scripts-dir=./scripts]' ./gradlew runWeb --args='[--host=0.0.0.0] [--port=8080] [--scripts-dir=./scripts]'
Routes: Routes:
GET /health GET /health
Authorization: Authorization:
@@ -104,12 +104,13 @@ fun main(args: Array<String>) {
} }
val port = cli.optionValue("--port=")?.toIntOrNull() ?: DEFAULT_PORT val port = cli.optionValue("--port=")?.toIntOrNull() ?: DEFAULT_PORT
val host = cli.optionValue("--host=")?.ifBlank { DEFAULT_HOST } ?: DEFAULT_HOST
val scriptsDir = File(cli.optionValue("--scripts-dir=") ?: DEFAULT_SCRIPTS_DIR).absoluteFile val scriptsDir = File(cli.optionValue("--scripts-dir=") ?: DEFAULT_SCRIPTS_DIR).absoluteFile
if (!scriptsDir.exists()) scriptsDir.mkdirs() if (!scriptsDir.exists()) scriptsDir.mkdirs()
val auth = loadOrCreateApiToken(scriptsDir) val auth = loadOrCreateApiToken(scriptsDir)
println("Starting script web host on http://$HOST:$port") println("Starting script web host on http://$host:$port")
println("Scripts directory: ${scriptsDir.absolutePath}") println("Scripts directory: ${scriptsDir.absolutePath}")
println("Auth token source: ${auth.source}") println("Auth token source: ${auth.source}")
when { when {
@@ -119,7 +120,7 @@ fun main(args: Array<String>) {
else -> println("Auth token loaded from file: ${auth.tokenFile?.absolutePath}") else -> println("Auth token loaded from file: ${auth.tokenFile?.absolutePath}")
} }
embeddedServer(Netty, port = port, host = HOST) { embeddedServer(Netty, port = port, host = host) {
module(scriptsDir, auth.token) module(scriptsDir, auth.token)
}.start(wait = true) }.start(wait = true)
} }