-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is now a single facility for configuring anything that is configurable. And we've stopped using SvelteKit's `env` facilities, as they're pretty obnoxious: trying to type the environment in your framework is quixotic. It's up to the application to do that, and so we do it.
- Loading branch information
Showing
15 changed files
with
125 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ node_modules/ | |
.svelte-kit | ||
build/ | ||
.env | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Eagerly resolve the configuration on startup, such that startup fails if the | ||
// configuration is invalid. | ||
// | ||
// We don't actually use any of the features of this hooks module, other than | ||
// that it is evaluated on startup; other modules are not. | ||
import "$lib/server/configuration"; | ||
|
||
// TODO we should have prom metrics, on, among other things, HTTP requests | ||
// handled, and the `handle` hook would be a good way to do that. | ||
|
||
// TODO SvelteKit logs an error every time anything requests a URL that does not | ||
// map to a route. Bonkers. Silence those by implementing `handleError`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as v from "@badrap/valita"; | ||
import fs from "node:fs"; | ||
|
||
const zoektUrlSchema = v.string().map((u) => new URL(u)); | ||
const fileConfigurationSchema = v.object({ | ||
zoektUrl: zoektUrlSchema.optional(), | ||
openGrokProjectMappings: v | ||
.record(v.string()) | ||
.map((o) => new Map(Object.entries(o))) | ||
.optional(), | ||
}); | ||
type FileConfiguration = v.Infer<typeof fileConfigurationSchema>; | ||
|
||
const environmentConfigurationSchema = v.object({ | ||
ZOEKT_URL: zoektUrlSchema.optional(), | ||
}); | ||
|
||
type Configuration = { | ||
readonly zoektUrl: URL; | ||
readonly openGrokProjectMappings: ReadonlyMap<string, string>; | ||
}; | ||
export const configuration: Configuration = await (async function () { | ||
const defaultConfigFilePath = "/etc/neogrok/config.json"; | ||
const configFilePath = | ||
process.env.NEOGROK_CONFIG_FILE ?? defaultConfigFilePath; | ||
let fileConfig: FileConfiguration | undefined; | ||
try { | ||
fileConfig = fileConfigurationSchema.parse( | ||
JSON.parse(await fs.promises.readFile(configFilePath, "utf8")), | ||
{ mode: "strict" } | ||
); | ||
} catch (e) { | ||
// Swallow errors related to the default config file being missing. | ||
if ( | ||
!( | ||
e && | ||
typeof e === "object" && | ||
"code" in e && | ||
e.code === "ENOENT" && | ||
configFilePath === defaultConfigFilePath | ||
) | ||
) { | ||
throw new Error(`Configuration file at ${configFilePath} is invalid`, { | ||
cause: e, | ||
}); | ||
} | ||
} | ||
|
||
const environmentConfig = environmentConfigurationSchema.parse(process.env, { | ||
mode: "strip", | ||
}); | ||
|
||
const zoektUrl = environmentConfig.ZOEKT_URL ?? fileConfig?.zoektUrl; | ||
if (zoektUrl === undefined) { | ||
throw new Error( | ||
`"ZOEKT_URL" must be defined in the environment, or "zoektUrl" must be defined in the configuration file at ${configFilePath}` | ||
); | ||
} | ||
|
||
return { | ||
zoektUrl, | ||
openGrokProjectMappings: | ||
fileConfig?.openGrokProjectMappings ?? new Map<string, string>(), | ||
}; | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters