feat: support development url's

This commit is contained in:
Wyatt Johnson
2018-10-29 14:31:35 -06:00
parent 6fc2eabc88
commit 2571f048ad
8 changed files with 26 additions and 12 deletions
@@ -2,6 +2,7 @@ import { RequestHandler } from "express-jwt";
import { Redis } from "ioredis";
import { Db } from "mongodb";
import { Config } from "talk-common/config";
import TenantContext from "talk-server/graph/tenant/context";
import { TaskQueue } from "talk-server/services/queue";
import { Request } from "talk-server/types/express";
@@ -10,12 +11,14 @@ export interface TenantContextMiddlewareOptions {
mongo: Db;
redis: Redis;
queue: TaskQueue;
config: Config;
}
export const tenantContext = ({
mongo,
redis,
queue,
config,
}: TenantContextMiddlewareOptions): RequestHandler => (
req: Request,
res,
@@ -38,6 +41,7 @@ export const tenantContext = ({
req.talk.context = {
tenant: new TenantContext({
req,
config,
mongo,
redis,
tenant,
+1 -5
View File
@@ -42,11 +42,7 @@ export async function createTenantRouter(
// Any users may submit their GraphQL requests with authentication, this
// middleware will unpack their user into the request.
options.passport.authenticate("jwt", { session: false }),
tenantContext({
mongo: app.mongo,
redis: app.redis,
queue: app.queue,
}),
tenantContext(app),
await tenantGraphMiddleware({
schema: app.schemas.tenant,
config: app.config,
+6 -1
View File
@@ -1,3 +1,4 @@
import { Config } from "talk-common/config";
import { Tenant } from "talk-server/models/tenant";
import { Request } from "talk-server/types/express";
import { URL } from "url";
@@ -16,10 +17,14 @@ export function reconstructURL(req: Request, path: string = "/"): string {
* reconstructTenantURL will reconstruct a URL based off of the Tenant's domain.
*/
export function reconstructTenantURL(
config: Config,
tenant: Pick<Tenant, "domain">,
path: string = "/"
): string {
const url = new URL(path, `https://${tenant.domain}`);
let url: URL = new URL(path, `https://${tenant.domain}`);
if (config.get("env") === "development") {
url = new URL(path, `http://${tenant.domain}:${config.get("port")}`);
}
return url.href;
}
+5 -1
View File
@@ -1,17 +1,21 @@
import { Config } from "talk-common/config";
import { User } from "talk-server/models/user";
import { Request } from "talk-server/types/express";
export interface CommonContextOptions {
user?: User;
req?: Request;
config: Config;
}
export default class CommonContext {
public user?: User;
public req?: Request;
public config: Config;
constructor({ user, req }: CommonContextOptions) {
constructor({ user, req, config }: CommonContextOptions) {
this.user = user;
this.req = req;
this.config = config;
}
}
+4 -2
View File
@@ -1,18 +1,20 @@
import { Db } from "mongodb";
import { Config } from "talk-common/config";
import CommonContext from "talk-server/graph/common/context";
import { Request } from "talk-server/types/express";
export interface ManagementContextOptions {
mongo: Db;
config: Config;
req?: Request;
}
export default class ManagementContext extends CommonContext {
public mongo: Db;
constructor({ req, mongo }: ManagementContextOptions) {
super({ req });
constructor({ req, mongo, config }: ManagementContextOptions) {
super({ req, config });
this.mongo = mongo;
}
@@ -10,5 +10,5 @@ import ManagementContext from "./context";
export default (schema: GraphQLSchema, config: Config, mongo: Db) =>
graphqlMiddleware(config, async (req: Request) => ({
schema,
context: new ManagementContext({ req, mongo }),
context: new ManagementContext({ req, mongo, config }),
}));
+4 -1
View File
@@ -8,6 +8,7 @@ import { TaskQueue } from "talk-server/services/queue";
import TenantCache from "talk-server/services/tenant/cache";
import { Request } from "talk-server/types/express";
import { Config } from "talk-common/config";
import loaders from "./loaders";
import mutators from "./mutators";
@@ -17,6 +18,7 @@ export interface TenantContextOptions {
tenant: Tenant;
tenantCache: TenantCache;
queue: TaskQueue;
config: Config;
req?: Request;
user?: User;
}
@@ -37,10 +39,11 @@ export default class TenantContext extends CommonContext {
tenant,
mongo,
redis,
config,
tenantCache,
queue,
}: TenantContextOptions) {
super({ user, req });
super({ user, req, config });
this.tenant = tenant;
this.tenantCache = tenantCache;
@@ -19,7 +19,7 @@ const OIDCAuthIntegration: GQLOIDCAuthIntegrationTypeResolver<
// Note that when constructing the callback url with the tenant, the port
// information is lost.
return reconstructTenantURL(ctx.tenant, path);
return reconstructTenantURL(ctx.config, ctx.tenant, path);
},
};