refactor(main): delegate setup to config, store, and web packages

Replace in-file bootstrap logic with package-level constructors in `run`:
- use `config.Load()` instead of local env parsing/AppConfig helpers
- use `store.Open()` and `web.New()` for persistence and app wiring
- rename local store variable to `benchmarkStore` for clarity

This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.refactor(main): delegate setup to config, store, and web packages

Replace in-file bootstrap logic with package-level constructors in `run`:
- use `config.Load()` instead of local env parsing/AppConfig helpers
- use `store.Open()` and `web.New()` for persistence and app wiring
- rename local store variable to `benchmarkStore` for clarity

This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.
This commit is contained in:
2026-04-15 19:20:27 +03:00
parent c2572c5702
commit 7af0778047
5 changed files with 204 additions and 145 deletions

59
lib/config/config.go Normal file
View File

@@ -0,0 +1,59 @@
package config
import (
"os"
"strconv"
"time"
)
type Config struct {
Addr string
BadgerDir string
PageSize int
ShutdownTimeout time.Duration
}
func Load() Config {
return Config{
Addr: envOrDefault("APP_ADDR", ":8080"),
BadgerDir: envOrDefault("BADGER_DIR", "data/badger"),
PageSize: envIntOrDefault("PAGE_SIZE", 50),
ShutdownTimeout: envDurationOrDefault("SHUTDOWN_TIMEOUT", 10*time.Second),
}
}
func envOrDefault(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func envIntOrDefault(key string, fallback int) int {
value := os.Getenv(key)
if value == "" {
return fallback
}
parsed, err := strconv.Atoi(value)
if err != nil || parsed <= 0 {
return fallback
}
return parsed
}
func envDurationOrDefault(key string, fallback time.Duration) time.Duration {
value := os.Getenv(key)
if value == "" {
return fallback
}
parsed, err := time.ParseDuration(value)
if err != nil || parsed <= 0 {
return fallback
}
return parsed
}

141
lib/model/submission.go Normal file
View File

@@ -0,0 +1,141 @@
package model
import (
"errors"
"fmt"
"strings"
"time"
)
type BenchmarkConfig struct {
DurationSecs int `json:"durationSecs"`
Intensity int `json:"intensity"`
CoreFilter int `json:"coreFilter"`
MultiCore bool `json:"multiCore"`
}
type CPUInfo struct {
BrandString string `json:"brandString"`
VendorID string `json:"vendorID"`
PhysicalCores int `json:"physicalCores"`
LogicalCores int `json:"logicalCores"`
BaseClockMHz int `json:"baseClockMHz"`
BoostClockMHz int `json:"boostClockMHz"`
L1DataKB int `json:"l1DataKB"`
L2KB int `json:"l2KB"`
L3MB int `json:"l3MB"`
IsHybrid bool `json:"isHybrid,omitempty"`
Has3DVCache bool `json:"has3DVCache,omitempty"`
PCoreCount int `json:"pCoreCount,omitempty"`
ECoreCount int `json:"eCoreCount,omitempty"`
Cores []CPUCoreDescriptor `json:"cores,omitempty"`
SupportedFeatures []string `json:"supportedFeatures,omitempty"`
}
type CPUCoreDescriptor struct {
LogicalID int `json:"LogicalID"`
PhysicalID int `json:"PhysicalID"`
CoreID int `json:"CoreID"`
Type int `json:"Type"`
}
type CoreResult struct {
LogicalID int `json:"logicalID"`
CoreType string `json:"coreType"`
MOpsPerSec float64 `json:"mOpsPerSec"`
TotalOps int64 `json:"totalOps"`
}
type BenchmarkResult struct {
Config BenchmarkConfig `json:"config"`
CPUInfo CPUInfo `json:"cpuInfo"`
StartedAt time.Time `json:"startedAt"`
Duration int64 `json:"duration"`
TotalOps int64 `json:"totalOps"`
MOpsPerSec float64 `json:"mOpsPerSec"`
Score int64 `json:"score"`
CoreResults []CoreResult `json:"coreResults"`
}
type Submission struct {
SubmissionID string `json:"submissionID"`
Submitter string `json:"submitter"`
SubmittedAt time.Time `json:"submittedAt"`
BenchmarkResult
}
func (b BenchmarkResult) Validate() error {
if strings.TrimSpace(b.CPUInfo.BrandString) == "" {
return errors.New("cpuInfo.brandString is required")
}
if b.StartedAt.IsZero() {
return errors.New("startedAt is required and must be RFC3339-compatible")
}
if b.Config.DurationSecs <= 0 {
return errors.New("config.durationSecs must be greater than zero")
}
if b.Duration <= 0 {
return errors.New("duration must be greater than zero")
}
if b.TotalOps < 0 || b.Score < 0 || b.MOpsPerSec < 0 {
return errors.New("duration, totalOps, mOpsPerSec, and score must be non-negative")
}
if b.CPUInfo.LogicalCores < 0 || b.CPUInfo.PhysicalCores < 0 {
return errors.New("cpu core counts must be non-negative")
}
for _, result := range b.CoreResults {
if result.LogicalID < 0 {
return fmt.Errorf("coreResults.logicalID must be non-negative")
}
if result.MOpsPerSec < 0 || result.TotalOps < 0 {
return fmt.Errorf("coreResults values must be non-negative")
}
}
return nil
}
func NormalizeSubmitter(submitter string) string {
submitter = strings.TrimSpace(submitter)
if submitter == "" {
return "Anonymous"
}
return submitter
}
func ThreadModeLabel(multiCore bool) string {
if multiCore {
return "Multi-threaded"
}
return "Single-threaded"
}
func CloneSubmission(submission *Submission) *Submission {
if submission == nil {
return nil
}
copySubmission := *submission
if len(submission.CoreResults) > 0 {
copySubmission.CoreResults = append([]CoreResult(nil), submission.CoreResults...)
}
if len(submission.CPUInfo.Cores) > 0 {
copySubmission.CPUInfo.Cores = append([]CPUCoreDescriptor(nil), submission.CPUInfo.Cores...)
}
if len(submission.CPUInfo.SupportedFeatures) > 0 {
copySubmission.CPUInfo.SupportedFeatures = append([]string(nil), submission.CPUInfo.SupportedFeatures...)
}
return &copySubmission
}

284
lib/store/store.go Normal file
View File

@@ -0,0 +1,284 @@
package store
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"sync"
"time"
"cpu-benchmark-server/lib/model"
"github.com/dgraph-io/badger/v4"
"github.com/google/uuid"
)
const submissionPrefix = "submission:"
type indexedSubmission struct {
submission *model.Submission
searchText string
cpuText string
}
type Store struct {
db *badger.DB
mu sync.RWMutex
orderedIDs []string
records map[string]*indexedSubmission
}
func Open(path string) (*Store, error) {
opts := badger.DefaultOptions(path).WithLogger(nil)
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
store := &Store{
db: db,
records: make(map[string]*indexedSubmission),
}
if err := store.loadIndex(); err != nil {
_ = db.Close()
return nil, err
}
return store, nil
}
func (s *Store) Close() error {
return s.db.Close()
}
func (s *Store) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.orderedIDs)
}
func (s *Store) SaveSubmission(result model.BenchmarkResult, submitter string) (*model.Submission, error) {
submission := &model.Submission{
SubmissionID: uuid.NewString(),
Submitter: model.NormalizeSubmitter(submitter),
SubmittedAt: time.Now().UTC(),
BenchmarkResult: result,
}
key := submissionKey(submission.SubmittedAt, submission.SubmissionID)
payload, err := json.Marshal(submission)
if err != nil {
return nil, err
}
if err := s.db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key), payload)
}); err != nil {
return nil, err
}
indexed := newIndexedSubmission(submission)
s.mu.Lock()
s.records[submission.SubmissionID] = indexed
s.orderedIDs = append([]string{submission.SubmissionID}, s.orderedIDs...)
s.mu.Unlock()
return model.CloneSubmission(submission), nil
}
func (s *Store) ListSubmissions(page, pageSize int) ([]model.Submission, int) {
s.mu.RLock()
defer s.mu.RUnlock()
total := len(s.orderedIDs)
start, end, _ := pageBounds(page, pageSize, total)
results := make([]model.Submission, 0, max(0, end-start))
for _, id := range s.orderedIDs[start:end] {
record := s.records[id]
if record == nil {
continue
}
results = append(results, *model.CloneSubmission(record.submission))
}
return results, total
}
func (s *Store) SearchSubmissions(text, cpu string) []model.Submission {
queryText := normalizeSearchText(text)
cpuText := normalizeSearchText(cpu)
s.mu.RLock()
defer s.mu.RUnlock()
results := make([]model.Submission, 0)
for _, id := range s.orderedIDs {
record := s.records[id]
if record == nil {
continue
}
if !matchesSearch(record.searchText, queryText) {
continue
}
if !matchesSearch(record.cpuText, cpuText) {
continue
}
results = append(results, *model.CloneSubmission(record.submission))
}
return results
}
func (s *Store) loadIndex() error {
return s.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = true
opts.Prefix = []byte(submissionPrefix)
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
payload, err := item.ValueCopy(nil)
if err != nil {
return err
}
var submission model.Submission
if err := json.Unmarshal(payload, &submission); err != nil {
return fmt.Errorf("decode %q: %w", item.Key(), err)
}
s.records[submission.SubmissionID] = newIndexedSubmission(&submission)
s.orderedIDs = append(s.orderedIDs, submission.SubmissionID)
}
return nil
})
}
func newIndexedSubmission(submission *model.Submission) *indexedSubmission {
return &indexedSubmission{
submission: model.CloneSubmission(submission),
searchText: buildSearchText(submission),
cpuText: normalizeSearchText(submission.CPUInfo.BrandString),
}
}
func buildSearchText(submission *model.Submission) string {
parts := []string{
submission.SubmissionID,
submission.Submitter,
submission.CPUInfo.BrandString,
submission.CPUInfo.VendorID,
model.ThreadModeLabel(submission.Config.MultiCore),
strconv.Itoa(submission.Config.DurationSecs),
strconv.Itoa(submission.CPUInfo.PhysicalCores),
strconv.Itoa(submission.CPUInfo.LogicalCores),
strconv.FormatInt(submission.Duration, 10),
strconv.FormatInt(submission.TotalOps, 10),
strconv.FormatInt(submission.Score, 10),
fmt.Sprintf("%.4f", submission.MOpsPerSec),
}
for _, feature := range submission.CPUInfo.SupportedFeatures {
parts = append(parts, feature)
}
for _, result := range submission.CoreResults {
parts = append(parts,
strconv.Itoa(result.LogicalID),
result.CoreType,
strconv.FormatInt(result.TotalOps, 10),
fmt.Sprintf("%.4f", result.MOpsPerSec),
)
}
return normalizeSearchText(strings.Join(parts, " "))
}
func submissionKey(timestamp time.Time, submissionID string) string {
reversed := math.MaxInt64 - timestamp.UTC().UnixNano()
return fmt.Sprintf("%s%019d:%s", submissionPrefix, reversed, submissionID)
}
func normalizeSearchText(value string) string {
return strings.Join(strings.Fields(strings.ToLower(value)), " ")
}
func matchesSearch(target, query string) bool {
if query == "" {
return true
}
for _, token := range strings.Fields(query) {
if !strings.Contains(target, token) {
return false
}
}
return true
}
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 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
}

