mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
81 lines
1.8 KiB
TypeScript
81 lines
1.8 KiB
TypeScript
import { appRouter } from "@/server/api/root";
|
|
import { validateRequest } from "@/server/auth/auth";
|
|
import { api } from "@/utils/api";
|
|
import { createServerSideHelpers } from "@trpc/react-query/server";
|
|
import type { GetServerSidePropsContext, NextPage } from "next";
|
|
import dynamic from "next/dynamic";
|
|
import "swagger-ui-react/swagger-ui.css";
|
|
import { useEffect, useState } from "react";
|
|
import superjson from "superjson";
|
|
|
|
const SwaggerUI = dynamic(() => import("swagger-ui-react"), { ssr: false });
|
|
|
|
const Home: NextPage = () => {
|
|
const { data } = api.settings.getOpenApiDocument.useQuery();
|
|
const [spec, setSpec] = useState({});
|
|
|
|
useEffect(() => {
|
|
// Esto solo se ejecutará en el cliente
|
|
if (data) {
|
|
const protocolAndHost = `${window.location.protocol}//${window.location.host}/api`;
|
|
const newSpec = {
|
|
...data,
|
|
servers: [{ url: protocolAndHost }],
|
|
externalDocs: {
|
|
url: `${protocolAndHost}/settings.getOpenApiDocument`,
|
|
},
|
|
};
|
|
setSpec(newSpec);
|
|
}
|
|
}, [data]);
|
|
|
|
return (
|
|
<div className="h-screen bg-white">
|
|
<SwaggerUI spec={spec} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Home;
|
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
|
const { req, res } = context;
|
|
const { user, session } = await validateRequest(context.req, context.res);
|
|
if (!user) {
|
|
return {
|
|
redirect: {
|
|
permanent: true,
|
|
destination: "/",
|
|
},
|
|
};
|
|
}
|
|
const helpers = createServerSideHelpers({
|
|
router: appRouter,
|
|
ctx: {
|
|
req: req as any,
|
|
res: res as any,
|
|
db: null as any,
|
|
session: session,
|
|
user: user,
|
|
},
|
|
transformer: superjson,
|
|
});
|
|
if (user.rol === "user") {
|
|
const result = await helpers.user.byAuthId.fetch({
|
|
authId: user.id,
|
|
});
|
|
|
|
if (!result.canAccessToAPI) {
|
|
return {
|
|
redirect: {
|
|
permanent: true,
|
|
destination: "/",
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
return {
|
|
props: {},
|
|
};
|
|
}
|