A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.
Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.
C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.
CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
// Command usacorunner compiles and runs solutions against their tests, and
|
|
// lints them, for the USACO site's solve page (internal/runner). It runs
|
|
// untrusted code, so it is its own service: the site holds secrets and
|
|
// this holds none, and in production its container has no outside network,
|
|
// a read-only root, an exec-able tmpfs at /tmp and tight memory, CPU and
|
|
// pids limits (Dockerfile.runner lists them). Only the site talks to it.
|
|
//
|
|
// It reads PORT (default 8081) and MAX_PARALLEL (default 2): how many runs
|
|
// and lints may be in progress at once. More only makes each slower, and
|
|
// a timing that depends on the neighbours is no use for a time limit.
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"adam-french.co.uk/usaco-site/internal/runner"
|
|
)
|
|
|
|
func main() {
|
|
maxParallel := 2
|
|
if v := os.Getenv("MAX_PARALLEL"); v != "" {
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil || n < 1 {
|
|
log.Fatalf("MAX_PARALLEL=%q is not a positive number", v)
|
|
}
|
|
maxParallel = n
|
|
}
|
|
|
|
// The compilers are what the image is for: without g++ or python3 the
|
|
// runner still answers, but every run in that language fails to build.
|
|
var have, missing []string
|
|
for bin, ok := range runner.Tools() {
|
|
if ok {
|
|
have = append(have, bin)
|
|
} else {
|
|
missing = append(missing, bin)
|
|
}
|
|
}
|
|
sort.Strings(have)
|
|
sort.Strings(missing)
|
|
log.Printf("[runner] tools: %s", strings.Join(have, ", "))
|
|
if len(missing) > 0 {
|
|
log.Printf("[runner] not installed: %s", strings.Join(missing, ", "))
|
|
}
|
|
|
|
addr := ":" + getenv("PORT", "8081")
|
|
srv := &http.Server{
|
|
Addr: addr,
|
|
Handler: runner.Handler(maxParallel),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
// Above the runner's own 60s budget per request, which is what
|
|
// actually bounds a response.
|
|
WriteTimeout: 75 * time.Second,
|
|
}
|
|
log.Printf("listening on %s, %d at a time", addr, maxParallel)
|
|
log.Fatal(srv.ListenAndServe())
|
|
}
|
|
|
|
func getenv(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|