refactor(server): share env parsing, shutdown and score capping helpers

Collapse the three env parsers into one generic helper, the duplicated
graceful-shutdown block into shutdownServer, and the score cap into
pointsFor. Behaviour is unchanged.
This commit is contained in:
tiennm99 committed 2026-09-28 15:19:59 +07:00
1 parent 97a352f9f5
commit 5848751c6b
6 files changed
+56 -100

No files matched your search

+2 -6
View File
@@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"iter"
"math/rand/v2"
"net/url"
"os"
"slices"
@@ -27,8 +28,6 @@ import (
"strings"
"unicode"
"math/rand/v2"
"golang.org/x/text/unicode/norm"
_ "modernc.org/sqlite"
)
@@ -98,7 +97,7 @@ func Open(path string) (*Store, error) {
return nil, fmt.Errorf("dictionary not found at %s — run 'make fetch-dict && make dict' first: %w", path, err)
}
db, err := sql.Open("sqlite", dsn(path))
db, err := sql.Open("sqlite", DSN(path, true))
if err != nil {
return nil, fmt.Errorf("open dictionary: %w", err)
}
@@ -148,9 +147,6 @@ func Open(path string) (*Store, error) {
return s, nil
}
// dsn is the read-only URI the store opens with.
func dsn(path string) string { return DSN(path, true) }
// DSN builds a SQLite URI for path. The path must be escaped: SQLite reads
// '#' as a URI fragment delimiter, so a bare path containing one silently
// opens a different (usually nonexistent) file and reports a confusing schema
+5 -21
View File
@@ -269,20 +269,13 @@ func TestResolveUnknown(t *testing.T) {
}
}
// nearMissFixtureAt builds a tiny dictionary purpose-built for NearMiss: one
// nearMissFixture builds a tiny dictionary purpose-built for NearMiss: one
// word with a unique stripped form, one ambiguous pair that shares a stripped
// form with each other, and nothing else that could coincidentally collide.
func nearMissFixtureAt(tb testing.TB, dir string) string {
func nearMissFixture(tb testing.TB) *Store {
tb.Helper()
path := filepath.Join(dir, "nearmiss.db")
db, err := sql.Open("sqlite", "file:"+path)
if err != nil {
tb.Fatal(err)
}
defer func() { _ = db.Close() }()
data := fixtureSchema + `
path := writeDB(tb, fixtureSchema+`
INSERT INTO meta VALUES ('source_license','CC BY-SA 4.0'),('word_count','4'),('meaning_count','0');
INSERT INTO words VALUES
('bình yên','bình','yên',2),
@@ -292,17 +285,8 @@ INSERT INTO words VALUES
('ngữ nghĩa','ngữ','nghĩa',2),
('ngừ nghĩa','ngừ','nghĩa',2);
INSERT INTO syllables VALUES ('bình',1),('an',1),('ngữ',1),('ngừ',1);
`
if _, err := db.Exec(data); err != nil {
tb.Fatal(err)
}
return path
}
func nearMissFixture(tb testing.TB) *Store {
tb.Helper()
store, err := Open(nearMissFixtureAt(tb, tb.TempDir()))
`)
store, err := Open(path)
if err != nil {
tb.Fatalf("Open: %v", err)
}