implemented 07-server-mode.md: added server profile with REST API for builds, artifacts, live logs, and control tokens; lifecycle management for watcher; controller and service tests
This commit is contained in:
@@ -5,6 +5,7 @@ import org.springframework.boot.ExitCodeGenerator
|
||||
import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.runApplication
|
||||
import org.springframework.context.annotation.Profile
|
||||
import org.springframework.stereotype.Component
|
||||
import picocli.CommandLine
|
||||
import picocli.CommandLine.IFactory
|
||||
@@ -13,7 +14,9 @@ import kotlin.system.exitProcess
|
||||
@SpringBootApplication
|
||||
class GitTallyApplication
|
||||
|
||||
/** Not in the `server` profile: the second context started by `ServerCommand` must not run picocli again. */
|
||||
@Component
|
||||
@Profile("!server")
|
||||
class CliRunner(
|
||||
private val factory: IFactory,
|
||||
private val rootCommand: GitTallyCommand,
|
||||
|
||||
@@ -104,6 +104,10 @@ class InitCommand(
|
||||
server:
|
||||
# Public base URL of this GitTally installation — used for all links posted to Gitea.
|
||||
publicBaseUrl: ""
|
||||
# HTTP port of the `server` subcommand
|
||||
port: 18080
|
||||
# bind address of the `server` subcommand
|
||||
bindAddress: 0.0.0.0
|
||||
|
||||
# Gitea integration for fetching commits and posting build statuses.
|
||||
gitea:
|
||||
|
||||
@@ -1,16 +1,79 @@
|
||||
package de.hoennig.gittally.commands
|
||||
|
||||
import de.hoennig.gittally.GitTallyApplication
|
||||
import de.hoennig.gittally.config.ConfigLoader
|
||||
import org.springframework.boot.WebApplicationType
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder
|
||||
import org.springframework.context.ApplicationListener
|
||||
import org.springframework.context.ConfigurableApplicationContext
|
||||
import org.springframework.context.event.ContextClosedEvent
|
||||
import org.springframework.stereotype.Component
|
||||
import picocli.CommandLine.Command
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
/**
|
||||
* The CLI context runs without a web server (web type `none` in `application.yml`),
|
||||
* so this command launches a second `SpringApplication` with
|
||||
* `WebApplicationType.SERVLET` and the `server` profile, then blocks until shutdown.
|
||||
* The profile switches the web type via `application-server.yml` (`spring.main.*`
|
||||
* beats programmatic builder settings), starts the watcher via `ServerWatcherLifecycle`,
|
||||
* and keeps `CliRunner` out of the second context.
|
||||
*/
|
||||
@Component
|
||||
@Command(
|
||||
name = "server",
|
||||
description = ["Start the GitTally server"],
|
||||
mixinStandardHelpOptions = true,
|
||||
)
|
||||
class ServerCommand : Runnable {
|
||||
class ServerCommand(
|
||||
private val configLoader: ConfigLoader,
|
||||
) : Runnable {
|
||||
var workingDir: Path = Paths.get(".")
|
||||
|
||||
override fun run() {
|
||||
println("server – not yet implemented")
|
||||
val config = configLoader.load(workingDir)
|
||||
val context =
|
||||
SpringApplicationBuilder(GitTallyApplication::class.java)
|
||||
.web(WebApplicationType.SERVLET)
|
||||
.profiles(SERVER_PROFILE)
|
||||
.properties(
|
||||
"server.port=${config.server.port}",
|
||||
"server.address=${config.server.bindAddress}",
|
||||
).run()
|
||||
val port = context.environment.getProperty("local.server.port", config.server.port.toString())
|
||||
println("GitTally server listening on http://${config.server.bindAddress}:$port/ — Ctrl-C to stop")
|
||||
awaitShutdown(context)
|
||||
}
|
||||
|
||||
/** Blocks until the context closes; Ctrl-C triggers the shutdown hook Spring registered. */
|
||||
private fun awaitShutdown(context: ConfigurableApplicationContext) {
|
||||
val closed = CountDownLatch(1)
|
||||
context.addApplicationListener(ApplicationListener<ContextClosedEvent> { closed.countDown() })
|
||||
if (context.isActive) {
|
||||
closed.await()
|
||||
}
|
||||
if (jvmIsShuttingDown()) {
|
||||
// Parked so that main() never races the shutdown hooks — `SpringApplication.exit`
|
||||
// on the closing CLI context would print a stack trace. The JVM halts once the
|
||||
// hooks have finished; only a programmatic context close falls through.
|
||||
Thread.currentThread().join()
|
||||
}
|
||||
}
|
||||
|
||||
/** Once the JVM shuts down, registering a shutdown hook throws. */
|
||||
private fun jvmIsShuttingDown(): Boolean =
|
||||
try {
|
||||
val probe = Thread { }
|
||||
Runtime.getRuntime().addShutdownHook(probe)
|
||||
Runtime.getRuntime().removeShutdownHook(probe)
|
||||
false
|
||||
} catch (_: IllegalStateException) {
|
||||
true
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val SERVER_PROFILE = "server"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ data class GitTallyConfig(
|
||||
|
||||
data class ServerConfig(
|
||||
val publicBaseUrl: String = "",
|
||||
/** HTTP port of the `server` subcommand; 18080 like the legacy artifact server. */
|
||||
val port: Int = 18080,
|
||||
val bindAddress: String = "0.0.0.0",
|
||||
)
|
||||
|
||||
data class GitConfig(
|
||||
|
||||
@@ -10,11 +10,14 @@ import de.hoennig.gittally.config.ConfigLoader
|
||||
import de.hoennig.gittally.config.GitTallyConfig
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.client.JdkClientHttpRequestFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.web.client.RestClient
|
||||
import org.springframework.web.client.RestClientException
|
||||
import java.net.http.HttpClient
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.time.Duration
|
||||
|
||||
/** Outcome of [GiteaClient.readStatus]; errors are values, never exceptions. */
|
||||
sealed interface GiteaStatusResult {
|
||||
@@ -167,9 +170,16 @@ class GiteaClient(
|
||||
config.gitea.repo.isNotBlank() &&
|
||||
config.git.token.isNotBlank()
|
||||
|
||||
/** Timeouts like the legacy status proxy: callers (e.g. `/api/status`) must never hang. */
|
||||
private val requestFactory =
|
||||
JdkClientHttpRequestFactory(
|
||||
HttpClient.newBuilder().connectTimeout(REQUEST_TIMEOUT).build(),
|
||||
).apply { setReadTimeout(REQUEST_TIMEOUT) }
|
||||
|
||||
private fun restClient(config: GitTallyConfig): RestClient =
|
||||
RestClient
|
||||
.builder()
|
||||
.requestFactory(requestFactory)
|
||||
.baseUrl(config.gitea.baseUrl.trimEnd('/'))
|
||||
.defaultHeader("Authorization", "token ${config.git.token}")
|
||||
.build()
|
||||
@@ -178,4 +188,8 @@ class GiteaClient(
|
||||
val context: String? = null,
|
||||
val state: String? = null,
|
||||
)
|
||||
|
||||
companion object {
|
||||
private val REQUEST_TIMEOUT: Duration = Duration.ofSeconds(10)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.build.BuildResult
|
||||
import de.hoennig.gittally.build.BuildStatus
|
||||
import java.time.Instant
|
||||
|
||||
/** JSON statuses are lowercase like the legacy TSV/HTML statuses. */
|
||||
val BuildStatus.jsonName: String
|
||||
get() = name.lowercase()
|
||||
|
||||
data class BuildResultDto(
|
||||
val branch: String,
|
||||
val commit: String,
|
||||
val status: String,
|
||||
val startedAt: Instant,
|
||||
val durationSeconds: Long?,
|
||||
val artifactKey: String,
|
||||
) {
|
||||
companion object {
|
||||
fun from(result: BuildResult) =
|
||||
BuildResultDto(
|
||||
branch = result.branch,
|
||||
commit = result.commit,
|
||||
status = result.status.jsonName,
|
||||
startedAt = result.startedAt,
|
||||
durationSeconds = result.duration?.seconds,
|
||||
artifactKey = result.artifactKey,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** One entry of `GET /api/builds/current`; the log grows while the build runs. */
|
||||
data class CurrentBuildDto(
|
||||
val branch: String,
|
||||
val commit: String,
|
||||
val artifactKey: String,
|
||||
val status: String,
|
||||
val startedAt: Instant,
|
||||
val logSize: Long,
|
||||
)
|
||||
|
||||
/** Incremental live-log chunk; poll again with `offset = nextOffset`. */
|
||||
data class LogTailDto(
|
||||
val artifactKey: String,
|
||||
val offset: Long,
|
||||
val nextOffset: Long,
|
||||
val content: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* Effective status of a commit: the Gitea status when available, otherwise the
|
||||
* local repository status, otherwise `unknown`. A Gitea failure is an explicit
|
||||
* [giteaError] value — the request answers HTTP 200 and never hangs.
|
||||
*/
|
||||
data class CommitStatusDto(
|
||||
val commit: String,
|
||||
val status: String,
|
||||
val localStatus: String?,
|
||||
val giteaStatus: String?,
|
||||
val giteaError: String?,
|
||||
) {
|
||||
companion object {
|
||||
const val UNKNOWN_STATUS = "unknown"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.build.ArtifactStore
|
||||
import org.springframework.core.io.FileSystemResource
|
||||
import org.springframework.core.io.Resource
|
||||
import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.MediaTypeFactory
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.LinkOption
|
||||
import java.nio.file.Path
|
||||
|
||||
/**
|
||||
* Streams stored build artifacts. Status pages, JSON, and logs are served with
|
||||
* no-cache headers like legacy, so browsers always see the current build state.
|
||||
*/
|
||||
@RestController
|
||||
class ArtifactFileController(
|
||||
private val artifactStore: ArtifactStore,
|
||||
) {
|
||||
@GetMapping("/artifacts/{artifactKey}/{*path}")
|
||||
fun serve(
|
||||
@PathVariable artifactKey: String,
|
||||
@PathVariable path: String,
|
||||
): ResponseEntity<Resource> {
|
||||
val artifactDir =
|
||||
artifactStore.artifactDir(artifactKey)
|
||||
?: return ResponseEntity.notFound().build()
|
||||
val relativePath = path.removePrefix("/")
|
||||
if (relativePath.isBlank()) {
|
||||
return ResponseEntity.notFound().build()
|
||||
}
|
||||
val file = artifactDir.resolve(relativePath).normalize()
|
||||
if (!file.startsWith(artifactDir) || !Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
|
||||
return ResponseEntity.notFound().build()
|
||||
}
|
||||
val headers = HttpHeaders()
|
||||
headers.contentType = mediaType(file)
|
||||
if (file.extension() in NO_CACHE_EXTENSIONS) {
|
||||
headers.cacheControl = "no-store, max-age=0"
|
||||
headers.pragma = "no-cache"
|
||||
headers.expires = 0
|
||||
}
|
||||
return ResponseEntity.ok().headers(headers).body(FileSystemResource(file))
|
||||
}
|
||||
|
||||
private fun mediaType(file: Path): MediaType =
|
||||
when (file.extension()) {
|
||||
"log" -> MediaType(MediaType.TEXT_PLAIN, Charsets.UTF_8)
|
||||
else ->
|
||||
MediaTypeFactory
|
||||
.getMediaType(file.fileName.toString())
|
||||
.orElse(MediaType.APPLICATION_OCTET_STREAM)
|
||||
}
|
||||
|
||||
private fun Path.extension(): String = fileName.toString().substringAfterLast('.', "").lowercase()
|
||||
|
||||
companion object {
|
||||
private val NO_CACHE_EXTENSIONS = setOf("html", "json", "log")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.build.ArtifactStore
|
||||
import de.hoennig.gittally.build.BuildExecutor
|
||||
import de.hoennig.gittally.build.BuildResultRepository
|
||||
import de.hoennig.gittally.build.BuildStatus
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestHeader
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.StandardOpenOption
|
||||
|
||||
/**
|
||||
* JSON API over build results and running builds, replacing the legacy
|
||||
* `/control/…` endpoints. Mutating endpoints are guarded by the control token
|
||||
* (header [TOKEN_HEADER] or parameter `token`), like the legacy cancel token.
|
||||
*/
|
||||
@RestController
|
||||
class BuildsApiController(
|
||||
private val repository: BuildResultRepository,
|
||||
private val buildExecutor: BuildExecutor,
|
||||
private val artifactStore: ArtifactStore,
|
||||
private val controlTokens: ControlTokenService,
|
||||
) {
|
||||
@GetMapping("/api/builds/latest")
|
||||
fun latest(): List<BuildResultDto> = repository.latestPerBranch().map { BuildResultDto.from(it) }
|
||||
|
||||
@GetMapping("/api/builds/history")
|
||||
fun history(): List<BuildResultDto> = repository.history().map { BuildResultDto.from(it) }
|
||||
|
||||
/** The currently executing builds — several are possible, up to `builds.maxConcurrent`. */
|
||||
@GetMapping("/api/builds/current")
|
||||
fun current(): List<CurrentBuildDto> {
|
||||
val results = repository.history()
|
||||
return buildExecutor.currentBuilds().map { build ->
|
||||
CurrentBuildDto(
|
||||
branch = build.branch,
|
||||
commit = build.commit,
|
||||
artifactKey = build.artifactKey,
|
||||
status =
|
||||
(results.firstOrNull { it.artifactKey == build.artifactKey }?.status ?: BuildStatus.RUNNING)
|
||||
.jsonName,
|
||||
startedAt = build.startedAt,
|
||||
logSize = liveLogSize(build.liveLogFile),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Incremental live-log fetch of one running build; poll again with `offset = nextOffset`. */
|
||||
@GetMapping("/api/builds/current/{artifactKey}/log")
|
||||
fun currentLog(
|
||||
@PathVariable artifactKey: String,
|
||||
@RequestParam(defaultValue = "0") offset: Long,
|
||||
): ResponseEntity<Any> {
|
||||
val build =
|
||||
buildExecutor.currentBuilds().firstOrNull { it.artifactKey == artifactKey }
|
||||
?: return notFound("no running build with artifact key '$artifactKey'")
|
||||
return ResponseEntity.ok(readLogTail(artifactKey, build.liveLogFile, offset))
|
||||
}
|
||||
|
||||
/** Re-enqueues the branch's last recorded commit, like the legacy `/control/restart`. */
|
||||
@PostMapping("/api/builds/{branch}/restart")
|
||||
fun restart(
|
||||
@PathVariable branch: String,
|
||||
@RequestHeader(name = TOKEN_HEADER, required = false) headerToken: String?,
|
||||
@RequestParam(name = "token", required = false) paramToken: String?,
|
||||
): ResponseEntity<Any> {
|
||||
rejectBadToken(headerToken ?: paramToken)?.let { return it }
|
||||
val latest =
|
||||
repository.latestFor(branch)
|
||||
?: return notFound("branch '$branch' has no recorded build")
|
||||
val running = buildExecutor.startBuild(branch, latest.commit)
|
||||
return ResponseEntity.accepted().body(
|
||||
BuildResultDto(
|
||||
branch = running.branch,
|
||||
commit = running.commit,
|
||||
status = BuildStatus.PENDING.jsonName,
|
||||
startedAt = running.startedAt,
|
||||
durationSeconds = null,
|
||||
artifactKey = running.artifactKey,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/** Cancels by artifact key because multiple builds can run concurrently. */
|
||||
@PostMapping("/api/builds/{artifactKey}/cancel")
|
||||
fun cancel(
|
||||
@PathVariable artifactKey: String,
|
||||
@RequestHeader(name = TOKEN_HEADER, required = false) headerToken: String?,
|
||||
@RequestParam(name = "token", required = false) paramToken: String?,
|
||||
): ResponseEntity<Any> {
|
||||
rejectBadToken(headerToken ?: paramToken)?.let { return it }
|
||||
if (!buildExecutor.cancel(artifactKey)) {
|
||||
return notFound("no queued or running build with artifact key '$artifactKey'")
|
||||
}
|
||||
return ResponseEntity.accepted().body(mapOf("cancelled" to artifactKey))
|
||||
}
|
||||
|
||||
/** Removes the stored result and its artifact directory, like the legacy `/control/delete`. */
|
||||
@DeleteMapping("/api/builds/{artifactKey}")
|
||||
fun delete(
|
||||
@PathVariable artifactKey: String,
|
||||
@RequestHeader(name = TOKEN_HEADER, required = false) headerToken: String?,
|
||||
@RequestParam(name = "token", required = false) paramToken: String?,
|
||||
): ResponseEntity<Any> {
|
||||
rejectBadToken(headerToken ?: paramToken)?.let { return it }
|
||||
if (!repository.delete(artifactKey)) {
|
||||
return notFound("no build with artifact key '$artifactKey'")
|
||||
}
|
||||
artifactStore.prune(repository.history())
|
||||
return ResponseEntity.ok(mapOf("deleted" to artifactKey))
|
||||
}
|
||||
|
||||
private fun rejectBadToken(submittedToken: String?): ResponseEntity<Any>? =
|
||||
if (controlTokens.matches(submittedToken)) {
|
||||
null
|
||||
} else {
|
||||
ResponseEntity
|
||||
.status(HttpStatus.FORBIDDEN)
|
||||
.body(mapOf("error" to "missing or wrong control token"))
|
||||
}
|
||||
|
||||
private fun notFound(message: String): ResponseEntity<Any> = ResponseEntity.status(HttpStatus.NOT_FOUND).body(mapOf("error" to message))
|
||||
|
||||
private fun liveLogSize(liveLogFile: Path): Long = if (Files.isRegularFile(liveLogFile)) Files.size(liveLogFile) else 0L
|
||||
|
||||
/** At most [MAX_LOG_CHUNK] bytes per response; a chunk may split a multi-byte character. */
|
||||
private fun readLogTail(
|
||||
artifactKey: String,
|
||||
liveLogFile: Path,
|
||||
requestedOffset: Long,
|
||||
): LogTailDto {
|
||||
if (!Files.isRegularFile(liveLogFile)) {
|
||||
return LogTailDto(artifactKey, offset = 0, nextOffset = 0, content = "")
|
||||
}
|
||||
val size = Files.size(liveLogFile)
|
||||
val offset = requestedOffset.coerceIn(0L, size)
|
||||
val length = (size - offset).coerceAtMost(MAX_LOG_CHUNK).toInt()
|
||||
if (length == 0) {
|
||||
return LogTailDto(artifactKey, offset = offset, nextOffset = size, content = "")
|
||||
}
|
||||
FileChannel.open(liveLogFile, StandardOpenOption.READ).use { channel ->
|
||||
channel.position(offset)
|
||||
val buffer = ByteBuffer.allocate(length)
|
||||
while (buffer.hasRemaining() && channel.read(buffer) >= 0) {
|
||||
// keep reading until the requested chunk is complete or the file ends
|
||||
}
|
||||
buffer.flip()
|
||||
val content = String(buffer.array(), 0, buffer.limit(), Charsets.UTF_8)
|
||||
return LogTailDto(artifactKey, offset = offset, nextOffset = offset + buffer.limit(), content = content)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TOKEN_HEADER = "X-GitTally-Token"
|
||||
private const val MAX_LOG_CHUNK = 1024L * 1024L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.attribute.PosixFilePermissions
|
||||
import java.security.MessageDigest
|
||||
import java.security.SecureRandom
|
||||
|
||||
/**
|
||||
* Guards the mutating build endpoints with a shared secret, like the legacy cancel
|
||||
* token. The token is generated once and persisted (mode 600) so operators — and
|
||||
* the step-08 UI, server-side — can read it; delete the file to rotate it.
|
||||
*/
|
||||
class ControlTokenService(
|
||||
private val tokenFile: Path,
|
||||
) {
|
||||
@Synchronized
|
||||
fun token(): String {
|
||||
if (Files.isRegularFile(tokenFile)) {
|
||||
Files
|
||||
.readAllLines(tokenFile)
|
||||
.firstOrNull()
|
||||
?.trim()
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
?.let { return it }
|
||||
}
|
||||
val token = generateToken()
|
||||
Files.createDirectories(tokenFile.parent)
|
||||
Files.writeString(tokenFile, token + "\n")
|
||||
restrictToOwner(tokenFile)
|
||||
return token
|
||||
}
|
||||
|
||||
/** Constant-time comparison; null or blank never matches. */
|
||||
fun matches(submittedToken: String?): Boolean =
|
||||
!submittedToken.isNullOrBlank() &&
|
||||
MessageDigest.isEqual(submittedToken.toByteArray(), token().toByteArray())
|
||||
|
||||
/** 24 random bytes as hex, like legacy `openssl rand -hex 24`. */
|
||||
private fun generateToken(): String {
|
||||
val bytes = ByteArray(24)
|
||||
SecureRandom().nextBytes(bytes)
|
||||
return bytes.joinToString("") { "%02x".format(it) }
|
||||
}
|
||||
|
||||
private fun restrictToOwner(file: Path) {
|
||||
try {
|
||||
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-------"))
|
||||
} catch (_: UnsupportedOperationException) {
|
||||
// non-POSIX filesystem; the file stays with default permissions
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import java.nio.file.Paths
|
||||
|
||||
@Configuration
|
||||
class ServerConfiguration {
|
||||
/**
|
||||
* Token file relative to the working directory, matching how `ConfigLoader`
|
||||
* and the `BuildResultRepository` bean resolve their files. Nothing is written
|
||||
* until the first guarded request, so the bean is safe outside a git repository.
|
||||
*/
|
||||
@Bean
|
||||
fun controlTokenService(): ControlTokenService = ControlTokenService(Paths.get(".git/gittally/control-token"))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.watcher.Watcher
|
||||
import jakarta.annotation.PreDestroy
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent
|
||||
import org.springframework.context.annotation.Profile
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* Starts the watcher poll loop once the server context is ready and stops it on
|
||||
* shutdown. Only in the `server` profile — CLI commands and tests never start
|
||||
* the loop (see [Watcher]).
|
||||
*/
|
||||
@Component
|
||||
@Profile("server")
|
||||
class ServerWatcherLifecycle(
|
||||
private val watcher: Watcher,
|
||||
) {
|
||||
@EventListener(ApplicationReadyEvent::class)
|
||||
fun onApplicationReady() {
|
||||
watcher.start()
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
fun onShutdown() {
|
||||
watcher.stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.build.BuildResultRepository
|
||||
import de.hoennig.gittally.gitea.GiteaClient
|
||||
import de.hoennig.gittally.gitea.GiteaStatusResult
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
/**
|
||||
* Replaces the legacy `/control/status` proxy. The response always answers with
|
||||
* HTTP 200 and an explicit status — a Gitea failure becomes `giteaError` plus the
|
||||
* local fallback (or `unknown`), never a hanging request (the [GiteaClient]
|
||||
* requests time out).
|
||||
*/
|
||||
@RestController
|
||||
class StatusApiController(
|
||||
private val repository: BuildResultRepository,
|
||||
private val giteaClient: GiteaClient,
|
||||
) {
|
||||
@GetMapping("/api/status/{commit}")
|
||||
fun status(
|
||||
@PathVariable commit: String,
|
||||
): ResponseEntity<Any> {
|
||||
if (!COMMIT_PATTERN.matches(commit)) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(mapOf("error" to "'$commit' is not an abbreviated or full commit hash"))
|
||||
}
|
||||
val localStatus =
|
||||
repository
|
||||
.history()
|
||||
.firstOrNull { it.commit.startsWith(commit, ignoreCase = true) }
|
||||
?.status
|
||||
val giteaResult = giteaClient.readStatus(commit)
|
||||
val giteaStatus = (giteaResult as? GiteaStatusResult.Found)?.status
|
||||
val effective = giteaStatus ?: localStatus
|
||||
return ResponseEntity.ok(
|
||||
CommitStatusDto(
|
||||
commit = commit,
|
||||
status = effective?.jsonName ?: CommitStatusDto.UNKNOWN_STATUS,
|
||||
localStatus = localStatus?.jsonName,
|
||||
giteaStatus = giteaStatus?.jsonName,
|
||||
giteaError = (giteaResult as? GiteaStatusResult.Error)?.message,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** Like the legacy handler: 7 to 40 hex digits. */
|
||||
private val COMMIT_PATTERN = Regex("[0-9a-fA-F]{7,40}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.hoennig.gittally.server
|
||||
|
||||
import de.hoennig.gittally.watcher.Watcher
|
||||
import de.hoennig.gittally.watcher.WatcherState
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
class WatcherApiController(
|
||||
private val watcher: Watcher,
|
||||
) {
|
||||
/** Watcher health: whether the loop runs, last poll, last fetch/poll error. */
|
||||
@GetMapping("/api/watcher")
|
||||
fun watcher(): WatcherState = watcher.state()
|
||||
}
|
||||
Reference in New Issue
Block a user