Skip to content

Commit 474d09e

Browse files
authored
feat: allow multiple custom domains (thedevs-network#317)
* 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
1 parent 2161033 commit 474d09e

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

client/components/Settings/SettingsDomain.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const SettingsDomain: FC = () => {
7878
Point your domain A record to <b>192.64.116.170</b> then add the domain
7979
via form below:
8080
</Text>
81-
{domains.length ? (
81+
{domains.length > 0 && (
8282
<Table my={3} scrollWidth="550px">
8383
<thead>
8484
<tr>
@@ -115,7 +115,7 @@ const SettingsDomain: FC = () => {
115115
))}
116116
</tbody>
117117
</Table>
118-
) : (
118+
)}
119119
<Col
120120
alignItems="flex-start"
121121
onSubmit={onSubmit}
@@ -164,7 +164,6 @@ const SettingsDomain: FC = () => {
164164
{loading ? "Setting..." : "Set domain"}
165165
</Button>
166166
</Col>
167-
)}
168167
<Text color={message.color}>{message.text}</Text>
169168
<Modal id="delete-custom-domain" show={modal} closeHandler={closeModal}>
170169
<H2 mb={24} textAlign="center" bold>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dev": "npm run migrate && nodemon server/server.ts",
1111
"build": "next build client/ && rimraf production-server && tsc --project tsconfig.json && copyfiles -f \"server/mail/*.html\" production-server/mail",
1212
"start": "npm run migrate && NODE_ENV=production node production-server/server.js",
13-
"migrate": "knex migrate:up --env production",
13+
"migrate": "knex migrate:latest --env production",
1414
"lint": "eslint server/ --ext .js,.ts --fix",
1515
"lint:nofix": "eslint server/ --ext .js,.ts",
1616
"docs:build": "cd docs/api && tsc generate.ts --resolveJsonModule && node generate && cd ../.."

server/handlers/domains.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Handler } from "express";
22
import query from "../queries";
3+
import * as redis from "../redis";
34
import { CustomError, sanitize } from "../utils";
45

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

27+
redis.remove.domain(domain);
28+
2629
if (!domain) {
2730
throw new CustomError("Could not delete the domain.", 500);
2831
}

server/handlers/validators.ts

-5
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ export const addDomain = [
161161
.custom(value => urlRegex({ exact: true, strict: false }).test(value))
162162
.custom(value => value !== env.DEFAULT_DOMAIN)
163163
.withMessage("You can't use the default domain.")
164-
.custom(async (value, { req }) => {
165-
const domains = await query.domain.get({ user_id: req.user.id });
166-
if (domains.length !== 0) return Promise.reject();
167-
})
168-
.withMessage("You already own a domain. Contact support if you need more.")
169164
.custom(async value => {
170165
const domain = await query.domain.find({ address: value });
171166
if (domain?.user_id || domain?.banned) return Promise.reject();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as Knex from "knex";
2+
import * as models from "../models";
3+
4+
export async function up(knex: Knex): Promise<any> {
5+
await models.createUserTable(knex);
6+
await models.createIPTable(knex);
7+
await models.createDomainTable(knex);
8+
await models.createHostTable(knex);
9+
await models.createLinkTable(knex);
10+
await models.createVisitTable(knex);
11+
12+
await Promise.all([
13+
knex.raw(`
14+
ALTER TABLE domains
15+
DROP CONSTRAINT IF EXISTS domains_user_id_unique
16+
`),
17+
knex.raw(`
18+
ALTER TABLE domains
19+
ADD COLUMN IF NOT EXISTS uuid UUID DEFAULT uuid_generate_v4()
20+
`)
21+
]);
22+
}
23+
24+
export async function down(knex: Knex): Promise<any> {
25+
// do nothing
26+
}

server/models/domain.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ export async function createDomainTable(knex: Knex) {
2323
.integer("user_id")
2424
.references("id")
2525
.inTable("users")
26-
.onDelete("SET NULL")
27-
.unique();
26+
.onDelete("SET NULL");
27+
table
28+
.uuid("uuid")
29+
.notNullable()
30+
.defaultTo(knex.raw("uuid_generate_v4()"));
2831
table.timestamps(false, true);
2932
});
3033
}

0 commit comments

Comments
 (0)