-
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
143 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,17 @@ | ||
import { building } from "$app/environment"; | ||
import { resolveConfiguration } from "$lib/server/configuration"; | ||
|
||
if (!building) { | ||
// 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. | ||
await resolveConfiguration(); | ||
} | ||
|
||
// 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,78 @@ | ||
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 defaultConfigFilePath = "/etc/neogrok/config.json"; | ||
|
||
const environmentConfigurationSchema = v.object({ | ||
ZOEKT_URL: zoektUrlSchema.optional(), | ||
}); | ||
|
||
type Configuration = { | ||
readonly zoektUrl: URL; | ||
readonly openGrokProjectMappings: ReadonlyMap<string, string>; | ||
}; | ||
|
||
// We have to export a not-yet-bound `configuration` at module eval time because | ||
// either SvelteKit or Vite is _executing_ this module during the build, for | ||
// reasons that I do not understand. We do not want to enforce required | ||
// configuration options being defined at build time; neogrok does not prerender | ||
// anything. | ||
// | ||
// So, we do not want to resolve the configuration at module eval time. | ||
// | ||
// So, we rely on live export bindings plus a call to `resolveConfiguration` | ||
// from something else at server startup. Anything consuming the actual | ||
// configuration will have to avoid dereferencing it in the module scope. | ||
export let configuration: Configuration; | ||
export const resolveConfiguration: () => Promise<void> = async () => { | ||
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}` | ||
); | ||
} | ||
|
||
configuration = { | ||
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