Skip to content

Commit 24f2d1d

Browse files
committed
feat(docker): add Traefik configuration for reverse proxy and TLS support
1 parent c9f2892 commit 24f2d1d

File tree

7 files changed

+113
-3
lines changed

7 files changed

+113
-3
lines changed

docker/docker-compose.pord.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: '3'
2+
3+
services:
4+
reverse-proxy:
5+
image: traefik:v3.3
6+
command:
7+
- '--api.insecure=true'
8+
- '--providers.docker=true'
9+
- '--providers.docker.exposedbydefault=false'
10+
- '--providers.file.directory=/etc/traefik/config'
11+
- '--providers.file.watch=true'
12+
- '--entrypoints.web.address=:80'
13+
- '--entrypoints.websecure.address=:443'
14+
ports:
15+
- '80:80'
16+
- '443:443'
17+
- '9001:8080'
18+
volumes:
19+
- /var/run/docker.sock:/var/run/docker.sock
20+
- /etc/letsencrypt:/etc/letsencrypt
21+
- ./traefik-config:/etc/traefik/config
22+
networks:
23+
- traefik_network
24+
extra_hosts:
25+
- 'host.docker.internal:host-gateway'
26+
27+
networks:
28+
traefik_network:
29+
driver: bridge

docker/traefik-config/services.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
http:
2+
routers:
3+
frontend:
4+
rule: 'Host(`codefox.net`) && !PathPrefix(`/graphql`)'
5+
entrypoints:
6+
- websecure
7+
tls: {}
8+
service: frontend
9+
priority: 10
10+
11+
backend:
12+
rule: 'Host(`codefox.net`) && PathPrefix(`/graphql`)'
13+
entrypoints:
14+
- websecure
15+
tls: {}
16+
service: backend
17+
priority: 20
18+
redirect-all:
19+
rule: 'hostregexp(`{host:.+}`)'
20+
entrypoints:
21+
- web
22+
middlewares:
23+
- redirect-to-https
24+
service: noop
25+
26+
services:
27+
frontend:
28+
loadBalancer:
29+
servers:
30+
- url: 'http://host.docker.internal:3000'
31+
32+
backend:
33+
loadBalancer:
34+
servers:
35+
- url: 'http://host.docker.internal:8080'
36+
37+
noop:
38+
loadBalancer:
39+
servers:
40+
- url: 'http://localhost:9000'
41+
42+
middlewares:
43+
redirect-to-https:
44+
redirectScheme:
45+
scheme: https
46+
permanent: true
47+
48+
cors:
49+
headers:
50+
accessControlAllowMethods:
51+
- GET
52+
- POST
53+
- PUT
54+
- DELETE
55+
- OPTIONS
56+
accessControlAllowHeaders:
57+
- Content-Type
58+
- Authorization

docker/traefik-config/tls.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tls:
2+
certificates:
3+
- certFile: /etc/letsencrypt/live/codefox.net/fullchain.pem
4+
keyFile: /etc/letsencrypt/live/codefox.net/privkey.pem

frontend/src/app/api/runProject/route.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import puppetter from 'puppeteer';
77
import { useMutation } from '@apollo/client/react/hooks/useMutation';
88
import { toast } from 'sonner';
99
import { UPDATE_PROJECT_PHOTO_URL } from '@/graphql/request';
10+
import { TLS } from '@/utils/const';
1011

1112
const runningContainers = new Map<
1213
string,
@@ -147,14 +148,26 @@ async function buildAndRunDocker(
147148
console.log(`Running Docker container: ${containerName}`);
148149

149150
// 3. Run the Docker container
150-
const runCommand = `docker run -d --name ${containerName} -l "traefik.enable=true" \
151+
let runCommand;
152+
if (TLS) {
153+
runCommand = `docker run -d --name ${containerName} -l "traefik.enable=true" \
151154
-l "traefik.http.routers.${subdomain}.rule=Host(\\"${domain}\\")" \
152155
-l "traefik.http.routers.${subdomain}.entrypoints=websecure" \
153156
-l "traefik.http.routers.${subdomain}.tls=true" \
154157
-l "traefik.http.services.${subdomain}.loadbalancer.server.port=5173" \
155158
--network=codefox_traefik_network -p ${exposedPort}:5173 \
156159
-v "${directory}:/app" \
157160
${imageName}`;
161+
} else {
162+
runCommand = `docker run -d --name ${containerName} -l "traefik.enable=true" \
163+
-l "traefik.http.routers.${subdomain}.rule=Host(\\"${domain}\\")" \
164+
-l "traefik.http.routers.${subdomain}.entrypoints=web" \
165+
-l "traefik.http.services.${subdomain}.loadbalancer.server.port=5173" \
166+
--network=codefox_traefik_network -p ${exposedPort}:5173 \
167+
-v "${directory}:/app" \
168+
${imageName}`;
169+
}
170+
158171
console.log(`Executing run command: ${runCommand}`);
159172

160173
exec(runCommand, (runErr, runStdout, runStderr) => {

frontend/src/components/chat/code-engine/web-view.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ZoomOut,
1313
} from 'lucide-react';
1414
import puppeteer from 'puppeteer';
15+
import { URL_PROTOCOL_PREFIX } from '@/utils/const';
1516

1617
export default function WebPreview() {
1718
const { curProject, getWebUrl } = useContext(ProjectContext);
@@ -41,7 +42,7 @@ export default function WebPreview() {
4142
lastProjectPathRef.current = projectPath;
4243

4344
if (containerRef.current?.projectPath === projectPath) {
44-
setBaseUrl(`https://${containerRef.current.domain}`);
45+
setBaseUrl(`${URL_PROTOCOL_PREFIX}://${containerRef.current.domain}`);
4546
return;
4647
}
4748

@@ -52,7 +53,7 @@ export default function WebPreview() {
5253
domain,
5354
};
5455

55-
const baseUrl = `https://${domain}`;
56+
const baseUrl = `${URL_PROTOCOL_PREFIX}://${domain}`;
5657
console.log('baseUrl:', baseUrl);
5758
setBaseUrl(baseUrl);
5859
setDisplayPath('/');

frontend/src/utils/const.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
*/
66
export const URL_PROTOCOL_PREFIX =
77
process.env.TLS == 'false' ? 'http' : 'https';
8+
9+
/**
10+
* Validate if the current environment is using TLS
11+
*/
12+
export const TLS = process.env.TLS == 'true';

0 commit comments

Comments
 (0)