update 00000

This commit is contained in:
smfahim25 2025-03-19 10:45:42 +06:00
parent f9da806ec9
commit 40d600ab5b
3 changed files with 148 additions and 54 deletions

View file

@ -2,21 +2,36 @@ import Elysia, { t } from "elysia";
import { generateToken, getUserData, updateUser } from "./auth.controller";
import { verifyAuth } from "../../middlewares/auth.middlewares";
export const authRoute = new Elysia({
prefix: "/api/auth",
tags: ["Auth"],
detail: {
description: "Routes for managing users",
},
});
export const authRoute = new Elysia({ prefix: "/auth" });
authRoute.get(
"/user/:userId",
async ({ params: { userId } }) => await getUserData(userId),
{
detail: {
tags: ["Auth"],
summary: "Get user data",
description: "Retrieve user data by user ID",
},
params: t.Object({
userId: t.String(),
userId: t.String({ description: "The user ID" }),
}),
response: {
200: t.Object({
status: t.Number(),
message: t.String(),
data: t.Object({
// Define user data properties here
// For example:
id: t.String(),
// Add other fields
}),
}),
404: t.Object({
status: t.Number(),
message: t.String(),
}),
},
}
);
@ -24,22 +39,61 @@ authRoute.post(
"/user/update/:userId",
async ({ params: { userId }, body }) => await updateUser(userId, body),
{
detail: {
tags: ["Auth"],
summary: "Update user data",
description: "Update user payment status and expiration date",
},
params: t.Object({
userId: t.String(),
userId: t.String({ description: "The user ID to update" }),
}),
body: t.Object({
paid_status: t.String(),
package_expire_date: t.String(),
paid_status: t.String({ description: "User payment status" }),
package_expire_date: t.String({ description: "Package expiration date" }),
}),
response: {
200: t.Object({
status: t.Number(),
message: t.String(),
// Add other expected response properties
}),
400: t.Object({
status: t.Number(),
message: t.String(),
}),
},
}
);
authRoute.get(
"/generate-token/:userId",
async (context) => await generateToken(context)
async (context) => await generateToken(context),
{
detail: {
tags: ["Auth"],
summary: "Generate authentication token",
description: "Generate a new auth token for the specified user",
},
params: t.Object({
userId: t.String({ description: "The user ID" }),
}),
response: {
200: t.Object({
status: t.Number(),
message: t.String(),
token: t.String(),
}),
400: t.Object({
status: t.Number(),
message: t.String(),
}),
},
}
);
authRoute.get("/user/me", async ({ cookie }) => {
authRoute.get(
"/user/me",
async ({ cookie }) => {
const authData = await verifyAuth(cookie);
if (authData.status !== 200) {
return authData;
@ -57,4 +111,24 @@ authRoute.get("/user/me", async ({ cookie }) => {
return response;
}
}
});
},
{
detail: {
tags: ["Auth"],
summary: "Get current user data",
description: "Get currently authenticated user data from cookie",
},
response: {
200: t.Object({
status: t.Number(),
message: t.String(),
token: t.String(),
// Add other user data properties
}),
401: t.Object({
status: t.Number(),
message: t.String(),
}),
},
}
);

View file

@ -1,14 +1,33 @@
import Elysia from "elysia";
import { Elysia } from "elysia";
import { projectRoutes } from "./project/project.route";
import { uploadRoutes } from "./upload/upload.route";
import { authRoute } from "./auth/auth.route";
import { downloadRoute } from "./downloadCount/download.count.route";
export const api = new Elysia({});
api.get("/", () => {
export const api = new Elysia({ prefix: "/api" })
.get("/", () => {
console.log("Root endpoint accessed");
return "Hello from PlanPostAI Canvas API";
})
.get("/test", () => {
console.log("Test endpoint accessed");
return "Test endpoint works!";
})
.use(authRoute)
.use(projectRoutes)
.use(uploadRoutes)
.use(downloadRoute)
.onError(({ code, error, set }) => {
console.error(`API Error: ${code}`, error);
if (code === "NOT_FOUND") {
set.status = 404;
return "API Endpoint Not Found";
}
return "API Error Occurred";
});
// Log all registered routes for debugging
console.log("API Routes registered:");
api.routes.forEach((route) => {
console.log(`${route.method} ${api.prefix}${route.path}`);
});
api.use(authRoute);
api.use(projectRoutes);
api.use(uploadRoutes);
api.use(downloadRoute);

View file

@ -1,8 +1,7 @@
import { Elysia } from "elysia";
import swagger from "@elysiajs/swagger";
import { ENV } from "./config/env";
import cors from "@elysiajs/cors";
import { ENV } from "./config/env";
import { api } from "./api";
const allowedOrigins = [
@ -12,9 +11,7 @@ const allowedOrigins = [
"https://canvas.planpostai.com",
];
const app = new Elysia({
tags: ["Default"],
})
const app = new Elysia()
.use(
cors({
origin: allowedOrigins,
@ -40,27 +37,31 @@ const app = new Elysia({
description: "Canvas API Documentation",
},
tags: [
{
name: "Projects",
description: "All APIs related to Projects",
},
{
name: "Uploads",
description: "All APIs related to Uploads",
},
{ name: "Default", description: "Default endpoints" },
{ name: "Projects", description: "All APIs related to Projects" },
{ name: "Uploads", description: "All APIs related to Uploads" },
{ name: "Auth", description: "Authentication related endpoints" },
{ name: "Download", description: "Download count related endpoints" },
],
},
})
)
.get("/", () => "PlanPostAI Canvas Server is running")
.use(api)
.onError(({ code, error }) => {
console.error(`App Error: ${code}`, error);
if (code === "NOT_FOUND") return "Not Found :(";
console.log("hello from app.ts under error");
console.error(error);
return "An error occurred";
});
// all routes here
app.use(api);
app.listen(ENV.SERVER_PORT, () => {
console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}:${ENV.SERVER_PORT}`);
console.log(
`Swagger docs available at ${ENV.SERVER_URL}:${ENV.SERVER_PORT}/docs`
);
console.log("All registered routes:");
app.routes.forEach((route) => {
console.log(`${route.method} ${route.path}`);
});
});