Skip to content

Commit

Permalink
feat: allow multiple custom domains (#317)
Browse files Browse the repository at this point in the history
* add migration to remove unique constraint on user_id in domains table
* add migration to add uuid field to domains table
* run latest migrations on start, rather than next migration
* allow client to show multiple domains
* remove domains from redis when they are deleted
  • Loading branch information
hugomd authored Jul 14, 2020
1 parent 2161033 commit 474d09e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
5 changes: 2 additions & 3 deletions client/components/Settings/SettingsDomain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const SettingsDomain: FC = () => {
Point your domain A record to <b>192.64.116.170</b> then add the domain
via form below:
</Text>
{domains.length ? (
{domains.length > 0 && (
<Table my={3} scrollWidth="550px">
<thead>
<tr>
Expand Down Expand Up @@ -115,7 +115,7 @@ const SettingsDomain: FC = () => {
))}
</tbody>
</Table>
) : (
)}
<Col
alignItems="flex-start"
onSubmit={onSubmit}
Expand Down Expand Up @@ -164,7 +164,6 @@ const SettingsDomain: FC = () => {
{loading ? "Setting..." : "Set domain"}
</Button>
</Col>
)}
<Text color={message.color}>{message.text}</Text>
<Modal id="delete-custom-domain" show={modal} closeHandler={closeModal}>
<H2 mb={24} textAlign="center" bold>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev": "npm run migrate && nodemon server/server.ts",
"build": "next build client/ && rimraf production-server && tsc --project tsconfig.json && copyfiles -f \"server/mail/*.html\" production-server/mail",
"start": "npm run migrate && NODE_ENV=production node production-server/server.js",
"migrate": "knex migrate:up --env production",
"migrate": "knex migrate:latest --env production",
"lint": "eslint server/ --ext .js,.ts --fix",
"lint:nofix": "eslint server/ --ext .js,.ts",
"docs:build": "cd docs/api && tsc generate.ts --resolveJsonModule && node generate && cd ../.."
Expand Down
3 changes: 3 additions & 0 deletions server/handlers/domains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Handler } from "express";
import query from "../queries";
import * as redis from "../redis";
import { CustomError, sanitize } from "../utils";

export const add: Handler = async (req, res) => {
Expand All @@ -23,6 +24,8 @@ export const remove: Handler = async (req, res) => {
{ user_id: null }
);

redis.remove.domain(domain);

if (!domain) {
throw new CustomError("Could not delete the domain.", 500);
}
Expand Down
5 changes: 0 additions & 5 deletions server/handlers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ export const addDomain = [
.custom(value => urlRegex({ exact: true, strict: false }).test(value))
.custom(value => value !== env.DEFAULT_DOMAIN)
.withMessage("You can't use the default domain.")
.custom(async (value, { req }) => {
const domains = await query.domain.get({ user_id: req.user.id });
if (domains.length !== 0) return Promise.reject();
})
.withMessage("You already own a domain. Contact support if you need more.")
.custom(async value => {
const domain = await query.domain.find({ address: value });
if (domain?.user_id || domain?.banned) return Promise.reject();
Expand Down
26 changes: 26 additions & 0 deletions server/migrations/20200510140704_domains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as Knex from "knex";
import * as models from "../models";

export async function up(knex: Knex): Promise<any> {
await models.createUserTable(knex);
await models.createIPTable(knex);
await models.createDomainTable(knex);
await models.createHostTable(knex);
await models.createLinkTable(knex);
await models.createVisitTable(knex);

await Promise.all([
knex.raw(`
ALTER TABLE domains
DROP CONSTRAINT IF EXISTS domains_user_id_unique
`),
knex.raw(`
ALTER TABLE domains
ADD COLUMN IF NOT EXISTS uuid UUID DEFAULT uuid_generate_v4()
`)
]);
}

export async function down(knex: Knex): Promise<any> {
// do nothing
}
7 changes: 5 additions & 2 deletions server/models/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export async function createDomainTable(knex: Knex) {
.integer("user_id")
.references("id")
.inTable("users")
.onDelete("SET NULL")
.unique();
.onDelete("SET NULL");
table
.uuid("uuid")
.notNullable()
.defaultTo(knex.raw("uuid_generate_v4()"));
table.timestamps(false, true);
});
}
Expand Down

0 comments on commit 474d09e

Please sign in to comment.