427
lib/web/app.go Normal file
View File

@@ -0,0 +1,427 @@
package web
import (
"encoding/json"
"fmt"
"html/template"
"io"
"mime"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"cpu-benchmark-server/lib/model"
"cpu-benchmark-server/lib/store"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
const maxSubmissionBytes = 4 << 20
type App struct {
store *store.Store
templates *template.Template
pageSize int
}
type indexPageData struct {
Submissions []model.Submission
QueryText string
QueryCPU string
Page int
TotalPages int
TotalCount int
ShowingFrom int
ShowingTo int
PrevURL string
NextURL string
}
type jsonSubmissionEnvelope struct {
Submitter string `json:"submitter"`
Benchmark *model.BenchmarkResult `json:"benchmark"`
Result *model.BenchmarkResult `json:"result"`
Data *model.BenchmarkResult `json:"data"`
}
type flatSubmissionEnvelope struct {
Submitter string `json:"submitter"`
model.BenchmarkResult
}
type errorResponse struct {
Error string `json:"error"`
}
func New(store *store.Store, pageSize int) (*App, error) {
funcs := template.FuncMap{
"formatInt64": formatInt64,
"formatFloat": formatFloat,
"formatTime": formatTime,
"modeLabel": model.ThreadModeLabel,
}
templates, err := template.New("index.html").Funcs(funcs).ParseFiles("templates/index.html")
if err != nil {
return nil, err
}
return &App{
store: store,
templates: templates,
pageSize: pageSize,
}, nil
}
func (a *App) Routes() http.Handler {
router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
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
}
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"))
var (
submissions []model.Submission
totalCount int
)
if text != "" || cpu != "" {
matches := a.store.SearchSubmissions(text, cpu)
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)
data := indexPageData{
Submissions: submissions,
QueryText: text,
QueryCPU: cpu,
Page: page,
TotalPages: totalPageCount,
TotalCount: totalCount,
ShowingFrom: showingFrom,
ShowingTo: showingTo,
PrevURL: buildIndexURL(max(1, page-1), text, cpu),
NextURL: buildIndexURL(max(1, min(totalPageCount, page+1)), text, cpu),
}
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) {
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"))
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, err := parseSubmissionRequest(r)
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
}
submission, err := a.store.SaveSubmission(result, submitter)
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,
"submittedAt": submission.SubmittedAt,
})
}
func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, error) {
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)
}
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)
}
}
func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
return model.BenchmarkResult{}, "", fmt.Errorf("read request body: %w", err)
}
submitter := firstNonEmpty(
r.URL.Query().Get("submitter"),
r.Header.Get("X-Submitter"),
)
var nested jsonSubmissionEnvelope
if err := json.Unmarshal(body, &nested); err == nil {
submitter = firstNonEmpty(nested.Submitter, submitter)
for _, candidate := range []*model.BenchmarkResult{nested.Benchmark, nested.Result, nested.Data} {
if candidate != nil {
return *candidate, submitter, nil
}
}
}
var flat flatSubmissionEnvelope
if err := json.Unmarshal(body, &flat); err != nil {
return model.BenchmarkResult{}, "", fmt.Errorf("decode benchmark JSON: %w", err)
}
return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), nil
}
func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, error) {
if err := r.ParseMultipartForm(maxSubmissionBytes); err != nil {
return model.BenchmarkResult{}, "", fmt.Errorf("parse multipart form: %w", err)
}
payload, err := readMultipartPayload(r)
if err != nil {
return model.BenchmarkResult{}, "", err
}
var result model.BenchmarkResult
if err := json.Unmarshal(payload, &result); err != nil {
return model.BenchmarkResult{}, "", fmt.Errorf("decode benchmark JSON: %w", err)
}
return result, r.FormValue("submitter"), nil
}
func readMultipartPayload(r *http.Request) ([]byte, error) {
for _, field := range []string{"benchmark", "file", "benchmarkFile"} {
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"} {
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 string) string {
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)
}
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 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
}