feat(practice): add the code runner port and its TLS client

This commit is contained in:
shira committed 2026-10-11 09:48:44 +03:00
1 parent 53a125f2b5
commit facc1789b1
24 files changed
+3374 -6

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
package bootstrap
import (
"fmt"
"log/slog"
"os"
"lincodex/backend/config"
coderunner "lincodex/backend/internal/adapter/secondary/code_runner"
coderunnerdisabled "lincodex/backend/internal/adapter/secondary/code_runner/disabled"
"lincodex/backend/internal/port/outbound"
)
// newCodeRunner binds the CodeRunner port. An unset CODE_RUNNER_URL selects
// the disabled runner, which reports every request as unavailable and never as
// a verdict. A set URL selects the TLS client, and a failure to build it stops
// startup instead of falling back to the disabled runner. The constructor
// makes no network call, so startup does not depend on the runner.
func newCodeRunner(settings config.Config, logger *slog.Logger) (outbound.CodeRunner, error) {
runner := settings.CodeRunner
if err := runner.Validate(); err != nil {
return nil, fmt.Errorf("bootstrap.newCodeRunner - validate settings: %w", err)
}
if !runner.Enabled() {
logger.Info("code runner selected", slog.Bool("enabled", false))
logger.Warn("code runner is disabled; code practice runs nothing and every request is reported as unavailable")
return coderunnerdisabled.NewRunner(), nil
}
caPEM, err := codeRunnerCA(runner)
if err != nil {
return nil, fmt.Errorf("bootstrap.newCodeRunner - read CA: %w", err)
}
client, err := coderunner.NewClient(coderunner.Settings{
BaseURL: runner.URL,
Token: runner.Token,
RunKey: runner.RunKey,
CAPEM: caPEM,
ServerName: runner.ServerName,
RequestTimeout: runner.RequestTimeout,
})
if err != nil {
return nil, fmt.Errorf("bootstrap.newCodeRunner - create client: %w", err)
}
// Neither the URL nor any secret is logged.
logger.Info("code runner selected", slog.Bool("enabled", true))
return client, nil
}
// codeRunnerCA returns the CA PEM from the inline setting or the file.
func codeRunnerCA(runner config.CodeRunnerSettings) ([]byte, error) {
if runner.CAPEM != "" {
return []byte(runner.CAPEM), nil
}
pemBytes, err := os.ReadFile(runner.CAFile)
if err != nil {
return nil, fmt.Errorf("CODE_RUNNER_CA_FILE is not readable: %w", err)
}
return pemBytes, nil
}