Skip to content

Commit f0a238a

Browse files
unify serverless and master
* basic setup for aws * rewrite import/exports for compatibility * add dotenv * aws working * add ec2 managing lambda * manager function logic * add client-ec2 and logging * finish off initial start/stop logic * add pubsub to lambda * add dotenv plugin * use INSTANCE env var * redis port process env * npm i * Express version runs locally! Changes made: 1) New env variable EXPRESS, that should be passed as true, to run express version of the backend(that is with subscriptions), else false, to run the aws version of the backend, or to deploy to AWS-Lambda. 2) Changed the scripts in package.json, a) start_aws to run index.js of aws version. b) start_express to run the express version. 3) Updated the resolvers to use mongoose 6.X and now both aws and express version share common files such as models, resolvers etc. * chor: required changes implemented. * removed env variable for conditioning subs. Instead used "_HANDLER" , which is not null during aws/lambda runs. * renamed express_server to index and changed start script to be npm start. * Unified the AWS and Express Branches. (#150) * Express version runs locally! Changes made: 1) New env variable EXPRESS, that should be passed as true, to run express version of the backend(that is with subscriptions), else false, to run the aws version of the backend, or to deploy to AWS-Lambda. 2) Changed the scripts in package.json, a) start_aws to run index.js of aws version. b) start_express to run the express version. 3) Updated the resolvers to use mongoose 6.X and now both aws and express version share common files such as models, resolvers etc. * chor: required changes implemented. * removed env variable for conditioning subs. Instead used "_HANDLER" , which is not null during aws/lambda runs. * renamed express_server to index and changed start script to be npm start. Co-authored-by: Aadi Bajpai <[email protected]>
1 parent 45c327c commit f0a238a

File tree

17 files changed

+20589
-3291
lines changed

17 files changed

+20589
-3291
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
DB = mongodb+srv://username:[email protected]/test?retryWrites=true&w=majority
22
JWT_SECRET = somesupersecretstring
3-
REDIS_AUTH = yourredispasswd
4-
REDIS_URL = yourredisurl
5-
REDIS_PORT = yourredisport
3+
REDIS_AUTH = Redis password you get while provisioning a Redis DB
4+
REDIS_URL = Redis DB connection string
5+
REDIS_PORT = Any port you want.

graphql/resolvers.js

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { AuthenticationError, UserInputError, withFilter } from "apollo-server-express";
2-
import bcrypt from "bcryptjs";
3-
import jwt from "jsonwebtoken";
4-
import { customAlphabet } from "nanoid";
5-
import geolib from "geolib";
1+
const { AuthenticationError, UserInputError, withFilter } = require("apollo-server-express");
2+
const bcrypt = require("bcryptjs");
3+
const jwt = require("jsonwebtoken");
4+
const { customAlphabet } = require("nanoid");
5+
const geolib = require("geolib");
66
const { isPointWithinRadius } = geolib;
7-
import Beacon from "../models/beacon.js";
8-
import Landmark from "../models/landmark.js";
9-
import { User } from "../models/user.js";
7+
const Beacon = require("../models/beacon.js");
8+
const Landmark = require("../models/landmark.js");
9+
const { User } = require("../models/user.js");
1010

1111
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1212
// even if we generate 10 IDs per hour,
@@ -16,12 +16,15 @@ const nanoid = customAlphabet(alphabet, 6);
1616
const resolvers = {
1717
Query: {
1818
hello: () => "Hello world!",
19-
me: (_parent, _args, { user }) => user.populate("beacons.leader"),
19+
me: async (_parent, _args, { user }) => {
20+
await user.populate("beacons.leader beacons.landmarks");
21+
return user;
22+
},
2023
beacon: async (_parent, { id }, { user }) => {
21-
const beacon = await Beacon.findById(id).populate("landmarks").populate("leader");
24+
const beacon = await Beacon.findById(id).populate("landmarks leader");
2225
if (!beacon) return new UserInputError("No beacon exists with that id.");
2326
// return beacon iff user in beacon
24-
if (beacon.leader === user.id || beacon.followers.includes(user))
27+
if (beacon.leader.id === user.id || beacon.followers.includes(user))
2528
return new Error("User should be a part of beacon");
2629
return beacon;
2730
},
@@ -105,20 +108,18 @@ const resolvers = {
105108
location: beacon.startLocation,
106109
...beacon,
107110
});
108-
const newBeacon = await beaconDoc.save().then(b => b.populate("leader").execPopulate());
109-
111+
const newBeacon = await beaconDoc.save().then(b => b.populate("leader"));
110112
user.beacons.push(newBeacon.id);
111113
await user.save();
112114

113115
return newBeacon;
114116
},
115117

116118
changeBeaconDuration: async (_, { newExpiresAt, beaconID }, { user }) => {
117-
const beacon = await Beacon.findById(beaconID).populate("leader");
119+
const beacon = await Beacon.findById(beaconID);
118120

119121
if (!beacon) return new UserInputError("No beacon exists with that id.");
120-
if (beacon.leader.id != user.id)
121-
return new Error("Only the leader is allowed to change the beacon duration.");
122+
if (beacon.leader != user.id) return new Error("Only the leader is allowed to change the beacon duration.");
122123
if (beacon.startsAt.getTime() > newExpiresAt) return Error("Beacon can not expire before it has started.");
123124

124125
beacon.expiresAt = newExpiresAt;
@@ -136,7 +137,8 @@ const resolvers = {
136137

137138
beacon.followers.push(user);
138139
console.log("user joined beacon: ", user);
139-
await beacon.save().then(b => b.populate("leader").execPopulate());
140+
await beacon.save().then(b => b.populate("leader"));
141+
140142
pubsub.publish("BEACON_JOINED", { beaconJoined: user, beaconID: beacon.id });
141143

142144
user.beacons.push(beacon.id);
@@ -199,29 +201,29 @@ const resolvers = {
199201
return beacon;
200202
},
201203
},
202-
203-
Subscription: {
204-
beaconLocation: {
205-
subscribe: withFilter(
206-
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_LOCATION"]),
207-
(payload, variables) => payload.beaconID === variables.id
208-
),
204+
...(process.env._HANDLER == null && {
205+
Subscription: {
206+
beaconLocation: {
207+
subscribe: withFilter(
208+
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_LOCATION"]),
209+
(payload, variables) => payload.beaconID === variables.id
210+
),
211+
},
212+
userLocation: {
213+
subscribe: withFilter(
214+
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_LOCATION"]),
215+
(payload, variables, { user }) => {
216+
return payload.beaconID === variables.id && payload.userLocation.id !== user.id; // account for self updates
217+
}
218+
),
219+
},
220+
beaconJoined: {
221+
subscribe: withFilter(
222+
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_JOINED"]),
223+
(payload, variables) => payload.beaconID === variables.id
224+
),
225+
},
209226
},
210-
userLocation: {
211-
subscribe: withFilter(
212-
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_LOCATION"]),
213-
(payload, variables, { user }) => {
214-
return payload.beaconID === variables.id && payload.userLocation.id !== user.id; // account for self updates
215-
}
216-
),
217-
},
218-
beaconJoined: {
219-
subscribe: withFilter(
220-
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_JOINED"]),
221-
(payload, variables) => payload.beaconID === variables.id
222-
),
223-
},
224-
},
227+
}),
225228
};
226-
227-
export default resolvers;
229+
module.exports = resolvers;

graphql/schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { gql } from "apollo-server-express";
1+
const { gql } = require("apollo-server-express");
22

33
const typeDefs = gql`
44
type Location {
@@ -119,4 +119,4 @@ const typeDefs = gql`
119119
}
120120
`;
121121

122-
export default typeDefs;
122+
module.exports = typeDefs;

index.js renamed to index.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ import pubsub from "./pubsub.js";
1515

1616
const server = new ApolloServer({
1717
schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }), permissions),
18-
// schema: makeExecutableSchema({ typeDefs, resolvers }), // to temp disable shield on dev
18+
// schema: makeExecutableSchema({ typeDefs, resolvers }), // to temp disable shield on dev
1919
context: async ({ req, connection }) => {
2020
// initialize context even if it comes from subscription connection
21-
// TODO: cleanup
2221
if (connection) {
2322
return { user: connection.context.user, pubsub };
2423
}

models/beacon.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import mongoose from "mongoose";
1+
const mongoose = require("mongoose");
22

3-
import LocationSchema from "./location.js";
4-
import { UserSchema } from "./user.js";
3+
const LocationSchema = require("./location.js");
4+
const { UserSchema } = require("./user.js");
55

66
const { Schema, model } = mongoose;
77

@@ -22,4 +22,4 @@ const beaconSchema = new Schema(
2222
}
2323
);
2424

25-
export default model("Beacon", beaconSchema, "Beacons"); // specify collection name
25+
module.exports = model("Beacon", beaconSchema, "Beacons"); // specify collection name

models/landmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import mongoose from "mongoose";
1+
const mongoose = require("mongoose");
22

3-
import LocationSchema from "./location.js";
3+
const LocationSchema = require("./location.js");
44

55
const { Schema, model } = mongoose;
66

@@ -15,4 +15,4 @@ const landmarkSchema = new Schema(
1515
}
1616
);
1717

18-
export default model("Landmark", landmarkSchema, "Landmarks"); // specify collection name
18+
module.exports = model("Landmark", landmarkSchema, "Landmarks"); // specify collection name

models/location.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mongoose from "mongoose";
1+
const mongoose = require("mongoose");
22

33
const { Schema } = mongoose;
44

@@ -12,4 +12,4 @@ const locationSchema = new Schema(
1212
}
1313
);
1414

15-
export default locationSchema;
15+
module.exports = locationSchema;

models/user.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import mongoose from "mongoose";
1+
const mongoose = require("mongoose");
22

3-
import LocationSchema from "./location.js";
3+
const LocationSchema = require("./location.js");
44

55
const { Schema, model } = mongoose;
66

7-
export const UserSchema = new Schema(
7+
const UserSchema = new Schema(
88
{
99
name: String,
1010
email: String,
@@ -18,4 +18,4 @@ export const UserSchema = new Schema(
1818
}
1919
);
2020

21-
export const User = model("User", UserSchema, "Users"); // specify collection name
21+
module.exports = { UserSchema, User: model("User", UserSchema, "Users") }; // specify collection name

0 commit comments

Comments
 (0)