2024-01-16 08:10:14 +00:00
|
|
|
import debug from 'debug';
|
2024-01-16 09:36:35 +00:00
|
|
|
import express from 'express';
|
2024-02-22 11:56:26 +00:00
|
|
|
import cors from 'cors';
|
2024-01-16 09:36:35 +00:00
|
|
|
import { ApolloServer } from 'apollo-server-express';
|
|
|
|
import { createServer } from 'http';
|
|
|
|
import {
|
|
|
|
ApolloServerPluginDrainHttpServer,
|
2024-02-22 11:56:26 +00:00
|
|
|
ApolloServerPluginLandingPageLocalDefault,
|
|
|
|
AuthenticationError
|
2024-01-16 09:36:35 +00:00
|
|
|
} from 'apollo-server-core';
|
2024-02-22 11:56:26 +00:00
|
|
|
import session from 'express-session';
|
2024-01-16 08:10:14 +00:00
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
import { TypeSource } from '@graphql-tools/utils';
|
|
|
|
import { makeExecutableSchema } from '@graphql-tools/schema';
|
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
import { ServerConfig } from './config';
|
2024-02-22 11:56:26 +00:00
|
|
|
import { DEFAULT_GQL_PATH } from './constants';
|
2024-02-15 11:54:57 +00:00
|
|
|
import githubRouter from './routes/github';
|
2024-02-22 11:56:26 +00:00
|
|
|
import authRouter from './routes/auth';
|
2024-02-15 11:54:57 +00:00
|
|
|
import { Service } from './service';
|
2024-01-16 08:10:14 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:server');
|
|
|
|
|
2024-02-22 11:56:26 +00:00
|
|
|
declare module 'express-session' {
|
|
|
|
interface SessionData {
|
|
|
|
address: string;
|
|
|
|
chainId: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
export const createAndStartServer = async (
|
2024-02-15 11:54:57 +00:00
|
|
|
serverConfig: ServerConfig,
|
2024-01-16 09:36:35 +00:00
|
|
|
typeDefs: TypeSource,
|
|
|
|
resolvers: any,
|
2024-02-15 11:54:57 +00:00
|
|
|
service: Service
|
2024-01-16 09:36:35 +00:00
|
|
|
): Promise<ApolloServer> => {
|
|
|
|
const { host, port, gqlPath = DEFAULT_GQL_PATH } = serverConfig;
|
2024-02-22 11:56:26 +00:00
|
|
|
const { appOriginUrl, secret, domain, trustProxy } = serverConfig.session;
|
2024-01-16 08:10:14 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
// Create HTTP server
|
|
|
|
const httpServer = createServer(app);
|
|
|
|
|
|
|
|
// Create the schema
|
|
|
|
const schema = makeExecutableSchema({
|
|
|
|
typeDefs,
|
2024-01-17 05:23:01 +00:00
|
|
|
resolvers
|
2024-01-16 08:10:14 +00:00
|
|
|
});
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
const server = new ApolloServer({
|
|
|
|
schema,
|
|
|
|
csrfPrevention: true,
|
2024-02-22 11:56:26 +00:00
|
|
|
context: async ({ req }) => {
|
|
|
|
// https://www.apollographql.com/docs/apollo-server/v3/security/authentication#api-wide-authorization
|
|
|
|
|
|
|
|
const { address } = req.session;
|
|
|
|
|
|
|
|
if (!address) {
|
|
|
|
throw new AuthenticationError('Unauthorized: No active session');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find/create user from ETH address in request session
|
|
|
|
const user = await service.loadOrCreateUser(address);
|
|
|
|
|
|
|
|
return { user };
|
2024-01-17 05:23:01 +00:00
|
|
|
},
|
2024-01-16 09:36:35 +00:00
|
|
|
plugins: [
|
|
|
|
// Proper shutdown for the HTTP server
|
|
|
|
ApolloServerPluginDrainHttpServer({ httpServer }),
|
|
|
|
ApolloServerPluginLandingPageLocalDefault({ embed: true })
|
|
|
|
]
|
2024-01-16 08:10:14 +00:00
|
|
|
});
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
await server.start();
|
|
|
|
|
2024-02-22 11:56:26 +00:00
|
|
|
app.use(cors({
|
|
|
|
origin: appOriginUrl,
|
|
|
|
credentials: true
|
|
|
|
}));
|
|
|
|
|
|
|
|
const sessionOptions: session.SessionOptions = {
|
|
|
|
secret: secret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
|
|
|
cookie: {
|
|
|
|
secure: new URL(appOriginUrl).protocol === 'https:',
|
|
|
|
// TODO: Set cookie maxAge and handle cookie expiry in frontend
|
|
|
|
// maxAge: SESSION_COOKIE_MAX_AGE,
|
|
|
|
sameSite: new URL(appOriginUrl).protocol === 'https:' ? 'none' : 'lax'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (domain) {
|
|
|
|
sessionOptions.cookie!.domain = domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trustProxy) {
|
|
|
|
// trust first proxy
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
session(sessionOptions)
|
|
|
|
);
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
server.applyMiddleware({
|
|
|
|
app,
|
2024-02-22 11:56:26 +00:00
|
|
|
path: gqlPath,
|
|
|
|
cors: {
|
|
|
|
origin: [appOriginUrl],
|
|
|
|
credentials: true
|
|
|
|
}
|
2024-01-16 08:10:14 +00:00
|
|
|
});
|
2024-01-16 09:36:35 +00:00
|
|
|
|
2024-02-15 11:54:57 +00:00
|
|
|
app.use(express.json());
|
2024-02-22 11:56:26 +00:00
|
|
|
|
|
|
|
app.set('service', service);
|
|
|
|
app.use('/auth', authRouter);
|
2024-02-15 11:54:57 +00:00
|
|
|
app.use('/api/github', githubRouter);
|
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
httpServer.listen(port, host, () => {
|
|
|
|
log(`Server is listening on ${host}:${port}${server.graphqlPath}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
return server;
|
|
|
|
};
|