This repository was archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
145 lines (128 loc) · 3.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import express from "express";
import { graphqlExpress } from "apollo-server-express";
import { apolloUploadExpress } from "apollo-upload-server";
import bodyParser from "body-parser";
import logger from "morgan";
import cookieParser from "cookie-parser";
import cors from "cors";
import passport from "passport";
import schema from "./src/schema";
import {
instrument,
report,
createContext,
instrumentMiddleware
} from "./src/graphql/monitor";
import memoize from "./src/graphql/memoize";
import heritFromInterface from "./src/graphql/heritFromInterface";
import { execute, subscribe } from "graphql";
import { PubSub } from "graphql-subscriptions";
import {
SubscriptionServer,
SubscriptionManager
} from "subscriptions-transport-sse";
import { join } from "path";
import { MongoClient } from "mongodb";
import JwtStrategy from "./src/auth/JwtStrategy";
import AnonymousStrategy from "passport-anonymous";
import { FileStorage, MinioStorage } from "./src/storage";
import DBMigrate from "db-migrate";
const {
MINIO_ENDPOINT,
MINIO_PORT,
MINIO_KEY,
MINIO_SECRET,
MINIO_SECURE,
MINIO_BUCKET,
MONGO_URL = "mongodb://localhost:27017/popcornmoe_backend"
} = process.env;
heritFromInterface(schema);
memoize(schema);
instrument(schema);
const app = express();
let storage;
if (MINIO_ENDPOINT) {
console.log("Using Minio Storage");
storage = new MinioStorage({
endPoint: MINIO_ENDPOINT,
port: MINIO_PORT,
secure: MINIO_SECURE === "true",
accessKey: MINIO_KEY,
secretKey: MINIO_SECRET,
bucketName: MINIO_BUCKET
});
} else {
console.log("Using File Storage");
storage = new FileStorage(join(__dirname, "uploads"));
}
export const pubsub = new PubSub();
storage.register(app);
app.use(logger("dev"));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(
cors({
origin: true,
credentials: true
})
);
app.use(passport.initialize());
passport.serializeUser((user, cb) => cb(null, user));
passport.use(new AnonymousStrategy());
SubscriptionServer(
{
onSubscribe: (msg, params) => console.log(msg, params),
subscriptionManager: new SubscriptionManager({
schema,
pubsub
})
},
{
express: app,
path: "/subscriptions"
}
);
const dbm = DBMigrate.getInstance(true, {
config: {
defaultEnv: "mongo",
mongo: MONGO_URL
}
});
dbm.up().then(
() => {
MongoClient.connect(MONGO_URL).then(db => {
app.use((req, res, next) => {
req.db = db;
req.storage = storage;
req.pubsub = pubsub;
next();
});
app.post(
"/graphql",
passport.authenticate(["jwt", "anonymous"]),
apolloUploadExpress(),
instrumentMiddleware((req, res, next) =>
graphqlExpress({
schema,
context: req,
tracing: true,
formatError(e) {
console.error(e);
return e;
}
})(req, res, next)
)
);
passport.use(new JwtStrategy(db.collection("users")));
console.log("Connected on mongodb");
});
},
e => {
console.error("Migration failed", e);
dbm.down().then(() => process.exit(-1));
}
);
app.listen(3030, () => console.log("Listening on port 3030"));
process.on("unhandledRejection", error =>
console.error("unhandledRejection", error)
);