Database setup for the Website service (#13)
This PR contains the work done to introduce a _Fluent_-based persistence layer for the Website service, selectable at runtime alongside the existing in-memory default, plus the local dev tooling and docs to support it. To provide further details about the work: * Persistence package * The `Driver` and `TLS` enumerations * The `Configuration` type * The `Service` factory that builds the service * `PrepareDB` for migrations registration * The `Probe` for readiness checks. * App integration * Builds the driver, registers migrations, and attaches `Fluent` to the service lifecycle so it starts/stops with the HTTP server. * Migrate-on-boot is gated to the in-memory backend; MySQL/MariaDB is migrated out of band via --database-migrate so shared databases never race on startup. * The `ConfigReader+Properties` extension maps database.* config keys onto the driver. * Library * Added database configuration constants. * The `HealthController` controller gains a readiness probe: `GET /health/ready` checks whether the database is reachable, separate from the existing liveness check. * Others * Updated the `docker-compose` files to support a database service behind a database profile, and hardened for local development * New database targets on the `Makefile` file and overall documentation updated * Updated the `.env.local`, `Dockerfile`, and `README` files to document the persistence workflow, config keys, and local DB commands Reviewed-on: rock-n-code/loud-amsterdam#13 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
|
||||
/// A backend that resolves localized strings for an explicit locale and reports the languages it serves.
|
||||
///
|
||||
/// This is the seam that decouples ``Localize`` and ``LanguageList`` from *how* localizations are stored
|
||||
/// and resolved. The shipping implementation, ``StringCatalog``, reads a raw `.xcstrings` catalog so it
|
||||
/// behaves identically on Darwin and Linux. A future backend — for example one built on
|
||||
/// `String(localized:)` for a native Apple app that needs plural and device variations — can conform
|
||||
/// without changing any caller.
|
||||
///
|
||||
/// Resolution never fails: an implementation returns the key itself when it has no localization for it,
|
||||
/// mirroring Foundation's `String(localized:)`. This is the contract both a dictionary lookup and the
|
||||
/// native API can honour, since the native API cannot distinguish a missing key from a translation that
|
||||
/// happens to equal the key.
|
||||
protocol CatalogResolving: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The source (development) language, used as the final fallback when a locale has no localization.
|
||||
var sourceLanguage: String { get }
|
||||
|
||||
/// Every language the backend can resolve strings for, including the source language.
|
||||
var languages: Set<String> { get }
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Resolves a catalog key in the given locale.
|
||||
/// - Parameters:
|
||||
/// - key: the catalog key to look up.
|
||||
/// - locale: the locale to resolve the key in.
|
||||
/// - Returns: the localized string, falling back to the source language, then to the key itself.
|
||||
func string(
|
||||
for key: String,
|
||||
in locale: Locale
|
||||
) -> String
|
||||
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import Foundation
|
||||
|
||||
/// A decoded `.xcstrings` String Catalog, read directly from a bundle's resources.
|
||||
///
|
||||
/// The catalog is parsed from raw JSON rather than through Foundation's compiled-catalog APIs (`String(localized:)`,
|
||||
/// `Bundle.localizations`, `Bundle.preferredLocalizations`). Those are either unavailable or non-functional on non-Darwin platforms
|
||||
/// (Linux), where the toolchain ships no `xcstringstool` and so copies the raw `.xcstrings` into the resource bundle instead of compiling it.
|
||||
/// Reading the catalog ourselves gives identical behaviour on every platform the service builds for.
|
||||
///
|
||||
/// Only simple `stringUnit` values are decoded; plural and device variations are not represented.
|
||||
struct StringCatalog: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The source language of the catalog, used as the fallback when a key lacks a requested localization.
|
||||
let sourceLanguage: String
|
||||
|
||||
/// The resolved entries, keyed by catalog key then by language code.
|
||||
let entries: [String: [String: String]]
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// Every language the catalog provides a localization for, including the source language.
|
||||
var languages: Set<String> {
|
||||
var languages = Set(entries.values.flatMap(\.keys))
|
||||
|
||||
languages.insert(sourceLanguage)
|
||||
|
||||
return languages
|
||||
}
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Reads the catalog named `table` from `bundle`.
|
||||
///
|
||||
/// Falls back to an empty catalog (source language `"en"`, no entries) when the resource is missing or cannot be decoded, so lookups degrade
|
||||
/// to returning the key and the language list to the default.
|
||||
/// - Parameters:
|
||||
/// - bundle: the bundle whose resources contain the String Catalog.
|
||||
/// - table: the name of the String Catalog resource, without the `.xcstrings` extension.
|
||||
init(
|
||||
bundle: Bundle,
|
||||
table: String = "Localizable"
|
||||
) {
|
||||
guard let url = bundle.url(
|
||||
forResource: table,
|
||||
withExtension: .Extension.stringCatalog
|
||||
) else {
|
||||
self.sourceLanguage = "en"
|
||||
self.entries = [:]
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
let decoder = JSONDecoder()
|
||||
let data = try Data(contentsOf: url)
|
||||
let decoded = try decoder.decode(
|
||||
Decoded.self,
|
||||
from: data
|
||||
)
|
||||
|
||||
self.sourceLanguage = decoded.sourceLanguage
|
||||
self.entries = decoded.strings
|
||||
.mapValues { entry in
|
||||
(entry.localizations ?? [:])
|
||||
.compactMapValues { $0.stringUnit?.value }
|
||||
}
|
||||
} catch {
|
||||
self.sourceLanguage = "en"
|
||||
self.entries = [:]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - CatalogResolving
|
||||
|
||||
extension StringCatalog: CatalogResolving {
|
||||
|
||||
func string(
|
||||
for key: String,
|
||||
in locale: Locale
|
||||
) -> String {
|
||||
guard let byLanguage = entries[key] else {
|
||||
return key
|
||||
}
|
||||
|
||||
let language = locale
|
||||
.language
|
||||
.languageCode?
|
||||
.identifier
|
||||
?? sourceLanguage
|
||||
|
||||
return byLanguage[language]
|
||||
?? byLanguage[sourceLanguage]
|
||||
?? key
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Decoding Types
|
||||
|
||||
private extension StringCatalog {
|
||||
|
||||
/// The subset of the `.xcstrings` format needed to resolve simple string entries.
|
||||
struct Decoded: Decodable {
|
||||
|
||||
let sourceLanguage: String
|
||||
let strings: [String: Entry]
|
||||
|
||||
struct Entry: Decodable {
|
||||
let localizations: [String: Localization]?
|
||||
}
|
||||
|
||||
struct Localization: Decodable {
|
||||
let stringUnit: StringUnit?
|
||||
}
|
||||
|
||||
struct StringUnit: Decodable {
|
||||
let value: String
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
enum Extension {
|
||||
static let stringCatalog = "xcstrings"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user