Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/api/env/.env.functional.test
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ FAUCET_URL=https://faucet.sandbox-01.aksh.pw/faucet
API_NODE_ENDPOINT=https://api.sandbox-01.aksh.pw
PROVIDER_PROXY_URL=https://console-provider-proxy.akash.network

ALLOW_ANONYMOUS_USER_TRIAL=true
AMPLITUDE_API_KEY=AMPLITUDE_API_KEY
AMPLITUDE_SAMPLING=1
NOTIFICATIONS_API_BASE_URL=http://localhost:3081

FEATURE_FLAGS_ENABLE_ALL=true
3 changes: 1 addition & 2 deletions apps/api/env/.env.local.sample
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ PROVIDER_PROXY_URL=http://localhost:3040

DEPLOYMENT_ENV=local

ALLOW_ANONYMOUS_USER_TRIAL=true
AMPLITUDE_API_KEY=AMPLITUDE_API_KEY
AMPLITUDE_SAMPLING=1

NOTIFICATIONS_API_BASE_URL=http://localhost:3081
NOTIFICATIONS_API_BASE_URL=http://localhost:3081
3 changes: 1 addition & 2 deletions apps/api/env/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ BILLING_ENABLED=true
STRIPE_CHECKOUT_REDIRECT_URL=https://console.akash.network
PROVIDER_PROXY_URL=https://console-provider-proxy.akash.network

ALLOW_ANONYMOUS_USER_TRIAL=false
AMPLITUDE_SAMPLING=1
AMPLITUDE_SAMPLING=1
3 changes: 3 additions & 0 deletions apps/api/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ WEBSITE_URL=
AMPLITUDE_API_KEY=
AMPLITUDE_SAMPLING=
NOTIFICATIONS_API_BASE_URL=
FEATURE_FLAGS_ENABLE_ALL=true
UNLEASH_SERVER_API_TOKEN=
UNLEASH_SERVER_API_URL=
4 changes: 3 additions & 1 deletion apps/api/env/.env.unit.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ SQL_LOG_FORMAT=pretty
DEPLOYMENT_ENV=test
PROVIDER_PROXY_URL=http://localhost:3040
AMPLITUDE_API_KEY=AMPLITUDE_API_KEY
NOTIFICATIONS_API_BASE_URL=http://localhost:3081
NOTIFICATIONS_API_BASE_URL=http://localhost:3081

