feat(docker): add Dockerfile and .dockerignore for app containerization

This commit is contained in:
gameloader
2025-11-10 22:14:35 +08:00
parent 4cb9db1b7c
commit 2165fbe5ec
4 changed files with 161 additions and 48 deletions

View File

@ -7,38 +7,47 @@ import { getNumericParam } from "@/lib/route-params";
type AssignmentRouteParams = { assignmentId: string | string[] | undefined };
type SubmissionRecord = Awaited<ReturnType<typeof prisma.submission.findMany>>[number];
type ExportableColumn = {
header: string;
getter: (submission: SubmissionRecord) => unknown;
};
const EXPORTABLE_COLUMNS = {
studentId: { header: "学号", getter: (submission: any) => submission.studentId },
studentName: { header: "姓名", getter: (submission: any) => submission.studentName },
studentId: { header: "学号", getter: (submission) => submission.studentId },
studentName: { header: "姓名", getter: (submission) => submission.studentName },
originalFilename: {
header: "文件名",
getter: (submission: any) => submission.originalFilename,
getter: (submission) => submission.originalFilename,
},
fileUrl: { header: "文件地址", getter: (submission: any) => submission.fileUrl },
fileUrl: { header: "文件地址", getter: (submission) => submission.fileUrl },
submittedAt: {
header: "提交时间",
getter: (submission: any) =>
getter: (submission) =>
submission.submittedAt ? new Date(submission.submittedAt).toISOString() : "",
},
evaluationScore: {
header: "得分",
getter: (submission: any) =>
getter: (submission) =>
typeof submission.evaluationScore === "number"
? submission.evaluationScore
: "",
},
evaluationComment: {
header: "评价评语",
getter: (submission: any) => submission.evaluationComment ?? "",
getter: (submission) => submission.evaluationComment ?? "",
},
evaluatedAt: {
header: "评价时间",
getter: (submission: any) =>
getter: (submission) =>
submission.evaluatedAt ? new Date(submission.evaluatedAt).toISOString() : "",
},
} as const;
} satisfies Record<string, ExportableColumn>;
const DEFAULT_COLUMNS = ["studentId", "evaluationScore"] as Array<keyof typeof EXPORTABLE_COLUMNS>;
type ExportableColumnKey = keyof typeof EXPORTABLE_COLUMNS;
const DEFAULT_COLUMNS: ExportableColumnKey[] = ["studentId", "evaluationScore"];
export async function GET(
request: Request,
@ -84,7 +93,7 @@ export async function GET(
return NextResponse.json({ error: "请选择有效的导出列" }, { status: 400 });
}
const submissions = await prisma.submission.findMany({
const submissions: SubmissionRecord[] = await prisma.submission.findMany({
where: { assignmentId },
orderBy: { submittedAt: "asc" },
});

View File

@ -2,35 +2,62 @@ import { S3Client, PutObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3
import { Readable } from "node:stream";
import { randomUUID } from "crypto";
const {
AUTOTEACHER_S3_BUCKET,
AUTOTEACHER_S3_REGION,
AUTOTEACHER_S3_ENDPOINT_URL,
AUTOTEACHER_S3_ACCESS_KEY_ID,
AUTOTEACHER_S3_SECRET_ACCESS_KEY,
AUTOTEACHER_S3_PUBLIC_BASE_URL,
AUTOTEACHER_S3_USE_SSL,
} = process.env;
type S3EnvConfig = {
bucket: string;
region?: string;
endpoint?: string;
accessKeyId?: string;
secretAccessKey?: string;
publicBaseUrl?: string;
useSsl: boolean;
};
if (!AUTOTEACHER_S3_BUCKET) {
throw new Error("Missing AUTOTEACHER_S3_BUCKET environment variable");
}
let cachedConfig: S3EnvConfig | null = null;
let cachedClient: S3Client | null = null;
const useSsl = (AUTOTEACHER_S3_USE_SSL ?? "true").toLowerCase() !== "false";
const resolveEnvConfig = (): S3EnvConfig => {
const bucket = process.env.AUTOTEACHER_S3_BUCKET;
if (!bucket) {
throw new Error("Missing AUTOTEACHER_S3_BUCKET environment variable");
}
const s3Client = new S3Client({
region: AUTOTEACHER_S3_REGION || "us-east-1",
endpoint: AUTOTEACHER_S3_ENDPOINT_URL,
forcePathStyle: Boolean(AUTOTEACHER_S3_ENDPOINT_URL),
credentials:
AUTOTEACHER_S3_ACCESS_KEY_ID && AUTOTEACHER_S3_SECRET_ACCESS_KEY
? {
accessKeyId: AUTOTEACHER_S3_ACCESS_KEY_ID,
secretAccessKey: AUTOTEACHER_S3_SECRET_ACCESS_KEY,
}
: undefined,
tls: useSsl,
});
return {
bucket,
region: process.env.AUTOTEACHER_S3_REGION || "us-east-1",
endpoint: process.env.AUTOTEACHER_S3_ENDPOINT_URL,
accessKeyId: process.env.AUTOTEACHER_S3_ACCESS_KEY_ID,
secretAccessKey: process.env.AUTOTEACHER_S3_SECRET_ACCESS_KEY,
publicBaseUrl: process.env.AUTOTEACHER_S3_PUBLIC_BASE_URL,
useSsl: (process.env.AUTOTEACHER_S3_USE_SSL ?? "true").toLowerCase() !== "false",
};
};
const getConfig = (): S3EnvConfig => {
if (!cachedConfig) {
cachedConfig = resolveEnvConfig();
}
return cachedConfig;
};
const getClient = (): S3Client => {
if (!cachedClient) {
const config = getConfig();
cachedClient = new S3Client({
region: config.region,
endpoint: config.endpoint,
forcePathStyle: Boolean(config.endpoint),
credentials:
config.accessKeyId && config.secretAccessKey
? {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
}
: undefined,
tls: config.useSsl,
});
}
return cachedClient;
};
export type UploadResult = {
key: string;
@ -38,19 +65,20 @@ export type UploadResult = {
};
const buildFileUrl = (key: string): string => {
if (AUTOTEACHER_S3_PUBLIC_BASE_URL) {
return `${AUTOTEACHER_S3_PUBLIC_BASE_URL.replace(/\/$/, "")}/${key}`;
const config = getConfig();
if (config.publicBaseUrl) {
return `${config.publicBaseUrl.replace(/\/$/, "")}/${key}`;
}
if (AUTOTEACHER_S3_ENDPOINT_URL) {
return `${AUTOTEACHER_S3_ENDPOINT_URL.replace(/\/$/, "")}/${AUTOTEACHER_S3_BUCKET}/${key}`;
if (config.endpoint) {
return `${config.endpoint.replace(/\/$/, "")}/${config.bucket}/${key}`;
}
if (AUTOTEACHER_S3_REGION) {
return `https://${AUTOTEACHER_S3_BUCKET}.s3.${AUTOTEACHER_S3_REGION}.amazonaws.com/${key}`;
if (config.region) {
return `https://${config.bucket}.s3.${config.region}.amazonaws.com/${key}`;
}
return `https://${AUTOTEACHER_S3_BUCKET}.s3.amazonaws.com/${key}`;
return `https://${config.bucket}.s3.amazonaws.com/${key}`;
};
export async function uploadAssignmentFile(
@ -70,24 +98,30 @@ export async function uploadAssignmentFile(
contentType = "application/msword";
}
const config = getConfig();
const client = getClient();
const command = new PutObjectCommand({
Bucket: AUTOTEACHER_S3_BUCKET,
Bucket: config.bucket,
Key: key,
Body: fileBuffer,
ContentType: contentType,
});
await s3Client.send(command);
await client.send(command);
return { key, url: buildFileUrl(key) };
}
export async function downloadAssignmentFile(key: string): Promise<Buffer> {
const config = getConfig();
const client = getClient();
const command = new GetObjectCommand({
Bucket: AUTOTEACHER_S3_BUCKET,
Bucket: config.bucket,
Key: key,
});
const response = await s3Client.send(command);
const response = await client.send(command);
const body = response.Body;
if (!body) {
throw new Error("无法读取存储的作业文件");