Skip to content

Commit

Permalink
feat: configure caddy to server api and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 3, 2020
1 parent b3f29e5 commit 5a3566a
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MELI_HOST=http://localhost:3001
MELI_UI_URL=http://localhost:3000
MELI_UI_HOST=http://localhost:3000
MELI_PORT=3001
MELI_SITES_DOMAIN=loopback.sh
MELI_CADDY_MELI_SERVER_HOST=host.docker.internal:3001
Expand Down
3 changes: 2 additions & 1 deletion docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ services:

caddy:
image: caddy
command: ['caddy', 'run', '--config', '/etc/caddy/config.json']
command: [ 'caddy', 'run', '--config', '/etc/caddy/config.json' ]
ports:
- 127.0.0.1:80:80
- 127.0.0.1:8080:8080
- 127.0.0.1:2019:2019
volumes:
- ./data/caddy:/sites
Expand Down
2 changes: 1 addition & 1 deletion setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ process.env['FORCE_COLOR'] = '1';

// env
process.env.MELI_HOST = 'http://localhost:3001';
process.env.MELI_UI_URL = 'http://localhost:3000';
process.env.MELI_UI_HOST = 'http://localhost:3000';
process.env.MELI_MAIL_HOST = 'localhost';
process.env.MELI_MAIL_PORT = '1025';
process.env.MELI_BILLING_PRICE_ID = 'price_123';
Expand Down
4 changes: 2 additions & 2 deletions src/auth/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ async function handler(req: Request, res: Response, next: NextFunction) {
if (err) {
return next(err);
}
logger.debug(`Redirecting to ${env.MELI_UI_URL} with cookie ${authCookieName} ${JSON.stringify(cookieOptions(), null, 2)}`);
logger.debug(`Redirecting to ${env.MELI_UI_HOST.host} with cookie ${authCookieName} ${JSON.stringify(cookieOptions(), null, 2)}`);
res
.cookie(authCookieName, token, cookieOptions())
.redirect(env.MELI_UI_URL);
.redirect(env.MELI_UI_HOST.host);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/auth/passport/gitea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (
&& env.MELI_GITEA_CLIENT_ID
&& env.MELI_GITEA_CLIENT_SECRET
) {
const oauthCallbackUrl = `${env.MELI_HOST}${gitea_callback}`;
const oauthCallbackUrl = `${env.MELI_HOST.host}${gitea_callback}`;
logger.debug('Enabling gitea auth', oauthCallbackUrl);

passport.use('gitea', new OAuth2Strategy(
Expand Down
2 changes: 1 addition & 1 deletion src/auth/passport/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (
&& env.MELI_GITHUB_CLIENT_ID
&& env.MELI_GITHUB_CLIENT_SECRET
) {
const oauthCallbackUrl = `${env.MELI_HOST}${github_callback}`;
const oauthCallbackUrl = `${env.MELI_HOST.host}${github_callback}`;
logger.debug('Enabling github auth', oauthCallbackUrl);

passport.use('github', new OAuth2Strategy(
Expand Down
2 changes: 1 addition & 1 deletion src/auth/passport/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (
&& env.MELI_GITLAB_CLIENT_ID
&& env.MELI_GITLAB_CLIENT_SECRET
) {
const oauthCallbackUrl = `${env.MELI_HOST}${gitlab_callback}`;
const oauthCallbackUrl = `${env.MELI_HOST.host}${gitlab_callback}`;
logger.debug('Enabling gitlab auth', oauthCallbackUrl);

passport.use('gitlab', new OAuth2Strategy(
Expand Down
2 changes: 1 addition & 1 deletion src/auth/passport/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (
env.MELI_GOOGLE_CLIENT_ID
&& env.MELI_GOOGLE_CLIENT_SECRET
) {
const oauthCallbackUrl = `${env.MELI_HOST}${google_callback}`;
const oauthCallbackUrl = `${env.MELI_HOST.host}${google_callback}`;
logger.debug('Enabling google auth', oauthCallbackUrl);

// TODO could we use the OAuth2 strategy ?
Expand Down
42 changes: 42 additions & 0 deletions src/caddy/config/app-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { env } from '../../env';

export const appServer = {
listen: [':8080'],
routes: [
{
group: 'api',
match: [{
path: [
'/api/*',
'/auth/*',
],
}],
handle: [
// https://caddyserver.com/docs/json/apps/http/servers/routes/handle/reverse_proxy/
{
handler: 'reverse_proxy',
upstreams: [{
dial: env.MELI_CADDY_API_HOST?.host || env.MELI_HOST.host,
}],
},
],
terminal: true,
},
{
group: 'ui',
match: [{
path: ['/*'],
}],
handle: [
// https://caddyserver.com/docs/json/apps/http/servers/routes/handle/reverse_proxy/
{
handler: 'reverse_proxy',
upstreams: [{
dial: env.MELI_CADDY_UI_HOST?.host || env.MELI_UI_HOST.host,
}],
},
],
terminal: true,
},
],
};
2 changes: 2 additions & 0 deletions src/caddy/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getBranchFilesDir, getBranchStaticDir } from '../entities/sites/get-sit
import { RedirectType, ReverseProxyRedirectConfig } from '../entities/sites/redirect';
import { getReverseProxyDial } from './utils/get-reverse-proxy-dial';
import { URL } from 'url';
import { appServer } from './config/app-server';

const logger = new Logger('meli.server:caddy');

Expand Down Expand Up @@ -66,6 +67,7 @@ async function generateConfig(): Promise<any> {
apps: {
http: {
servers: {
app: appServer,
sites: {
listen: [':80'],
routes: [
Expand Down
5 changes: 5 additions & 0 deletions src/commons/validators/to-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { URL } from 'url';

export function toUrl(str: string): URL {
return str ? new URL(str) : undefined;
}
2 changes: 1 addition & 1 deletion src/entities/orgs/handlers/invites/add-invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function handler(req: Request, res: Response): Promise<void> {

await sendInvite(email, {
org: org.name,
url: `${env.MELI_UI_URL}/invite?token=${invite.token}`,
url: `${env.MELI_UI_HOST.host}/invite?token=${invite.token}`,
});

emitEvent(EventType.org_invite_added, {
Expand Down
3 changes: 1 addition & 2 deletions src/entities/sites/serialize-redirect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Branch } from './branch';
import { env } from '../../env';
import { Site } from './site';
import { getSiteUrl } from './get-site-url';
import { getBranchUrl } from './get-branch-url';
Expand All @@ -13,6 +12,6 @@ export function serializeRedirect(site: Site, branch: Branch, redirect: Redirect
type: redirect.type,
path: redirect.path,
config: redirect.config,
url: `${url}${env.MELI_CADDY_REDIRECT_PREFIX}${formatRedirectPath(redirect.path)}`,
url: `${url}${formatRedirectPath(redirect.path)}`,
};
}
24 changes: 14 additions & 10 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import {
} from './commons/env/transformers';
import { AppError } from './commons/errors/app-error';
import { URL } from 'url';
import { toUrl } from './commons/validators/to-url';

export interface Env {
DEBUG: string;
MELI_PORT: number;
MELI_HOST: string;
MELI_HOST: URL;
MELI_SITES_DOMAIN: URL;
MELI_PUBLIC_HOST: string;
MELI_JWT_SECRET: string;
MELI_JWT_TOKEN_EXPIRATION: number;
MELI_UI_URL: string;
MELI_UI_HOST: URL;
MELI_MONGO_URI: string;
MELI_GITLAB_URL: string;
MELI_GITLAB_CLIENT_ID: string;
Expand Down Expand Up @@ -68,7 +69,8 @@ export interface Env {
MELI_STATIC_DIR: string;
MELI_CADDY_MELI_SERVER_HOST: string;
MELI_BCRYPT_SALTROUNDS: number;
MELI_CADDY_REDIRECT_PREFIX: string;
MELI_CADDY_UI_HOST: URL;
MELI_CADDY_API_HOST: URL;
}

const envSpec: EnvSpec<Env> = {
Expand All @@ -80,10 +82,10 @@ const envSpec: EnvSpec<Env> = {
schema: number().default(3000),
},
MELI_HOST: {
schema: string().required(),
schema: string().required().custom(toUrl),
},
MELI_SITES_DOMAIN: {
schema: string().required().custom(url => new URL(url)),
schema: string().required().custom(toUrl),
},
MELI_PUBLIC_HOST: {
schema: string(),
Expand All @@ -95,8 +97,8 @@ const envSpec: EnvSpec<Env> = {
transform: stringToInt(),
schema: number().min(3600).default(86400 * 30),
},
MELI_UI_URL: {
schema: string().required(),
MELI_UI_HOST: {
schema: string().required().custom(toUrl),
},
MELI_GITLAB_URL: {
schema: string().default('https://gitlab.com'),
Expand Down Expand Up @@ -286,9 +288,11 @@ const envSpec: EnvSpec<Env> = {
transform: stringToInt(),
schema: number().optional().default(10),
},
MELI_CADDY_REDIRECT_PREFIX: {
// TODO included slash might be confusing
schema: string().optional().default('/env/').pattern(/^\/[\w_-]+$/),
MELI_CADDY_UI_HOST: {
schema: string().optional().custom(toUrl),
},
MELI_CADDY_API_HOST: {
schema: string().optional().custom(toUrl),
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function main(): Promise<any> {
app.use(compression());
app.use(helmet());
app.use(cors({
origin: env.MELI_UI_URL,
origin: env.MELI_UI_HOST.host,
credentials: true,
}));

Expand Down

0 comments on commit 5a3566a

Please sign in to comment.