Skip to content

Commit 131e42a

Browse files
feat: configure the subdomain router (#49)
1 parent 1d60daa commit 131e42a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+310
-441
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ COPY package.json bun.lockb ./
55
RUN bun install --production
66
COPY . .
77

8-
RUN bun --bun run build
9-
CMD ["bun", "run", "index.js"]
8+
RUN bun run build
9+
CMD ["bun", "run", ".output/server/index.mjs"]

README.md

+10-85
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,35 @@ Every application or package built to support IC Hack'25 tech.
44

55
## What is in this?
66

7+
This is a gigantic single running Nuxt4 application. The 3 main frontend applications are separated under subdomain routing.
8+
79
- [Server (Perry)](./server/) - First time custom backend server built.
8-
- [Admin (Ferb)](./app) - An insider website that manages users, teams, announcements... admin stuff - tbd to be app/pages/admin or something. update before merge.
10+
- [Admin (Ferb)](./app/pages/$admin/) - An admin dashboard website that manages users, teams, announcements.
11+
- [Internal (Phineas)](./app/pages/$my/) - The Internal Website used by hackers, volunteers, etc.
12+
- [Landing Page (Isabella)](./app/pages/$www/) - A static landing page.
913

1014
## Before you start
1115

1216
1. You must read this README in full.
1317
1. Read relevant portions of the documentation.
1418

1519
> [!note]
16-
> All ichack related systems every year are **private**. Feel free to fork this repository but keep it private. You could make your repo public after IC Hack is over, but make sure there are no senstive information in the repository.
20+
> All ichack related systems every year are **private**. A public **port** (not _fork_) of the repository is allowed but all sensitive information must be deleted from all commit history.
1721
1822
## Documentation
1923

2024
This is the first time IC Hack tech systems will have extensive documentation. Open [the docs folder](./documentation/) as an [Obsidian Vault](https://obsidian.md).
2125

26+
1. Install [Obsidian](https://obsidian.md/).
27+
2. Open the path `./documentation` as an Obsidian Vault.
28+
2229
> [!important]
2330
> The documentation started authoring from 4th July 2024. The contributors will try to document this code as much as possible and encourage the future volunteers to do so as well. However, it may not be the case that everything is documented perfectly.
2431
>
2532
> As a result, some or most of the pages here will be incomplete. Over time, we will do our best that it does not happen.
2633
>
2734
> The tense used in the documentation will be confusing depending on the date it was written and the event that was spoken about.
2835
29-
## How to run?
30-
31-
### Pre-setup
32-
33-
You'll need Docker, bun, and psql installed.
34-
35-
### Environment Variables
36-
37-
Without setting environment variables, your code will not execute or behave correctly. Copy the `.env.template` file into `.env.local` and `.env.test` and fill it appriorately.
38-
39-
#### Dev setup
40-
41-
```env
42-
PGUSER=admin
43-
PGPASSWORD=rootpasswd
44-
PGDB=postgres
45-
PGHOST=0.0.0.0
46-
PGPORT=5432
47-
48-
DISCORD_CLIENT_ID=anything, you'll know if you're testing this
49-
DISCORD_SERVER_ID=see above
50-
DISCORD_CLIENT_SECRET=see above
51-
```
52-
53-
#### Test setup
54-
55-
```env
56-
PGUSER=test
57-
PGPASSWORD=test
58-
PGDB=postgres
59-
PGHOST=0.0.0.0
60-
PGPORT=5432
61-
PGCA=
62-
63-
DISCORD_CLIENT_ID=
64-
DISCORD_SERVER_ID=
65-
DISCORD_CLIENT_SECRET=
66-
```
67-
68-
### Docker Compose
69-
70-
> [!important]
71-
> If you use windows, please try working on WSL2 since our codebase works best inside a Unix/Linux environment.
72-
73-
In the root directory of the project, execute:
74-
75-
```bash
76-
bun install
77-
bun reset-db
78-
bun run dev
79-
```
80-
81-
You can visit the admin page at `localhost:3000`. This will be updated to `admin.localhost:3000` soon:tm:.
82-
83-
The api routes can be accessed from `localhost:3000/api`, as expected.
84-
85-
To stop the containers from running, simply use CTRL+C.
86-
87-
To add yourself as a god user for testing, edit and execute `bun run scripts/seed.ts`.
88-
89-
If you ran the project headless, execute (in the same root directory)
90-
91-
```bash
92-
docker compose down
93-
```
94-
95-
Refer to [Docker](./documentation/Techologies/Docker.md) to learn about how we containerised it and used compose.
96-
97-
#### Common Docker error
98-
99-
If you get the error
100-
101-
```
102-
Error response from daemon: network 91cc91888ed27849c518f6769cf18115ddb56b0ef833e745e764964a6a2586da not found
103-
```
104-
105-
Run the following command and try again.
106-
107-
```bash
108-
docker-compose -f dev.docker-compose.yaml down --remove-orphans
109-
```
110-
11136
## Development rules
11237

11338
### Working on issues
@@ -134,7 +59,7 @@ You should create a PR **as soon as** you create a branch.
13459
1. The actual commit messages within your PR **does not follow** any conventional naming pattern. However, your PR title must follow `type: description` format, similar to your branch name.
13560
1. If any of the repo admins approve your PR and you need to need to update your branch, consider rebase to avoid making another commit for the admins to review again.
13661

137-
#### PR <--> ClickUp Integration
62+
#### PR <> ClickUp Integration
13863

13964
1. When you create a comment, in the description include the following text: `Link CU-86bzr6uwe`, you will find your taskId in the ClickUp Card you are working.
14065
1. You can use special commit messages to change the status of your card. E.g. a commit message can be, `updated the readme #86bzr6uwe[in review]`.

app/.dockerignore

-5
This file was deleted.

app/.gitignore

-24
This file was deleted.

app/README.md

-62
This file was deleted.

app/router.options.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { RouterOptions } from 'nuxt/schema';
2+
3+
export default <RouterOptions>{
4+
routes(_routes) {
5+
const { ssrContext } = useNuxtApp();
6+
let subdomain = '';
7+
8+
if (import.meta.server)
9+
subdomain = ssrContext?.event.context.subdomain ?? 'www';
10+
else {
11+
const { host } = useRequestURL();
12+
const config = useRuntimeConfig();
13+
if (!config.public.mainDomain.includes(host))
14+
subdomain = host.match(/^[^.]*/g)?.[0] ?? 'www';
15+
else subdomain = 'www';
16+
}
17+
18+
const subdomainRoutes = _routes
19+
.filter(({ path }) => path.startsWith(`/$${subdomain}`))
20+
.map(({ path, ...rest }) => ({
21+
...rest,
22+
path: path.replace(`/$${subdomain}`, '')
23+
}));
24+
return subdomainRoutes;
25+
}
26+
};

docker-compose.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ services:
1818
- '3000:3000'
1919
depends_on:
2020
- postgres
21-
env_file:
22-
- .env.local
21+
environment:
22+
- PGUSER=admin
23+
- PGPASSWORD=rootpasswd
24+
- PGDB=postgres
25+
- PGHOST=posgres
26+
- PGPORT=5432
2327

2428
volumes:
2529
postgres_data:
+30-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
[
2-
"file-explorer",
3-
"global-search",
4-
"switcher",
5-
"graph",
6-
"backlink",
7-
"outgoing-link",
8-
"tag-pane",
9-
"properties",
10-
"page-preview",
11-
"note-composer",
12-
"command-palette",
13-
"editor-status",
14-
"bookmarks",
15-
"outline",
16-
"word-count",
17-
"file-recovery"
18-
]
1+
{
2+
"file-explorer": true,
3+
"global-search": true,
4+
"switcher": true,
5+
"graph": true,
6+
"backlink": true,
7+
"canvas": false,
8+
"outgoing-link": true,
9+
"tag-pane": true,
10+
"properties": true,
11+
"page-preview": true,
12+
"daily-notes": false,
13+
"templates": false,
14+
"note-composer": true,
15+
"command-palette": true,
16+
"slash-command": false,
17+
"editor-status": true,
18+
"bookmarks": true,
19+
"markdown-importer": false,
20+
"zk-prefixer": false,
21+
"random-note": false,
22+
"outline": true,
23+
"word-count": true,
24+
"slides": false,
25+
"audio-recorder": false,
26+
"workspaces": false,
27+
"file-recovery": true,
28+
"publish": false,
29+
"sync": false
30+
}

documentation/Directory Structure.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ created: 2024-07-04
33
authors:
44
- Nishant
55
---
6+
## Version 1 (Deprecated)
67

78
The monorepo is a big TypeScript project with multiple projects included here.
89
- `apps/*` directory contains all the projects for the front-end websites.
910
- `packages/*` directory contains [[Nuxt]] layers and packages that make the building block of the applications.
1011
- `server` directory is a [[Hono]] application that deals with... the server.
11-
- `nginx` directory contains the configuration file for [[Nginx]] reverse proxy.
12+
- `nginx` directory contains the configuration file for [[Nginx]] reverse proxy.
13+
14+
> [!note]
15+
> This directory structure has been resigned. Check out [[The Fresh Architecture]]
16+
17+
## Version 2
18+
The monorepo is a big TypeScript project with multiple projects included here.
19+
- `app` directory contains all the projects for the front-end websites. This is achieved via [[Subdomain Routing]].
20+
- `layers/*` directory contains [[Nuxt]] layers that make the building block of the applications.
21+
- `server` directory is a [[Hono]] application that deals with the server.

documentation/Directory Structure/apps/admin/What it is.md renamed to documentation/Directory Structure/app/Admin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
created: 2024-07-08
2+
created: 2024-11-30
33
authors:
44
- Nishant
55
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
created: 2024-11-30
3+
authors:
4+
- Nishant
5+
---
6+
The application runs in the Server-side rendering (SSR) mode. This means the HTML document is rendered on the server, and sent to the browser. The JavaScript files soon follow along and the JavaScript code then renders it again on the client side, giving the page the interactivity.
7+
8+
In the server side, when the requrest comes to render a page, a middleware is executed, more specifically `./server/middleware/subdomain.ts`. In this middleware, we analyse the hostname and extract the subdomain from it. The subdomain is then attached to the `ssrContext`, which is sent to the browser as a payload. Now that the middleware is executed and we have have the subdomain used (undefined if no subdomain was used), Nitro now understands that it isn't an API route, it is a page route. The handler is given to Nuxt to render the initial HTML render of the page.
9+
10+
When Nuxt is given the control from Nitro, it is intercepted by `app/router.options.ts` that returns a modified list of routes. This uses the subdomain to filter and map the existing list of routes. Nuxt finds the component function to render the page in the modified list of routes to render the page. In more detail, the `router.options.ts` has the payload from the server in the `ssrContext`. This also protects from any cross-domain access.

documentation/Directory Structure/layers/base-layer.md

-11
This file was deleted.

0 commit comments

Comments
 (0)