-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
142 lines (121 loc) · 4.08 KB
/
dev-server.js
File metadata and controls
142 lines (121 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { exec } = require('child_process');
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const next = require('next');
const { parse } = require('url');
const dev = process.env.NODE_ENV !== 'production';
const requestedProtocol = process.env.LOCAL_ALIAS_PROTOCOL || 'https';
const localAliasHost =
process.env.LOCAL_ALIAS_HOST || 'local.dev.1day1streak.com';
const bindHost = process.env.LOCAL_BIND_HOST;
const isMac = process.platform === 'darwin';
const isWindows = process.platform === 'win32';
const strictHttps = process.env.LOCAL_ALIAS_HTTPS_STRICT === 'true';
const allowInsecureTls = process.env.LOCAL_ALLOW_INSECURE_TLS === 'true';
const keyPath =
process.env.LOCAL_SSL_KEY_PATH || '_wildcard.dev.1day1streak.com-key.pem';
const certPath =
process.env.LOCAL_SSL_CERT_PATH || '_wildcard.dev.1day1streak.com.pem';
const resolvedKeyPath = path.resolve(process.cwd(), keyPath);
const resolvedCertPath = path.resolve(process.cwd(), certPath);
function hasHttpsCertFiles() {
return fs.existsSync(resolvedKeyPath) && fs.existsSync(resolvedCertPath);
}
const shouldUseHttps =
requestedProtocol === 'https' ? hasHttpsCertFiles() : false;
if (requestedProtocol === 'https' && !shouldUseHttps) {
const message = [
'HTTPS 인증서 파일을 찾지 못했습니다.',
`LOCAL_SSL_KEY_PATH=${resolvedKeyPath}`,
`LOCAL_SSL_CERT_PATH=${resolvedCertPath}`,
].join('\n');
if (strictHttps) {
throw new Error(message);
}
console.warn(message);
console.warn(
'LOCAL_ALIAS_PROTOCOL=http 으로 fallback 합니다. ' +
'HTTPS가 필요하면 인증서를 추가하거나 경로를 지정해주세요.'
);
}
const protocol = shouldUseHttps ? 'https' : 'http';
const defaultPort = shouldUseHttps ? 443 : 3000;
const port = Number(
process.env.LOCAL_ALIAS_PORT || process.env.PORT || defaultPort
);
const localUrl =
port === (shouldUseHttps ? 443 : 80)
? `${protocol}://${localAliasHost}`
: `${protocol}://${localAliasHost}:${port}`;
if (dev) {
// NOTE: 자체 인증서 백엔드 호출이 필요할 때만 수동으로 허용
if (allowInsecureTls) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
// Turbopack 비활성화 (로컬 alias 환경 안정화 목적)
process.env.TURBOPACK = '0';
}
const app = next({
dev,
hostname: localAliasHost,
port,
turbopack: false,
});
const handle = app.getRequestHandler();
function getListenArgs() {
const defaultHost = isMac ? undefined : isWindows ? undefined : '127.0.0.1';
if (bindHost === 'none') {
return [port];
}
const host = bindHost || defaultHost;
return host ? [port, host] : [port];
}
function getHttpsOptions() {
return {
key: fs.readFileSync(resolvedKeyPath),
cert: fs.readFileSync(resolvedCertPath),
};
}
function openBrowser(url) {
if (process.env.LOCAL_ALIAS_OPEN_BROWSER === 'false') {
return;
}
const openCommand = isMac ? 'open' : isWindows ? 'start' : 'xdg-open';
exec(`${openCommand} "${url}"`);
}
app
.prepare()
.then(() => {
const requestHandler = async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
await handle(req, res, parsedUrl);
} catch (error) {
console.error('Error occurred handling', req.url, error);
res.statusCode = 500;
res.end('internal server error');
}
};
const server = shouldUseHttps
? https.createServer(getHttpsOptions(), requestHandler)
: http.createServer(requestHandler);
server.listen(...getListenArgs(), (error) => {
if (error) {
throw error;
}
console.log('============================================');
console.log('Local alias server is ready.');
console.log(`URL: ${localUrl}`);
console.log(`Protocol: ${protocol.toUpperCase()}`);
console.log(`Host: ${localAliasHost}`);
console.log(`Port: ${port}`);
console.log('============================================');
openBrowser(localUrl);
});
})
.catch((error) => {
console.error('Failed to start local alias server:', error);
process.exit(1);
});