-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystone.ts
146 lines (137 loc) · 4.44 KB
/
keystone.ts
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
146
import dotenv from "dotenv";
import { config } from "@keystone-6/core";
import { TypeInfo } from ".keystone/types";
import { uploadFile, getUrl } from "./s3";
dotenv.config();
import * as Models from "./models";
import { withAuth, session } from "./auth";
const multer = require("multer");
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
export default withAuth(
config<TypeInfo>({
server: {
port: 8080,
cors: {
origin: [
"http://localhost:3000",
"http://slop.entroponics.com.s3-website-us-east-1.amazonaws.com",
],
},
maxFileSize: 200 * 1024 * 1024,
//custom endpoint used to upload movie images to S3 bucket
extendExpressApp: (app, commonContext) => {
app.get("/api/movie/:movieKey", async (req, res) => {
const { movieKey } = req.params;
if (!movieKey) {
return res.send(404);
}
try {
const url = await getUrl({ key: movieKey });
return res.json(url);
} catch (e) {
res
.status(500)
.send({ message: "Internal server error. Please try again" });
}
});
app.post(
"/api/movie",
upload.single("movieImage"),
async (req, res) => {
const userId = req.body.userId;
const context = await commonContext.withRequest(req, res);
const fileData = {
movieTitle: req.body.movieTitle,
//@ts-ignore
movieImage: req.file,
};
const userIsAuthorized = () => {
return context.query.User.findOne({
where: { id: userId },
})
.then((res) => {
if (res === null) {
return false;
} else {
return true;
}
})
.catch(console.error);
};
const authResult = await userIsAuthorized();
if (authResult) {
const s3upload = await uploadFile(fileData);
res.json(s3upload);
} else {
res
.status(401)
.send({ message: "User does not have authorization" });
}
// - - ***** if we want this endpoint to be responsible for
// - - ***** creating an entire slop, this might be where to start
// const movie = await context.query.Movie.createOne({
// data: {
// title: 'movie title',
// author: - - - etc
// },
// query: 'id name posts { id title }', //<== identifies what fields you want in response
// });
},
);
},
},
db: {
provider: "mysql",
url: `mysql://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:6652/${process.env.DB_NAME}`,
enableLogging: true,
idField: { kind: "uuid" },
useMigrations: true,
},
telemetry: false,
graphql: {
playground: true,
apolloConfig: {
introspection: true,
},
},
storage: {
my_S3_images: {
kind: "s3",
type: "image",
bucketName: process.env.S3_BUCKET_NAME || "keystone-test",
region: process.env.S3_REGION || "us-east-1",
accessKeyId: process.env.S3_ACCESS_KEY_ID || "keystone",
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || "keystone",
signed: { expiry: 5000 },
forcePathStyle: true,
},
my_S3_sounds: {
kind: "s3",
type: "file",
bucketName: process.env.S3_BUCKET_NAME || "keystone-test",
region: process.env.S3_REGION || "us-east-1",
accessKeyId: process.env.S3_ACCESS_KEY_ID || "keystone",
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || "keystone",
signed: { expiry: 5000 },
forcePathStyle: true,
},
},
lists: Models,
session,
}),
);
//***********Images API
//If support for image fields is enabled in the system,
//then an images API will be made available on the context object.
//This API takes advantage of the following types:
// type ImageMode = 'local';
// type ImageExtension = 'jpg' | 'png' | 'webp' | 'gif';
// type ImageData = {
// mode: ImageMode;
// id: string;
// extension: ImageExtension;
// filesize: number;
// width: number;
// height: number;
// };