diff --git a/typegate/engine/src/lib.rs b/typegate/engine/src/lib.rs index c89f95872a..308ead2e55 100644 --- a/typegate/engine/src/lib.rs +++ b/typegate/engine/src/lib.rs @@ -102,6 +102,11 @@ pub async fn launch_typegate_deno( main_mod: deno_core::ModuleSpecifier, import_map_url: Option, ) -> Result<()> { + std::env::var("REDIS_URL") + .ok() + .ok_or_else(|| std::env::set_var("REDIS_URL", "none")) + .ok(); + let permissions = deno_runtime::permissions::PermissionsOptions { allow_run: Some(["hostname"].into_iter().map(str::to_owned).collect()), allow_sys: Some(vec![]), diff --git a/typegate/src/config.ts b/typegate/src/config.ts index 4df62a1698..c34e3b5b35 100644 --- a/typegate/src/config.ts +++ b/typegate/src/config.ts @@ -19,8 +19,10 @@ const schema = { hostname: z.string(), redis_url: z .string() - .url() .transform((s: string) => { + if (s == "none") { + return new URL("redis://none"); + } const url = new URL(s); if (url.password === "") { url.password = Deno.env.get("REDIS_PASSWORD") ?? ""; diff --git a/typegate/src/main.ts b/typegate/src/main.ts index 6e33a60604..9aa880e259 100644 --- a/typegate/src/main.ts +++ b/typegate/src/main.ts @@ -4,14 +4,16 @@ import { deferred } from "std/async/deferred.ts"; import { init_native } from "native"; -import { ReplicatedRegister } from "./typegate/register.ts"; +import { Register, ReplicatedRegister } from "./typegate/register.ts"; import config, { redisConfig } from "./config.ts"; import { Typegate } from "./typegate/mod.ts"; -import { RedisRateLimiter } from "./typegate/rate_limiter.ts"; +import { RateLimiter, RedisRateLimiter } from "./typegate/rate_limiter.ts"; import { SystemTypegraph } from "./system_typegraphs.ts"; import * as Sentry from "sentry"; import { getLogger } from "./log.ts"; import { init_runtimes } from "./runtimes/mod.ts"; +import { MemoryRegister } from "test-utils/memory_register.ts"; +import { NoLimiter } from "test-utils/no_limiter.ts"; const logger = getLogger(import.meta); logger.info(`typegate v${config.version} starting`); @@ -46,19 +48,29 @@ init_native(); await init_runtimes(); const deferredTypegate = deferred(); -const register = await ReplicatedRegister.init( - deferredTypegate, - redisConfig, -); -const limiter = await RedisRateLimiter.init(redisConfig); -const typegate = new Typegate(register, limiter); +let register: Register | undefined; +let limiter: RateLimiter | undefined; + +if (redisConfig.hostname != "none") { + register = await ReplicatedRegister.init(deferredTypegate, redisConfig); + limiter = await RedisRateLimiter.init(redisConfig); +} else { + logger.warning("Entering Redis-less mode"); + register = new MemoryRegister(); + limiter = new NoLimiter(); +} + +const typegate = new Typegate(register!, limiter!); + deferredTypegate.resolve(typegate); -const lastSync = await register.historySync().catch((err) => { - logger.error(err); - throw new Error(`failed to load history at boot, aborting: {err.message}`); -}); -register.startSync(lastSync); +if (register instanceof ReplicatedRegister) { + const lastSync = await register.historySync().catch((err) => { + logger.error(err); + throw new Error(`failed to load history at boot, aborting: ${err.message}`); + }); + register.startSync(lastSync); +} await SystemTypegraph.loadAll(typegate, !config.packaged);