Files
usaco/site/internal/handlers/daily.go
T
adamfandClaude Opus 5.5 3a8f651a6f
Test and publish the site / test (push) Successful in 1m9s
Test and publish the site / publish (push) Successful in 1m31s
Import LeetCode problems from a pasted link on the site
The problem list's admin row gains a link box beside "Today's LeetCode":
POST /leetcode/import takes {url} and does what `usaco new
<leetcode-link>` does, through the same lib/leetcode.py run and "Start
<slug>" commit as the daily question, then opens the solve page. A
problem already in the repo is only opened.

leetcode.Slug reads the link as slug_of in lib/leetcode.py does: full
links with /description/ and query strings, www., no scheme, or a bare
slug. Anything else is a 400.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 13:02:11 +01:00

96 lines
2.7 KiB
Go

package handlers
import (
"context"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"adam-french.co.uk/usaco-site/internal/leetcode"
)
// LeetCode is the part of leetcode.Importer the handlers use, so tests can
// fake LeetCode and the import script.
type LeetCode interface {
Daily(ctx context.Context) (string, error)
Import(ctx context.Context, root, slug string) error
}
func (s *Server) leetcode() LeetCode {
if s.LeetCode != nil {
return s.LeetCode
}
return &leetcode.Importer{}
}
// startDaily is `usaco daily`: it imports today's LeetCode daily question
// into solving/ and commits it, and answers its name for the page to open.
func (s *Server) startDaily(ctx *gin.Context) {
c, cancel := context.WithTimeout(ctx.Request.Context(), 60*time.Second)
defer cancel()
lc := s.leetcode()
slug, err := lc.Daily(c)
if err != nil {
log.Printf("[daily] %v", err)
ctx.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
// The slug becomes a directory name.
if !validName.MatchString(slug) {
ctx.JSON(http.StatusBadGateway, gin.H{"error": "LeetCode named the daily question " + slug + ", which is not a usable name"})
return
}
s.startLeetCode(ctx, c, lc, slug)
}
// startImport is `usaco new <leetcode-link>`: the same import, for the
// problem a pasted link names.
func (s *Server) startImport(ctx *gin.Context) {
var body struct {
URL string `json:"url"`
}
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid JSON"})
return
}
slug, err := leetcode.Slug(body.URL)
if err != nil || !validName.MatchString(slug) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "not a LeetCode problem link"})
return
}
c, cancel := context.WithTimeout(ctx.Request.Context(), 60*time.Second)
defer cancel()
s.startLeetCode(ctx, c, s.leetcode(), slug)
}
// startLeetCode imports slug into solving/, commits it as "Start <slug>" and
// answers {name, created}. A problem already in the repo (started yesterday
// from the CLI, or solved months ago) is only named, never imported again.
func (s *Server) startLeetCode(ctx *gin.Context, c context.Context, lc LeetCode, slug string) {
if idx := s.Index(); idx != nil {
if _, ok := idx.Problems[slug]; ok {
ctx.JSON(http.StatusOK, gin.H{"name": slug, "created": false})
return
}
}
created := false
err := s.Repo.Mutate(c, "Start "+slug, func(dir string) error {
if _, err := findOnDisk(dir, slug); err == nil {
created = false
return nil
}
created = true
if err := lc.Import(c, dir, slug); err != nil {
return &requestError{http.StatusBadGateway, err.Error()}
}
return nil
})
if err != nil {
editFailed(ctx, "Start "+slug, err)
return
}
ctx.JSON(http.StatusOK, gin.H{"name": slug, "created": created})
}