~app/server.ts
.ts
TypeScript
(application/typescript)
// 1st-party
import { InMemoryCacheAdapter } from "@ethicdevs/fastify-stream-react-views";
import {
  AppServer,
  makeAppServer,
  startAppServer,
  stopAppServerAndExit,
} from "@ethicdevs/react-monolith";
// 3rd-party
import cuid from "cuid";
import fastifyCookie, { CookieSerializeOptions } from "@fastify/cookie";
import fastifyCustomSession, {
  PrismaSessionAdapter,
} from "@ethicdevs/fastify-custom-session";
import fastifyFormBody from "@fastify/formbody";
import fastifyGitServer from "@ethicdevs/fastify-git-server";
import fastifyServeStatic from "fastify-static";
import loadPrismJsLanguages from "prismjs/components/";
// generated via script[generate:prisma]
import { GlobalRole, PrismaClient } from "@prisma/client";
// app root
import * as Paths from "../paths";
import { version as appVersion } from "../package.json";
// app
import { Const } from "./const";
import { Env } from "./env";
import { codeAnalysisPlugin, cryptoPlugin, prismaPlugin } from "./plugins";
import { makeGitServerService } from "./services/gitServer";
import {
  getEnv,
  localAppDomainPreHandler,
  makeRequestHandler,
} from "./utils/server";

let server: null | AppServer = null;

