update 00000
This commit is contained in:
parent
f9da806ec9
commit
40d600ab5b
3 changed files with 148 additions and 54 deletions
|
|
@ -2,21 +2,36 @@ import Elysia, { t } from "elysia";
|
||||||
import { generateToken, getUserData, updateUser } from "./auth.controller";
|
import { generateToken, getUserData, updateUser } from "./auth.controller";
|
||||||
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
||||||
|
|
||||||
export const authRoute = new Elysia({
|
export const authRoute = new Elysia({ prefix: "/auth" });
|
||||||
prefix: "/api/auth",
|
|
||||||
tags: ["Auth"],
|
|
||||||
detail: {
|
|
||||||
description: "Routes for managing users",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
authRoute.get(
|
authRoute.get(
|
||||||
"/user/:userId",
|
"/user/:userId",
|
||||||
async ({ params: { userId } }) => await getUserData(userId),
|
async ({ params: { userId } }) => await getUserData(userId),
|
||||||
{
|
{
|
||||||
|
detail: {
|
||||||
|
tags: ["Auth"],
|
||||||
|
summary: "Get user data",
|
||||||
|
description: "Retrieve user data by user ID",
|
||||||
|
},
|
||||||
params: t.Object({
|
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",
|
"/user/update/:userId",
|
||||||
async ({ params: { userId }, body }) => await updateUser(userId, body),
|
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({
|
params: t.Object({
|
||||||
userId: t.String(),
|
userId: t.String({ description: "The user ID to update" }),
|
||||||
}),
|
}),
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
paid_status: t.String(),
|
paid_status: t.String({ description: "User payment status" }),
|
||||||
package_expire_date: t.String(),
|
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(
|
authRoute.get(
|
||||||
"/generate-token/:userId",
|
"/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);
|
const authData = await verifyAuth(cookie);
|
||||||
if (authData.status !== 200) {
|
if (authData.status !== 200) {
|
||||||
return authData;
|
return authData;
|
||||||
|
|
@ -57,4 +111,24 @@ authRoute.get("/user/me", async ({ cookie }) => {
|
||||||
return response;
|
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(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,33 @@
|
||||||
import Elysia from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import { projectRoutes } from "./project/project.route";
|
import { projectRoutes } from "./project/project.route";
|
||||||
import { uploadRoutes } from "./upload/upload.route";
|
import { uploadRoutes } from "./upload/upload.route";
|
||||||
import { authRoute } from "./auth/auth.route";
|
import { authRoute } from "./auth/auth.route";
|
||||||
import { downloadRoute } from "./downloadCount/download.count.route";
|
import { downloadRoute } from "./downloadCount/download.count.route";
|
||||||
|
|
||||||
export const api = new Elysia({});
|
export const api = new Elysia({ prefix: "/api" })
|
||||||
api.get("/", () => {
|
.get("/", () => {
|
||||||
|
console.log("Root endpoint accessed");
|
||||||
return "Hello from PlanPostAI Canvas API";
|
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);
|
|
||||||
|
|
|
||||||
37
src/app.ts
37
src/app.ts
|
|
@ -1,8 +1,7 @@
|
||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import swagger from "@elysiajs/swagger";
|
import swagger from "@elysiajs/swagger";
|
||||||
|
|
||||||
import { ENV } from "./config/env";
|
|
||||||
import cors from "@elysiajs/cors";
|
import cors from "@elysiajs/cors";
|
||||||
|
import { ENV } from "./config/env";
|
||||||
import { api } from "./api";
|
import { api } from "./api";
|
||||||
|
|
||||||
const allowedOrigins = [
|
const allowedOrigins = [
|
||||||
|
|
@ -12,9 +11,7 @@ const allowedOrigins = [
|
||||||
"https://canvas.planpostai.com",
|
"https://canvas.planpostai.com",
|
||||||
];
|
];
|
||||||
|
|
||||||
const app = new Elysia({
|
const app = new Elysia()
|
||||||
tags: ["Default"],
|
|
||||||
})
|
|
||||||
.use(
|
.use(
|
||||||
cors({
|
cors({
|
||||||
origin: allowedOrigins,
|
origin: allowedOrigins,
|
||||||
|
|
@ -40,27 +37,31 @@ const app = new Elysia({
|
||||||
description: "Canvas API Documentation",
|
description: "Canvas API Documentation",
|
||||||
},
|
},
|
||||||
tags: [
|
tags: [
|
||||||
{
|
{ name: "Default", description: "Default endpoints" },
|
||||||
name: "Projects",
|
{ name: "Projects", description: "All APIs related to 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" },
|
||||||
name: "Uploads",
|
|
||||||
description: "All APIs related to Uploads",
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
.get("/", () => "PlanPostAI Canvas Server is running")
|
||||||
|
.use(api)
|
||||||
.onError(({ code, error }) => {
|
.onError(({ code, error }) => {
|
||||||
|
console.error(`App Error: ${code}`, error);
|
||||||
if (code === "NOT_FOUND") return "Not Found :(";
|
if (code === "NOT_FOUND") return "Not Found :(";
|
||||||
console.log("hello from app.ts under error");
|
return "An error occurred";
|
||||||
console.error(error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// all routes here
|
|
||||||
app.use(api);
|
|
||||||
|
|
||||||
app.listen(ENV.SERVER_PORT, () => {
|
app.listen(ENV.SERVER_PORT, () => {
|
||||||
console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}:${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}`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue