|
1 | 1 | import { DbService } from "./db/db.service"; |
| 2 | +import { LogService } from "./w3id/log-service"; |
2 | 3 | import { GraphQLServer } from "./protocol/graphql-server"; |
3 | 4 | import { registerHttpRoutes } from "./http/server"; |
4 | 5 | import fastify, { |
5 | | - FastifyInstance, |
6 | | - FastifyRequest, |
7 | | - FastifyReply, |
| 6 | + FastifyInstance, |
| 7 | + FastifyRequest, |
| 8 | + FastifyReply, |
8 | 9 | } from "fastify"; |
9 | 10 | import { renderVoyagerPage } from "graphql-voyager/middleware"; |
10 | 11 | import { createYoga } from "graphql-yoga"; |
11 | 12 | import dotenv from "dotenv"; |
12 | 13 | import path from "path"; |
13 | | -import neo4j from "neo4j-driver"; |
| 14 | +import neo4j, { Driver } from "neo4j-driver"; |
| 15 | +import { W3ID } from "./w3id/w3id"; |
14 | 16 |
|
15 | 17 | dotenv.config({ path: path.resolve(__dirname, "../../../.env") }); |
16 | 18 |
|
17 | 19 | class EVault { |
18 | | - server: FastifyInstance; |
19 | | - graphqlServer: GraphQLServer; |
20 | | - |
21 | | - constructor() { |
22 | | - const uri = process.env.NEO4J_URI || "bolt://localhost:7687"; |
23 | | - const user = process.env.NEO4J_USER || "neo4j"; |
24 | | - const password = process.env.NEO4J_PASSWORD || "neo4j"; |
25 | | - |
26 | | - if ( |
27 | | - !process.env.NEO4J_URI || |
28 | | - !process.env.NEO4J_USER || |
29 | | - !process.env.NEO4J_PASSWORD |
30 | | - ) { |
31 | | - console.warn( |
32 | | - "Using default Neo4j connection parameters. Set NEO4J_URI, NEO4J_USER, and NEO4J_PASSWORD environment variables for custom configuration.", |
33 | | - ); |
34 | | - } |
35 | | - |
36 | | - const driver = neo4j.driver(uri, neo4j.auth.basic(user, password)); |
37 | | - const dbService = new DbService(driver); |
38 | | - this.graphqlServer = new GraphQLServer(dbService); |
39 | | - |
40 | | - // Create Fastify server |
41 | | - this.server = fastify({ |
42 | | - logger: true, |
43 | | - }); |
| 20 | + server: FastifyInstance; |
| 21 | + graphqlServer: GraphQLServer; |
| 22 | + logService: LogService; |
| 23 | + driver: Driver; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + const uri = process.env.NEO4J_URI || "bolt://localhost:7687"; |
| 27 | + const user = process.env.NEO4J_USER || "neo4j"; |
| 28 | + const password = process.env.NEO4J_PASSWORD || "neo4j"; |
| 29 | + |
| 30 | + if ( |
| 31 | + !process.env.NEO4J_URI || |
| 32 | + !process.env.NEO4J_USER || |
| 33 | + !process.env.NEO4J_PASSWORD |
| 34 | + ) { |
| 35 | + console.warn( |
| 36 | + "Using default Neo4j connection parameters. Set NEO4J_URI, NEO4J_USER, and NEO4J_PASSWORD environment variables for custom configuration." |
| 37 | + ); |
44 | 38 | } |
45 | 39 |
|
46 | | - async initialize() { |
47 | | - // Register HTTP routes |
48 | | - await registerHttpRoutes(this.server); |
| 40 | + this.driver = neo4j.driver(uri, neo4j.auth.basic(user, password)); |
49 | 41 |
|
50 | | - // Create Yoga instance with the schema from GraphQLServer |
51 | | - const yoga = createYoga({ |
52 | | - schema: this.graphqlServer.getSchema(), |
53 | | - graphiql: true, |
54 | | - }); |
| 42 | + const dbService = new DbService(this.driver); |
| 43 | + this.logService = new LogService(this.driver); |
| 44 | + this.graphqlServer = new GraphQLServer(dbService); |
| 45 | + |
| 46 | + this.server = fastify({ |
| 47 | + logger: true, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + async initialize() { |
| 52 | + await registerHttpRoutes(this.server); |
| 53 | + |
| 54 | + const w3id = await W3ID.get({ |
| 55 | + id: process.env.W3ID as string, |
| 56 | + driver: this.driver, |
| 57 | + password: process.env.ENCRYPTION_PASSWORD, |
| 58 | + }); |
55 | 59 |
|
56 | | - // Mount GraphQL endpoint |
57 | | - this.server.route({ |
58 | | - url: "/graphql", |
59 | | - method: ["GET", "POST", "OPTIONS"], |
60 | | - handler: async (req: FastifyRequest, reply: FastifyReply) => { |
61 | | - const response = await yoga.handleNodeRequest( |
62 | | - req.raw, |
63 | | - reply.raw, |
64 | | - ); |
65 | | - response.headers.forEach((value, key) => { |
66 | | - reply.header(key, value); |
67 | | - }); |
68 | | - reply.status(response.status); |
69 | | - reply.send(response.body); |
70 | | - return reply; |
71 | | - }, |
| 60 | + const yoga = createYoga({ |
| 61 | + schema: this.graphqlServer.getSchema(), |
| 62 | + graphiql: true, |
| 63 | + }); |
| 64 | + // change |
| 65 | + |
| 66 | + this.server.route({ |
| 67 | + url: "/graphql", |
| 68 | + method: ["GET", "POST", "OPTIONS"], |
| 69 | + handler: async (req: FastifyRequest, reply: FastifyReply) => { |
| 70 | + const response = await yoga.fetch(req.url, { |
| 71 | + method: req.method, |
| 72 | + headers: req.headers, |
| 73 | + body: req.method === "POST" ? req.body : undefined, |
| 74 | + }); |
| 75 | + reply.status(response.status); |
| 76 | + const headers: Record<string, string> = {}; |
| 77 | + response.headers.forEach((value, key) => { |
| 78 | + headers[key] = value; |
72 | 79 | }); |
| 80 | + reply.headers(headers); |
| 81 | + reply.send(response.body); |
| 82 | + return reply; |
| 83 | + }, |
| 84 | + }); |
73 | 85 |
|
74 | | - // Mount Voyager endpoint |
75 | | - this.server.get( |
76 | | - "/voyager", |
77 | | - (req: FastifyRequest, reply: FastifyReply) => { |
78 | | - reply.type("text/html").send( |
79 | | - renderVoyagerPage({ |
80 | | - endpointUrl: "/graphql", |
81 | | - }), |
82 | | - ); |
83 | | - }, |
84 | | - ); |
85 | | - } |
| 86 | + // Mount Voyager endpoint |
| 87 | + this.server.get("/voyager", (req: FastifyRequest, reply: FastifyReply) => { |
| 88 | + reply.type("text/html").send( |
| 89 | + renderVoyagerPage({ |
| 90 | + endpointUrl: "/graphql", |
| 91 | + }) |
| 92 | + ); |
| 93 | + }); |
| 94 | + } |
86 | 95 |
|
87 | | - async start() { |
88 | | - await this.initialize(); |
89 | | - |
90 | | - const port = process.env.NOMAD_PORT_http || process.env.PORT || 4000; |
91 | | - |
92 | | - await this.server.listen({ port: Number(port), host: "0.0.0.0" }); |
93 | | - console.log(`Server started on http://0.0.0.0:${port}`); |
94 | | - console.log( |
95 | | - `GraphQL endpoint available at http://0.0.0.0:${port}/graphql`, |
96 | | - ); |
97 | | - console.log( |
98 | | - `GraphQL Voyager available at http://0.0.0.0:${port}/voyager`, |
99 | | - ); |
100 | | - console.log( |
101 | | - `API Documentation available at http://0.0.0.0:${port}/documentation`, |
102 | | - ); |
103 | | - } |
| 96 | + async start() { |
| 97 | + await this.initialize(); |
| 98 | + |
| 99 | + const port = process.env.NOMAD_PORT_http || process.env.PORT || 4000; |
| 100 | + |
| 101 | + await this.server.listen({ port: Number(port), host: "0.0.0.0" }); |
| 102 | + console.log(`Server started on http://0.0.0.0:${port}`); |
| 103 | + console.log(`GraphQL endpoint available at http://0.0.0.0:${port}/graphql`); |
| 104 | + console.log(`GraphQL Voyager available at http://0.0.0.0:${port}/voyager`); |
| 105 | + console.log(`API Documentation available at http://0.0.0.0:${port}/docs`); |
| 106 | + } |
104 | 107 | } |
105 | 108 |
|
106 | 109 | const evault = new EVault(); |
|
0 commit comments