async function main(): Promise<AppServer> {
  const env = getEnv();
  const prisma = new PrismaClient();

  const depsBaseUrl = `/${Paths.PUBLIC_FOLDER_NAME}/${Paths.ASSET_DEPS_FOLDER_NAME}`;
  const publicBaseUrl = `/${Paths.PUBLIC_FOLDER_NAME}`;

  const cookiesOpts: CookieSerializeOptions = {
    domain: `.${Env.DEPLOYMENT_DOMAIN}`,
    httpOnly: true,
    path: "/",
    secure: false,
    sameSite: "lax",
    signed: true,
  };

  server = await makeAppServer(Env.HOST, Env.PORT, {
    appName: Const.APP_NAME,
    appVersion,
    env,
    a11y: {
      localeDirection: "ltr",
    },
    assets: {
      depsFolder: Paths.ASSET_DEPS_FOLDER_NAME,
      importPrefix: publicBaseUrl,
    },
    cacheAdapter: new InMemoryCacheAdapter({
      maxSize: Const.SSR_CACHE_MAX_SIZE_BYTES,
    }),
    featureFlags: {
      withIncrementalBuild: true,
      withImportsMap: true,
      withInstantRouter: true,
      withStyledSSR: true,
      withTreeShaking: true,
    },
    paths: {
      assetsOutFolder: Paths.PUBLIC_FOLDER,
      distFolder: Paths.DIST_FOLDER,
      islandsFolder: Paths.ISLANDS_FOLDER,
      rootFolder: Paths.ROOT_FOLDER,
      routesFile: Paths.ROUTES_FILE,
      viewsFolder: Paths.VIEWS_FOLDER,
    },
    externalDependencies: {
      "cross-fetch": "CrossFetch",
      "markdown-to-jsx": "MarkdownToJSX",
      prismjs: "Prism",
    },
    baseHeadTags: [
      {
        kind: "link",
        rel: "manifest",
        href: "/manifest.json",
      },
      {
        kind: "meta",
        name: "shortcut icon",
        content: "/public/favicon.ico",
      },
      {
        kind: "meta",
        name: "viewport",
        content:
          "minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no, user-scalable=no, viewport-fit=cover",
      },
      {
        kind: "meta",
        name: "mobile-web-app-capable",
        content: "yes",
      },
      {
        kind: "meta",
        name: "apple-mobile-web-app-capable",
        content: "yes",
      },
      {
        kind: "meta",
        name: "apple-mobile-web-app-status-bar-style",
        content: "default",
      },
      {
        kind: "meta",
        name: "format-detection",
        content: "telephone=no",
      },
      {
        kind: "meta",
        name: "apple-mobile-web-app-title",
        content: Const.APP_NAME,
      },
      {
        kind: "meta",
        name: "og:type",
        content: "website",
      },
      {
        kind: "meta",
        name: "og:site_name",
        content: Const.APP_NAME,
      },
      {
        kind: "meta",
        name: "og:title",
        content: Const.APP_NAME,
      },
      {
        kind: "meta",
        name: "og:image",
        content: `${Env.DEPLOYMENT_SCHEME}://${Env.DEPLOYMENT_DOMAIN}/public/assets/social-icon.png`,
      },
      {
        kind: "meta",
        name: "twitter:card",
        content: "summary",
      },
      {
        kind: "meta",
        name: "twitter:title",
        content: Const.APP_NAME,
      },
      {
        kind: "meta",
        name: "twitter:image",
        content: `${Env.DEPLOYMENT_SCHEME}://${Env.DEPLOYMENT_DOMAIN}/public/assets/social-icon-192.png`,
      },
    ],
    baseScriptTags: [
      // add ES Module shim as long as browsers (firefox) not all supports it
      {
        async: true,
        id: "es-importmap-shim",
        src: `${depsBaseUrl}/es-module-shims.production.min.js`,
        type: "application/javascript",
      },
      /*{
        async: true,
        defer: false,
        id: "importmap-service-worker-register",
        src: `/register-imsw.js`,
        type: "module",
      },*/
      {
        id: "prism.js-config",
        type: "application/javascript",
        textContent: `if (typeof window !== "undefined") {
  window.Prism = window.Prism || { plugins: {} };
  window.Prism.manual = true;
  window.Prism.plugins.autoloader = {
    use_minifed: ${env === "production" ? "true" : "false"},
    languages_path: "/public/assets/prism/components/",
  };
}`,
      },
    ],
    setupServerBeforeRoutes(s) {
      // add a preHandler to warn against bad localhost usage (so cookies works)
      s.addHook("preHandler", localAppDomainPreHandler);

      // load Prism languages/grammars to enable SSR syntax highlighting
      try {
        loadPrismJsLanguages();
      } catch (err) {
        console.error(
          "Could not load Prism.JS languages, syntax highlighting may not work as expected.",
          `Error: ${(err as Error).message}`
        );
      }

      // add a reply decorator so we can reply with common props from app
      s.decorateReply("makeRequestHandler", makeRequestHandler);

      // register the code analysis plugin
      s.register(codeAnalysisPlugin);

      // register the crypto plugin
      s.register(cryptoPlugin).after(() => {
        const gitService = makeGitServerService({
          cryptoService: s.cryptoService,
          request: {
            prisma,
          } as any,
        });

        // register the Git Server plugin and bind the resolvers/callbacks
        // to the gitService instance made above
        s.register(fastifyGitServer, {
          withSideBandMessages: true,
          authorizationResolver: gitService.authorizationResolver,
          repositoryResolver: gitService.repositoryResolver,
          onPush: gitService.onPushEvent,
        });
      });

      // register the Prisma client plugin
      s.register(prismaPlugin, { prisma });

      // register the plugin to be able to decode Form's body (multipart)
      s.register(fastifyFormBody);

      // register the plugin to serve static assets from public folder
      s.register(fastifyServeStatic, {
        root: Paths.PUBLIC_FOLDER,
        prefix: publicBaseUrl,
      });

      // register the cookies plugin
      s.register(fastifyCookie, {
        secret: Env.COOKIE_SECRET,
        parseOptions: cookiesOpts,
      });

      // register the session plugin + prisma session store adapter
      s.register(fastifyCustomSession, {
        cookieName: Env.COOKIE_NAME,
        cookieOptions: cookiesOpts,
        getUniqId: cuid as () => string,
        password: Env.COOKIE_SECRET,
        storeAdapter: new PrismaSessionAdapter(prisma) as any,
        ttl: Const.SESSION_TTL_SECONDS,
        initialSession: {
          sessionId: null,
          authenticated: false,
          curr_user_avatar_uri: null,
          curr_user_uid: null,
          curr_user_username: null,
          curr_user_role: GlobalRole.GUEST,
          flash_message: null,
          flash_message_shown_once: false,
          two_factor_lock: false,
          two_factor_complete: false,
        },
      });

      // serve the import-map service worker interceptor
      s.get("/interceptor-imsw.js", {}, async (_, reply) => {
        return reply.sendFile("interceptor-imsw.js");
      });

      // serve the import-map service worker register
      s.get("/register-imsw.js", {}, async (_, reply) => {
        return reply.sendFile("register-imsw.js");
      });

      // serve the app manifest.json
      s.get("/manifest.json", {}, async (_, reply) => {
        return reply.sendFile("manifest.json");
      });
    },
  });

  await startAppServer(server);

  return server;
}

// Catch errors that are fatal
["unhandledRejection", "uncaughtException"].forEach((exception) => {
  process.on(exception, async (reason: Error) => {
    await stopAppServerAndExit(server, reason);
  });
});

// Catch standard linux signals to kill a daemon
["SIGQUIT", "SIGTERM", "SIGINT"].forEach((killSignal) => {
  process.on(killSignal, async () => {
    await stopAppServerAndExit(server);
  });
});

// Start the application server
main()
  .then((server) => {
    // safe because it wont start if null
    const { $config, $host, $port } = server.reactMonolith!;
    console.log(
      `[🚀][${$config.env}] App Server ready at http://${$host}:${$port} !`
    );
  })
  .catch(async (err) => {
    const error = err as Error;
    console.error(`[❌] Cannot start App Server. Error: ${error.message}`);
    await stopAppServerAndExit(null, error);
  });


GitFOSS • v0.2.0 (#421408f) • MIT License

GitFOSS