Run and lint solutions on the site
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]>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// Package handlers is the site's REST API: the problem list and pages (public
|
||||
// for solved problems, everything for the admin), the admin's edits, the
|
||||
// Claude explanations (explain.go) and recording reviews (review.go), and
|
||||
// the Gitea webhook that tells the server to pull.
|
||||
// Claude explanations (explain.go) and recording reviews (review.go), the
|
||||
// solve page's code editing and runs (run.go), and the Gitea webhook that
|
||||
// tells the server to pull.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
@@ -43,6 +44,9 @@ type Server struct {
|
||||
// Pull asks for a sync. It must not block: the webhook answers at once
|
||||
// and the sync runs in the background.
|
||||
Pull func()
|
||||
// Dir is the clone's work tree. The solve page reads solutions and tests
|
||||
// from it rather than the index, which truncates big files.
|
||||
Dir string
|
||||
// Claude writes the explanations; nil when CLAUDE_API_KEY is not set,
|
||||
// which turns only that feature off.
|
||||
Claude explain.Asker
|
||||
@@ -55,6 +59,9 @@ type Server struct {
|
||||
// Audio keeps review recordings for playback; nil when the S3_*
|
||||
// settings are not given, and the recordings are then thrown away.
|
||||
Audio AudioStore
|
||||
// Runner compiles, runs and lints code for the solve page; nil when
|
||||
// RUNNER_URL is not set, which turns only running and linting off.
|
||||
Runner Runner
|
||||
|
||||
idx atomic.Pointer[index.Index]
|
||||
// Separate, so a review and an explanation of one problem can run at
|
||||
@@ -97,6 +104,10 @@ func (s *Server) Routes(api *gin.RouterGroup) {
|
||||
admin.GET("/problems/:name/review", s.reviewStatus)
|
||||
admin.GET("/problems/:name/reviews/:id/audio", s.reviewAudio)
|
||||
admin.DELETE("/problems/:name/reviews/:id", s.deleteReview)
|
||||
admin.GET("/problems/:name/code", s.getCode)
|
||||
admin.PUT("/problems/:name/code", s.saveCode)
|
||||
admin.POST("/problems/:name/run", s.run)
|
||||
admin.POST("/problems/:name/lint", s.lint)
|
||||
api.POST("/hooks/gitea", s.webhook)
|
||||
}
|
||||
|
||||
@@ -202,15 +213,21 @@ func (e *requestError) Error() string { return e.msg }
|
||||
func (s *Server) mutate(ctx *gin.Context, name, message string, edit func(dir string) error) {
|
||||
c, cancel := context.WithTimeout(ctx.Request.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
err := s.Repo.Mutate(c, message, edit)
|
||||
if err := s.Repo.Mutate(c, message, edit); err != nil {
|
||||
editFailed(ctx, message, err)
|
||||
return
|
||||
}
|
||||
if p, ok := s.Index().Problems[name]; ok {
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
} else {
|
||||
ctx.JSON(http.StatusOK, gin.H{})
|
||||
}
|
||||
}
|
||||
|
||||
// editFailed answers for an edit Mutate refused or could not save.
|
||||
func editFailed(ctx *gin.Context, message string, err error) {
|
||||
var re *requestError
|
||||
switch {
|
||||
case err == nil:
|
||||
if p, ok := s.Index().Problems[name]; ok {
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
} else {
|
||||
ctx.JSON(http.StatusOK, gin.H{})
|
||||
}
|
||||
case errors.As(err, &re):
|
||||
ctx.JSON(re.status, gin.H{"error": re.msg})
|
||||
case errors.Is(err, repo.ErrRejected):
|
||||
|
||||
Reference in New Issue
Block a user