Advertisement
AdzeB

/services/internal/index.ts

Mar 9th, 2025
201
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Fastify from "fastify";
  2. import { http } from "@nitric/sdk";
  3. import { setupFastify } from "../../resources/fastify-setup";
  4. import { RoleRepository } from "../../repositories/role.repository";
  5. import { UserRepository } from "../../repositories/user.repository";
  6. import { registerInternalRoutes } from "./routes";
  7. import dbClient from "../../db";
  8. import { FastifyRequest, FastifyReply } from "fastify";
  9. import { LogtoUser } from "../../resources/logto-fastify";
  10.  
  11. // Add this before creating the Fastify instance
  12. declare module "fastify" {
  13.   export interface FastifyInstance {
  14.     db: Awaited<ReturnType<typeof dbClient>> | null;
  15.     roleRepository: RoleRepository | null;
  16.     userRepository: UserRepository | null;
  17.     verifyLogtoToken: (
  18.       request: FastifyRequest,
  19.       reply: FastifyReply
  20.     ) => Promise<boolean>;
  21.     requireScopes: (
  22.       scopes: string[]
  23.     ) => (request: FastifyRequest, reply: FastifyReply) => Promise<boolean>;
  24.     requireRoles: (
  25.       roles: string[]
  26.     ) => (request: FastifyRequest, reply: FastifyReply) => Promise<boolean>;
  27.     getLogtoUserClaims: (request: FastifyRequest) => LogtoUser | null;
  28.     getLogtoUserClaim: (request: FastifyRequest, claimName: string) => any;
  29.   }
  30. }
  31.  
  32. async function bootstrap(port: number) {
  33.   const fastify = Fastify({
  34.     logger: true,
  35.   });
  36.  
  37.   // Setup Fastify with plugins, error handlers, etc.
  38.   await setupFastify(fastify, {
  39.     serviceName: "Internal",
  40.     serviceVersion: "1.0.0",
  41.     setupRepositories: (fastify, db) => {
  42.       fastify.decorate("roleRepository", new RoleRepository(db));
  43.       fastify.decorate("userRepository", new UserRepository(db));
  44.     },
  45.     logtoConfig: {
  46.       jwksUrl: `${process.env.LOGTO_EXPRESS_ENDPOINT}/oidc/jwks`,
  47.       issuer: `${process.env.LOGTO_EXPRESS_ENDPOINT}/oidc`,
  48.       audience: "",
  49.     },
  50.   });
  51.  
  52.   // Register all routes for this service
  53.   registerInternalRoutes(fastify);
  54.  
  55.   const address = await fastify.listen({ port });
  56.  
  57.   console.log(`Server listening on ${address}`);
  58.   return fastify.server;
  59. }
  60.  
  61. // Start the server with Nitric
  62. http(bootstrap, () => {
  63.   console.log("Internal service started");
  64. });
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement