2026-03-25 09:39:51 +07:00
|
|
|
"""
|
|
|
|
|
Application configuration loaded from environment variables.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CONTENT_DIR = os.environ.get('CONTENT_DIR', 'content')
|
2026-07-21 20:50:01 +07:00
|
|
|
|
|
|
|
|
# Assets directory: derived from parent of CONTENT_DIR
|
|
|
|
|
# E.g. CONTENT_DIR='content' → ASSETS_DIR='assets'
|
|
|
|
|
# CONTENT_DIR='examples/content' → ASSETS_DIR='examples/assets'
|
|
|
|
|
# CONTENT_DIR='/app/content' → ASSETS_DIR='/app/assets'
|
|
|
|
|
ASSETS_DIR = os.path.join(os.path.dirname(CONTENT_DIR.rstrip(os.sep)), 'assets')
|
2026-08-29 00:17:48 +07:00
|
|
|
|
feat(assets): optimasi serving gambar (WebP on-the-fly + cache disk)
Arsitektur: upload tetap menyimpan original apa adanya; optimasi terjadi
saat serving — gambar raster di-resize (maks 1600px) + encode WebP
(quality 80), di-generate on-the-fly lalu di-cache di disk.
- config.CACHE_DIR = sibling ASSETS_DIR ('assets-cache'), WAJIB di luar
ASSETS_DIR supaya export_bundle (os.walk tanpa filter) tetap lossless
- services/image_cache.py: varian WebP, cache key path+mtime+size+width,
invalidasi otomatis via mtime, atomic write, fallback original bila gagal
- routes/lessons.py::send_assets: handler (bukan static serve) —
.png/.jpg/.jpeg/.webp/.gif -> varian WebP; .svg & GIF animasi pass-through;
?orig=1 untuk original; ?w= untuk lebar alternatif; ETag + Cache-Control
- Dockerfile: buat /app/assets-cache writable
Test (services/tests/test_asset_cache.py, 20 test): lokasi cache di luar
ASSETS_DIR, cache reuse, invalidasi mtime, passthrough SVG/GIF animasi,
fallback file korup, ETag/304, dan acceptance test — upload -> request ->
export bundle berisi HANYA original tanpa .webp turunan.
2026-10-25 19:19:07 +07:00
|
|
|
# Cache varian ringan (WebP) untuk asset gambar raster.
|
|
|
|
|
#
|
|
|
|
|
# WAJIB di luar ASSETS_DIR — sibling, bukan `assets/.cache/`. Alasan:
|
|
|
|
|
# `export_bundle` (routes/content_editor.py) men-zip SELURUH isi ASSETS_DIR
|
|
|
|
|
# tanpa filter, jadi file cache di dalam ASSETS_DIR akan ikut ter-zip lalu
|
|
|
|
|
# ter-import ulang sebagai asset biasa (menggandakan file & merusak bundel).
|
|
|
|
|
# Dengan cache di luar, export 100% lossless — hanya original yang ikut.
|
|
|
|
|
#
|
|
|
|
|
# E.g. CONTENT_DIR='content' → CACHE_DIR='assets-cache'
|
|
|
|
|
# CONTENT_DIR='/app/content' → CACHE_DIR='/app/assets-cache'
|
|
|
|
|
CACHE_DIR = os.path.join(os.path.dirname(CONTENT_DIR.rstrip(os.sep)), 'assets-cache')
|
|
|
|
|
|
|
|
|
|
# Dimensi maksimum (sisi terpanjang, px) untuk varian WebP yang di-generate.
|
|
|
|
|
ASSET_VARIANT_MAX_DIM = int(os.environ.get('ASSET_VARIANT_MAX_DIM', '1600'))
|
|
|
|
|
# Kualitas WebP (1-100).
|
|
|
|
|
ASSET_VARIANT_QUALITY = int(os.environ.get('ASSET_VARIANT_QUALITY', '80'))
|
|
|
|
|
# Cache-Control max-age untuk respons asset (detik).
|
|
|
|
|
ASSET_CACHE_MAX_AGE = int(os.environ.get('ASSET_CACHE_MAX_AGE', '3600'))
|
|
|
|
|
|
2026-08-29 00:17:48 +07:00
|
|
|
# ── Evaluation weights & done threshold ───────────────────────────
|
|
|
|
|
# Bobot exercise (0-100) dan quiz (0-100) untuk composite score.
|
|
|
|
|
LESSON_EXERCISE_WEIGHT = float(os.environ.get('LESSON_EXERCISE_WEIGHT', '70'))
|
|
|
|
|
LESSON_QUIZ_WEIGHT = float(os.environ.get('LESSON_QUIZ_WEIGHT', '30'))
|
|
|
|
|
# Ambang minimum composite score (0-100) untuk status lesson "done".
|
|
|
|
|
LESSON_DONE_MIN_PERCENT = float(os.environ.get('LESSON_DONE_MIN_PERCENT', '75'))
|
|
|
|
|
|
|
|
|
|
# Validasi: weights harus berjumlah 100
|
|
|
|
|
_total_weight = LESSON_EXERCISE_WEIGHT + LESSON_QUIZ_WEIGHT
|
|
|
|
|
if abs(_total_weight - 100.0) > 0.01:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"LESSON_EXERCISE_WEIGHT + LESSON_QUIZ_WEIGHT must equal 100, got {_total_weight}"
|
|
|
|
|
)
|