FEATURE_FLAGS_ENABLE_ALL=true
3 changes: 2 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"sql-formatter": "^15.3.2",
"stripe": "^16.8.0",
"ts-results": "^3.3.0",
"tsyringe": "^4.8.0",
"tsyringe": "^4.10.0",
"unleash-client": "^6.6.0",
"uuid": "^9.0.1",
"zod": "3.*"
},
Expand Down
12 changes: 10 additions & 2 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import packageJson from "../package.json";
import { apiKeysRouter } from "./auth/routes/api-keys/api-keys.router";
import { bidsRouter } from "./bid/routes/bids/bids.router";
import { certificateRouter } from "./certificate/routes/certificate.router";
import { FeatureFlagsService } from "./core/services/feature-flags/feature-flags.service";
import { shutdownServer } from "./core/services/shutdown-server/shutdown-server";
import { chainDb, syncUserSchema, userDb } from "./db/dbConnection";
import { deploymentSettingRouter } from "./deployment/routes/deployment-setting/deployment-setting.router";
import { deploymentsRouter } from "./deployment/routes/deployments/deployments.router";
Expand Down Expand Up @@ -173,11 +175,17 @@ export async function initApp() {
await initDb();
startScheduler();

await container.resolve(FeatureFlagsService).initialize();

appLogger.info({ event: "SERVER_STARTING", url: `http://localhost:${PORT}` });
serve({
const server = serve({
fetch: appHono.fetch,
port: typeof PORT === "string" ? parseInt(PORT) : PORT
port: typeof PORT === "string" ? parseInt(PORT, 10) : PORT
});
const shutdown = () => shutdownServer(server, appLogger, container.dispose.bind(container));

process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);
} catch (error) {
appLogger.error({ event: "APP_INIT_ERROR", error });
}
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export class AuthService {

get currentUser(): UserOutput {
// BUGALERT: https://github.com/akash-network/console/issues/1447
return this.executionContextService.get("CURRENT_USER");
return this.executionContextService.get("CURRENT_USER")!;
}

set ability(ability: Ability) {
this.executionContextService.set("ABILITY", ability);
}

get ability(): Ability {
return this.executionContextService.get("ABILITY");
return this.executionContextService.get("ABILITY")!;
}

get isAuthenticated(): boolean {
Expand Down
47 changes: 32 additions & 15 deletions apps/api/src/core/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import { z } from "zod";

export const envSchema = z.object({
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
STD_OUT_LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json"),
SQL_LOG_FORMAT: z.enum(["raw", "pretty"]).optional().default("raw"),
FLUENTD_TAG: z.string().optional().default("pino"),
FLUENTD_HOST: z.string().optional(),
FLUENTD_PORT: z.number({ coerce: true }).optional().default(24224),
NODE_ENV: z.enum(["development", "production", "test"]).optional().default("development"),
POSTGRES_DB_URI: z.string(),
POSTGRES_MAX_CONNECTIONS: z.number({ coerce: true }).optional().default(20),
DRIZZLE_MIGRATIONS_FOLDER: z.string().optional().default("./drizzle"),
DEPLOYMENT_ENV: z.string().optional().default("production"),
AMPLITUDE_API_KEY: z.string(),
AMPLITUDE_SAMPLING: z.number({ coerce: true }).optional().default(1)
});
export const envSchema = z
.object({
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
STD_OUT_LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json"),
SQL_LOG_FORMAT: z.enum(["raw", "pretty"]).optional().default("raw"),
FLUENTD_TAG: z.string().optional().default("pino"),
FLUENTD_HOST: z.string().optional(),
FLUENTD_PORT: z.number({ coerce: true }).optional().default(24224),
NODE_ENV: z.enum(["development", "production", "test"]).optional().default("development"),
POSTGRES_DB_URI: z.string(),
POSTGRES_MAX_CONNECTIONS: z.number({ coerce: true }).optional().default(20),
DRIZZLE_MIGRATIONS_FOLDER: z.string().optional().default("./drizzle"),
DEPLOYMENT_ENV: z.string().optional().default("production"),
NETWORK: z.enum(["mainnet", "testnet", "sandbox"]).default("mainnet"),
AMPLITUDE_API_KEY: z.string(),
AMPLITUDE_SAMPLING: z.number({ coerce: true }).optional().default(1),
UNLEASH_SERVER_API_URL: z.string().optional(),
UNLEASH_SERVER_API_TOKEN: z.string().optional(),
UNLEASH_APP_NAME: z.string().optional().default("console-api"),
FEATURE_FLAGS_ENABLE_ALL: z
.string()
.default("false")
.transform(value => value === "true")
})
.superRefine((value, ctx) => {
if (!value.FEATURE_FLAGS_ENABLE_ALL && (!value.UNLEASH_SERVER_API_URL || !value.UNLEASH_SERVER_API_TOKEN)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "UNLEASH_SERVER_API_URL and UNLEASH_SERVER_API_TOKEN are required when FEATURE_FLAGS_ENABLE_ALL is false"
});
}
});

export const envConfig = envSchema.parse(process.env);
6 changes: 3 additions & 3 deletions apps/api/src/core/services/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { z, ZodObject, ZodRawShape } from "zod";
import type { z, ZodEffects, ZodObject, ZodRawShape } from "zod";

interface ConfigServiceOptions<E extends ZodObject<ZodRawShape>, C extends Record<string, any>> {
interface ConfigServiceOptions<E extends ZodObject<ZodRawShape> | ZodEffects<ZodObject<ZodRawShape>>, C extends Record<string, any>> {
envSchema?: E;
config?: C;
}

// eslint-disable-next-line @typescript-eslint/ban-types
export class ConfigService<E extends ZodObject<ZodRawShape>, C extends Record<string, any> = {}> {
export class ConfigService<E extends ZodObject<ZodRawShape> | ZodEffects<ZodObject<ZodRawShape>>, C extends Record<string, any> = {}> {
private readonly config: C & z.infer<E>;

constructor(options: ConfigServiceOptions<E, C>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { singleton } from "tsyringe";

import { envSchema } from "@src/core/config/env.config";
import { ConfigService } from "@src/core/services/config/config.service";
import { envSchema } from "../../config/env.config";
import { ConfigService } from "../config/config.service";

@singleton()
export class CoreConfigService extends ConfigService<typeof envSchema> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import type { MongoAbility } from "@casl/ability";
import { AsyncLocalStorage } from "node:async_hooks";
import { singleton } from "tsyringe";

import type { UserOutput } from "@src/user/repositories";
import type { AppContext } from "../../types/app-context";

interface ExecutionStorage {
CURRENT_USER: UserOutput;
ABILITY: MongoAbility;
HTTP_CONTEXT: AppContext;
}

@singleton()
export class ExecutionContextService {
private readonly storage = new AsyncLocalStorage<Map<string, any>>();
private readonly storage = new AsyncLocalStorage<Map<string, unknown>>();

private get context() {
const store = this.storage.getStore();
Expand All @@ -15,12 +25,12 @@ export class ExecutionContextService {
return store;
}

set(key: string, value: any) {
set<K extends keyof ExecutionStorage>(key: K, value: ExecutionStorage[K] | undefined) {
this.context.set(key, value);
}

get(key: string) {
return this.context.get(key);
get<K extends keyof ExecutionStorage>(key: K): ExecutionStorage[K] | undefined {
return this.context.get(key) as ExecutionStorage[K] | undefined;
}

async runWithContext<R>(cb: (...args: any[]) => Promise<R>): Promise<R> {
Expand Down
Loading