Initial commit

This commit is contained in:
Bohdan Denysenko 2024-12-29 10:56:43 +00:00 • committed by GitHub
commit 864f37a652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 45596 additions and 0 deletions

54
app/server/main.ts Normal file
View file

@ -0,0 +1,54 @@
import "dotenv/config";
import express from "express";
import cors from "cors";
import ViteExpress from "vite-express";
import { ApolloServer } from "@apollo/server";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import { expressMiddleware } from "@apollo/server/express4";
import http from "http";
import fs from "fs";
import path from "path";
import { Query, Mutation, Subscription } from "./graphql/graphql";
const __dirname = import.meta.dirname;
const HOST = process.env.VITE_HOST || "localhost";
const PORT = parseInt(process.env.VITE_PORT || "3000");
const MOTD = `\n\n\n\x1b[35m-server is listening-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
const app = express();
const server = http.createServer(app);
const typeDefs = fs.readFileSync(
path.join(__dirname, "graphql", "schema.graphql"),
"utf8",
);
const resolvers = {
Query,
// Mutation,
// Subscription,
};
const apollo = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
});
await apollo.start();
app.use(
"/graphql",
cors<cors.CorsRequest>(),
express.json(),
expressMiddleware(apollo),
);
await new Promise<void>((resolve) =>
server.listen({ hostname: HOST, port: PORT }, resolve),
);
console.log(MOTD);
ViteExpress.bind(app, server);