mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-26 21:27:48 -05:00
Zod validated config for security and ease of getting started
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { add } from "date-fns";
|
||||
import { nanoid } from "nanoid";
|
||||
import { ServerConfig } from "~/config.server";
|
||||
import * as UserRepository from "~/features/user-page/UserRepository.server";
|
||||
import { IS_E2E_TEST_RUN } from "~/utils/e2e";
|
||||
import invariant from "~/utils/invariant";
|
||||
@@ -15,7 +16,7 @@ function logSkalpError(action: string) {
|
||||
|
||||
if (code === "ECONNREFUSED") {
|
||||
logger.error(
|
||||
`Skalop "${action}" failed: connection refused at ${process.env.SKALOP_SYSTEM_MESSAGE_URL} — is the skalop service running?`,
|
||||
`Skalop "${action}" failed: connection refused at ${ServerConfig.skalop.systemMessageUrl} — is the skalop service running?`,
|
||||
);
|
||||
} else {
|
||||
logger.error(`Skalop "${action}" failed:`, err);
|
||||
@@ -35,13 +36,13 @@ let systemMessagesDisabled = false;
|
||||
|
||||
if (!IS_E2E_TEST_RUN) {
|
||||
invariant(
|
||||
process.env.SKALOP_SYSTEM_MESSAGE_URL,
|
||||
ServerConfig.skalop.systemMessageUrl,
|
||||
"Missing env var: SKALOP_SYSTEM_MESSAGE_URL",
|
||||
);
|
||||
invariant(process.env.SKALOP_TOKEN, "Missing env var: SKALOP_TOKEN");
|
||||
invariant(ServerConfig.skalop.token, "Missing env var: SKALOP_TOKEN");
|
||||
} else if (
|
||||
!process.env.SKALOP_SYSTEM_MESSAGE_URL ||
|
||||
!process.env.SKALOP_TOKEN
|
||||
!ServerConfig.skalop.systemMessageUrl ||
|
||||
!ServerConfig.skalop.token
|
||||
) {
|
||||
systemMessagesDisabled = true;
|
||||
}
|
||||
@@ -63,14 +64,14 @@ export const send: ChatSystemMessageService["send"] = (partialMsg) => {
|
||||
};
|
||||
});
|
||||
|
||||
return void fetch(process.env.SKALOP_SYSTEM_MESSAGE_URL!, {
|
||||
return void fetch(ServerConfig.skalop.systemMessageUrl!, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
action: "sendMessage",
|
||||
messages: fullMessages,
|
||||
}),
|
||||
headers: [
|
||||
[SKALOP_TOKEN_HEADER_NAME, process.env.SKALOP_TOKEN!],
|
||||
[SKALOP_TOKEN_HEADER_NAME, ServerConfig.skalop.token!],
|
||||
["Content-Type", "application/json"],
|
||||
],
|
||||
}).catch(logSkalpError("sendMessage"));
|
||||
@@ -79,14 +80,14 @@ export const send: ChatSystemMessageService["send"] = (partialMsg) => {
|
||||
export function removeRoom(chatCode: string) {
|
||||
if (systemMessagesDisabled) return;
|
||||
|
||||
return void fetch(process.env.SKALOP_SYSTEM_MESSAGE_URL!, {
|
||||
return void fetch(ServerConfig.skalop.systemMessageUrl!, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
action: "removeRoom",
|
||||
chatCode,
|
||||
}),
|
||||
headers: [
|
||||
[SKALOP_TOKEN_HEADER_NAME, process.env.SKALOP_TOKEN!],
|
||||
[SKALOP_TOKEN_HEADER_NAME, ServerConfig.skalop.token!],
|
||||
["Content-Type", "application/json"],
|
||||
],
|
||||
}).catch(logSkalpError("removeRoom"));
|
||||
@@ -109,7 +110,7 @@ const metadataDedup = new Map<string, string>();
|
||||
|
||||
export async function setMetadata(args: SetMetadataArgs) {
|
||||
if (systemMessagesDisabled) return;
|
||||
if (!process.env.SKALOP_SYSTEM_MESSAGE_URL) return;
|
||||
if (!ServerConfig.skalop.systemMessageUrl) return;
|
||||
|
||||
const participantsKey = args.participantUserIds
|
||||
.slice()
|
||||
@@ -146,7 +147,7 @@ export async function setMetadata(args: SetMetadataArgs) {
|
||||
`Setting chat room metadata for ${args.chatCode} (participants: ${participantsKey})`,
|
||||
);
|
||||
|
||||
return void fetch(process.env.SKALOP_SYSTEM_MESSAGE_URL, {
|
||||
return void fetch(ServerConfig.skalop.systemMessageUrl, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
action: "setMetadata",
|
||||
@@ -162,7 +163,7 @@ export async function setMetadata(args: SetMetadataArgs) {
|
||||
},
|
||||
}),
|
||||
headers: [
|
||||
[SKALOP_TOKEN_HEADER_NAME, process.env.SKALOP_TOKEN!],
|
||||
[SKALOP_TOKEN_HEADER_NAME, ServerConfig.skalop.token!],
|
||||
["Content-Type", "application/json"],
|
||||
],
|
||||
}).catch(logSkalpError("setMetadata"));
|
||||
|
||||
Reference in New Issue
Block a user