Files
cpu-benchmarker-server/lib/web/app.go

705 lines
16 KiB
Go
Raw Normal View History

package web
2026-04-15 19:09:21 +03:00
import (
"encoding/json"
"fmt"
"html/template"
"io"
2026-04-17 13:25:48 +03:00
"log"
2026-04-15 19:09:21 +03:00
"mime"
"net/http"
"net/url"
2026-04-17 13:25:48 +03:00
"os"
"path/filepath"
2026-04-15 19:09:21 +03:00
"strconv"
"strings"
2026-04-17 13:25:48 +03:00
"sync"
2026-04-15 19:09:21 +03:00
"time"
"cpu-benchmark-server/lib/model"
"cpu-benchmark-server/lib/store"
2026-04-15 19:09:21 +03:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
const maxSubmissionBytes = 4 << 20
type App struct {
store *store.Store
2026-04-15 19:09:21 +03:00
templates *template.Template
pageSize int
2026-04-17 13:25:48 +03:00
errorLog *dailyErrorLogger
2026-04-15 19:09:21 +03:00
}
type indexPageData struct {
Submissions []model.Submission
QueryText string
QueryCPU string
QueryThread string
QueryPlatform string
QuerySort string
QueryIntensity int
QueryDuration int
Page int
TotalPages int
TotalCount int
ShowingFrom int
ShowingTo int
PrevURL string
NextURL string
2026-04-15 19:09:21 +03:00
}
type jsonSubmissionEnvelope struct {
2026-04-17 13:25:48 +03:00
Submitter string `json:"submitter"`
Platform string `json:"platform"`
Benchmark json.RawMessage `json:"benchmark"`
Result json.RawMessage `json:"result"`
Data json.RawMessage `json:"data"`
2026-04-15 19:09:21 +03:00
}
type flatSubmissionEnvelope struct {
Submitter string `json:"submitter"`
Platform string `json:"platform"`
model.BenchmarkResult
2026-04-15 19:09:21 +03:00
}
type errorResponse struct {
Error string `json:"error"`
}
2026-04-17 13:25:48 +03:00
func New(store *store.Store, pageSize int, errorLogDir string) (*App, error) {
2026-04-15 19:09:21 +03:00
funcs := template.FuncMap{
"formatInt64": formatInt64,
"formatFloat": formatFloat,
"formatTime": formatTime,
"modeLabel": model.ThreadModeLabel,
2026-04-15 19:09:21 +03:00
}
templates, err := template.New("index.html").Funcs(funcs).ParseFiles("templates/index.html")
if err != nil {
return nil, err
}
2026-04-17 13:25:48 +03:00
errorLog, err := newDailyErrorLogger(errorLogDir)
if err != nil {
return nil, err
}
2026-04-15 19:09:21 +03:00
return &App{
store: store,
templates: templates,
pageSize: pageSize,
2026-04-17 13:25:48 +03:00
errorLog: errorLog,
2026-04-15 19:09:21 +03:00
}, nil
}
func (a *App) Routes() http.Handler {
router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
2026-04-17 13:25:48 +03:00
router.Use(a.errorLoggingMiddleware)
2026-04-15 19:09:21 +03:00
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.Timeout(30 * time.Second))
router.Get("/", a.handleIndex)
router.Get("/healthz", a.handleHealth)
router.Get("/api/search", a.handleSearch)
router.Post("/api/submit", a.handleSubmit)
return router
}
2026-04-17 13:25:48 +03:00
func (a *App) Close() error {
if a == nil || a.errorLog == nil {
return nil
}
return a.errorLog.Close()
}
type dailyErrorLogger struct {
dir string
mu sync.Mutex
date string
file *os.File
logger *log.Logger
}
const maxLoggedResponseBodyBytes = 2048
func newDailyErrorLogger(dir string) (*dailyErrorLogger, error) {
l := &dailyErrorLogger{dir: dir}
if err := l.rotateIfNeeded(time.Now()); err != nil {
return nil, err
}
return l, nil
}
func (l *dailyErrorLogger) rotateIfNeeded(now time.Time) error {
date := now.Format("2006-01-02")
if l.file != nil && l.date == date {
return nil
}
if err := os.MkdirAll(l.dir, 0o755); err != nil {
return err
}
path := filepath.Join(l.dir, date+".error.log")
file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return err
}
if l.file != nil {
_ = l.file.Close()
}
l.date = date
l.file = file
l.logger = log.New(file, "", log.LstdFlags)
return nil
}
func (l *dailyErrorLogger) LogRequest(r *http.Request, status int, duration time.Duration, responseBody string) {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
if err := l.rotateIfNeeded(now); err != nil {
log.Printf("error log rotation failed: %v", err)
return
}
l.logger.Printf(
`status=%d method=%s path=%q query=%q ip=%q request_id=%q duration=%s content_type=%q content_length=%d user_agent=%q response_body=%q`,
status,
r.Method,
r.URL.Path,
r.URL.RawQuery,
r.RemoteAddr,
middleware.GetReqID(r.Context()),
duration,
r.Header.Get("Content-Type"),
r.ContentLength,
r.UserAgent(),
responseBody,
)
}
func (l *dailyErrorLogger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.file == nil {
return nil
}
err := l.file.Close()
l.file = nil
l.logger = nil
l.date = ""
return err
}
type statusCapturingResponseWriter struct {
http.ResponseWriter
status int
body strings.Builder
bodyTruncated bool
}
func (w *statusCapturingResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *statusCapturingResponseWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = http.StatusOK
}
if w.body.Len() < maxLoggedResponseBodyBytes {
remaining := maxLoggedResponseBodyBytes - w.body.Len()
if len(b) > remaining {
_, _ = w.body.Write(b[:remaining])
w.bodyTruncated = true
} else {
_, _ = w.body.Write(b)
}
} else {
w.bodyTruncated = true
}
return w.ResponseWriter.Write(b)
}
func (w *statusCapturingResponseWriter) LoggedBody() string {
body := strings.TrimSpace(w.body.String())
if body == "" {
return ""
}
if w.bodyTruncated {
return body + "... [truncated]"
}
return body
}
func (a *App) errorLoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startedAt := time.Now()
ww := &statusCapturingResponseWriter{ResponseWriter: w}
next.ServeHTTP(ww, r)
status := ww.status
if status == 0 {
status = http.StatusOK
}
if status >= http.StatusBadRequest {
a.errorLog.LogRequest(r, status, time.Since(startedAt), ww.LoggedBody())
}
})
}
2026-04-15 19:09:21 +03:00
func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
page := parsePositiveInt(r.URL.Query().Get("page"), 1)
text := strings.TrimSpace(r.URL.Query().Get("text"))
cpu := strings.TrimSpace(r.URL.Query().Get("cpu"))
thread := strings.TrimSpace(r.URL.Query().Get("thread"))
platform := strings.TrimSpace(r.URL.Query().Get("platform"))
sortBy := normalizeSortFilter(r.URL.Query().Get("sort"))
intensity := parsePositiveInt(r.URL.Query().Get("intensity"), 0)
durationSecs := parsePositiveInt(r.URL.Query().Get("durationSecs"), 0)
2026-04-15 19:09:21 +03:00
var (
submissions []model.Submission
2026-04-15 19:09:21 +03:00
totalCount int
)
if text != "" || cpu != "" || thread != "" || platform != "" || intensity > 0 || durationSecs > 0 || sortBy != "newest" {
matches := a.store.SearchSubmissions(text, cpu, thread, platform, sortBy, intensity, durationSecs)
2026-04-15 19:09:21 +03:00
totalCount = len(matches)
start, end, normalizedPage := pageBounds(page, a.pageSize, totalCount)
page = normalizedPage
submissions = matches[start:end]
} else {
var count int
submissions, count = a.store.ListSubmissions(page, a.pageSize)
totalCount = count
_, _, page = pageBounds(page, a.pageSize, totalCount)
}
totalPageCount := totalPages(totalCount, a.pageSize)
showingFrom, showingTo := visibleBounds(page, a.pageSize, len(submissions), totalCount)
2026-04-15 19:09:21 +03:00
data := indexPageData{
Submissions: submissions,
QueryText: text,
QueryCPU: cpu,
QueryThread: normalizeThreadFilter(thread),
QueryPlatform: normalizePlatformFilter(platform),
QuerySort: sortBy,
QueryIntensity: intensity,
QueryDuration: durationSecs,
Page: page,
TotalPages: totalPageCount,
TotalCount: totalCount,
ShowingFrom: showingFrom,
ShowingTo: showingTo,
PrevURL: buildIndexURL(max(1, page-1), text, cpu, thread, platform, sortBy, intensity, durationSecs),
NextURL: buildIndexURL(max(1, min(totalPageCount, page+1)), text, cpu, thread, platform, sortBy, intensity, durationSecs),
2026-04-15 19:09:21 +03:00
}
if err := a.templates.ExecuteTemplate(w, "index.html", data); err != nil {
http.Error(w, fmt.Sprintf("render template: %v", err), http.StatusInternalServerError)
}
}
func (a *App) handleHealth(w http.ResponseWriter, _ *http.Request) {
2026-04-15 19:09:21 +03:00
writeJSON(w, http.StatusOK, map[string]any{
"status": "ok",
"submissions": a.store.Count(),
})
}
func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) {
results := a.store.SearchSubmissions(
r.URL.Query().Get("text"),
r.URL.Query().Get("cpu"),
r.URL.Query().Get("thread"),
r.URL.Query().Get("platform"),
r.URL.Query().Get("sort"),
parsePositiveInt(r.URL.Query().Get("intensity"), 0),
parsePositiveInt(r.URL.Query().Get("durationSecs"), 0),
)
2026-04-15 19:09:21 +03:00
writeJSON(w, http.StatusOK, results)
}
func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxSubmissionBytes)
result, submitter, platform, err := parseSubmissionRequest(r)
2026-04-15 19:09:21 +03:00
if err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
return
}
if err := result.Validate(); err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
return
}
platform = model.NormalizePlatform(platform)
if err := model.ValidatePlatform(platform); err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
return
}
submission, err := a.store.SaveSubmission(result, submitter, platform)
2026-04-15 19:09:21 +03:00
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{Error: fmt.Sprintf("store submission: %v", err)})
return
}
writeJSON(w, http.StatusCreated, map[string]any{
"success": true,
"submissionID": submission.SubmissionID,
"submitter": submission.Submitter,
"platform": submission.Platform,
2026-04-15 19:09:21 +03:00
"submittedAt": submission.SubmittedAt,
})
}
func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, string, error) {
2026-04-15 19:09:21 +03:00
contentType := r.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil && contentType != "" {
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse content type: %w", err)
2026-04-15 19:09:21 +03:00
}
switch mediaType {
case "", "application/json":
return parseJSONSubmission(r)
case "multipart/form-data":
return parseMultipartSubmission(r)
default:
return model.BenchmarkResult{}, "", "", fmt.Errorf("unsupported content type %q", mediaType)
2026-04-15 19:09:21 +03:00
}
}
func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) {
2026-04-15 19:09:21 +03:00
body, err := io.ReadAll(r.Body)
if err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("read request body: %w", err)
2026-04-15 19:09:21 +03:00
}
submitter := firstNonEmpty(
r.URL.Query().Get("submitter"),
r.Header.Get("X-Submitter"),
)
platform := firstNonEmpty(
r.URL.Query().Get("platform"),
r.Header.Get("X-Platform"),
)
2026-04-15 19:09:21 +03:00
var nested jsonSubmissionEnvelope
if err := json.Unmarshal(body, &nested); err == nil {
submitter = firstNonEmpty(nested.Submitter, submitter)
platform = firstNonEmpty(nested.Platform, platform)
2026-04-17 13:25:48 +03:00
for _, candidate := range []struct {
name string
payload json.RawMessage
}{
{name: "benchmark", payload: nested.Benchmark},
{name: "result", payload: nested.Result},
{name: "data", payload: nested.Data},
} {
if len(candidate.payload) == 0 || string(candidate.payload) == "null" {
continue
}
var result model.BenchmarkResult
if err := json.Unmarshal(candidate.payload, &result); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode %s JSON: %w", candidate.name, err)
2026-04-15 19:09:21 +03:00
}
2026-04-17 13:25:48 +03:00
return result, submitter, platform, nil
2026-04-15 19:09:21 +03:00
}
}
var flat flatSubmissionEnvelope
if err := json.Unmarshal(body, &flat); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err)
2026-04-15 19:09:21 +03:00
}
return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), firstNonEmpty(flat.Platform, platform, "windows"), nil
2026-04-15 19:09:21 +03:00
}
func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) {
2026-04-15 19:09:21 +03:00
if err := r.ParseMultipartForm(maxSubmissionBytes); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse multipart form: %w", err)
2026-04-15 19:09:21 +03:00
}
payload, err := readMultipartPayload(r)
if err != nil {
return model.BenchmarkResult{}, "", "", err
2026-04-15 19:09:21 +03:00
}
var result model.BenchmarkResult
2026-04-15 19:09:21 +03:00
if err := json.Unmarshal(payload, &result); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err)
2026-04-15 19:09:21 +03:00
}
return result, r.FormValue("submitter"), firstNonEmpty(r.FormValue("platform"), "windows"), nil
2026-04-15 19:09:21 +03:00
}
func readMultipartPayload(r *http.Request) ([]byte, error) {
for _, field := range []string{"benchmark", "file", "benchmarkFile"} {
2026-04-15 19:09:21 +03:00
file, _, err := r.FormFile(field)
if err == nil {
defer file.Close()
payload, readErr := io.ReadAll(file)
if readErr != nil {
return nil, fmt.Errorf("read multipart benchmark file: %w", readErr)
}
return payload, nil
}
if err != http.ErrMissingFile {
return nil, fmt.Errorf("read multipart benchmark file: %w", err)
}
}
for _, field := range []string{"benchmark", "payload", "result", "data"} {
2026-04-15 19:09:21 +03:00
if value := strings.TrimSpace(r.FormValue(field)); value != "" {
return []byte(value), nil
}
}
return nil, fmt.Errorf("multipart request must include benchmark JSON in a file field or text field named benchmark")
}
func parsePositiveInt(raw string, fallback int) int {
if raw == "" {
return fallback
}
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
return fallback
}
return value
}
func pageBounds(page, pageSize, total int) (int, int, int) {
if pageSize <= 0 {
pageSize = 50
}
totalPages := totalPages(total, pageSize)
if totalPages == 0 {
return 0, 0, 1
}
if page < 1 {
page = 1
}
if page > totalPages {
page = totalPages
}
start := (page - 1) * pageSize
end := min(total, start+pageSize)
return start, end, page
}
func totalPages(total, pageSize int) int {
if total == 0 || pageSize <= 0 {
return 0
}
pages := total / pageSize
if total%pageSize != 0 {
pages++
}
return pages
}
func visibleBounds(page, pageSize, visibleCount, total int) (int, int) {
if total == 0 || visibleCount == 0 {
return 0, 0
}
from := (page-1)*pageSize + 1
to := from + visibleCount - 1
return from, to
}
func buildIndexURL(page int, text, cpu, thread, platform, sortBy string, intensity, durationSecs int) string {
2026-04-15 19:09:21 +03:00
if page < 1 {
page = 1
}
values := url.Values{}
values.Set("page", strconv.Itoa(page))
if strings.TrimSpace(text) != "" {
values.Set("text", text)
}
if strings.TrimSpace(cpu) != "" {
values.Set("cpu", cpu)
}
if normalizedThread := normalizeThreadFilter(thread); normalizedThread != "" {
values.Set("thread", normalizedThread)
}
if normalizedPlatform := normalizePlatformFilter(platform); normalizedPlatform != "" {
values.Set("platform", normalizedPlatform)
}
if normalizedSort := normalizeSortFilter(sortBy); normalizedSort != "newest" {
values.Set("sort", normalizedSort)
}
if intensity > 0 {
values.Set("intensity", strconv.Itoa(intensity))
}
if durationSecs > 0 {
values.Set("durationSecs", strconv.Itoa(durationSecs))
}
2026-04-15 19:09:21 +03:00
return "/?" + values.Encode()
}
func writeJSON(w http.ResponseWriter, status int, payload any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
_ = encoder.Encode(payload)
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if trimmed := strings.TrimSpace(value); trimmed != "" {
return trimmed
}
}
return ""
}
func normalizeThreadFilter(value string) string {
switch strings.ToLower(strings.TrimSpace(value)) {
case "single":
return "single"
case "multi":
return "multi"
default:
return ""
}
}
func normalizePlatformFilter(value string) string {
switch strings.ToLower(strings.TrimSpace(value)) {
case "windows":
return "windows"
case "linux":
return "linux"
case "macos":
return "macos"
default:
return ""
}
}
func normalizeSortFilter(value string) string {
switch strings.ToLower(strings.TrimSpace(value)) {
case "", "newest":
return "newest"
case "oldest":
return "oldest"
case "score_desc":
return "score_desc"
case "score_asc":
return "score_asc"
case "mops_desc":
return "mops_desc"
case "mops_asc":
return "mops_asc"
default:
return "newest"
}
}
2026-04-15 19:09:21 +03:00
func formatInt64(value int64) string {
negative := value < 0
if negative {
value = -value
}
digits := strconv.FormatInt(value, 10)
if len(digits) <= 3 {
if negative {
return "-" + digits
}
return digits
}
var builder strings.Builder
if negative {
builder.WriteByte('-')
}
pre := len(digits) % 3
if pre > 0 {
builder.WriteString(digits[:pre])
if len(digits) > pre {
builder.WriteByte(',')
}
}
for i := pre; i < len(digits); i += 3 {
builder.WriteString(digits[i : i+3])
if i+3 < len(digits) {
builder.WriteByte(',')
}
}
return builder.String()
}
func formatFloat(value float64) string {
return fmt.Sprintf("%.2f", value)
}
func formatTime(value time.Time) string {
if value.IsZero() {
return "-"
}
return value.Format("2006-01-02 15:04:05 MST")
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}