Skip to content

Commit 612d5a5

Browse files
authored
feat(node): add support for native HTTPS configuration (#376)
1 parent 62e16e6 commit 612d5a5

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

packages/app-server/src/index.node.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFile } from 'node:fs/promises';
2+
import { createServer as createHttpsServer } from 'node:https';
23
import process, { env } from 'node:process';
34
import { safelySync } from '@corentinth/chisels';
45
import { serve } from '@hono/node-server';
@@ -69,10 +70,14 @@ const server = serve(
6970
{
7071
fetch: app.fetch,
7172
port: config.server.port,
73+
...(config.server.useHttps
74+
? {
75+
createServer: createHttpsServer,
76+
serverOptions: config.server.https,
77+
}
78+
: {}),
7279
},
73-
({ port }) => {
74-
logger.info({ port }, 'Server started');
75-
},
80+
({ port }) => logger.info({ port }, 'Server started'),
7681
);
7782

7883
process.on('SIGINT', async () => {

packages/app-server/src/modules/app/config/config.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,49 @@ export const configDefinition = {
3131
default: [],
3232
env: 'SERVER_CORS_ORIGINS',
3333
},
34+
useHttps: {
35+
doc: 'Whether to enable HTTPS for the server (only in node env)',
36+
schema: z
37+
.string()
38+
.trim()
39+
.toLowerCase()
40+
.transform(x => x === 'true')
41+
.pipe(z.boolean()),
42+
default: 'false',
43+
env: 'SERVER_USE_HTTPS',
44+
},
45+
https: {
46+
key: {
47+
doc: 'The key for HTTPS (only in node env)',
48+
schema: z.string().optional(),
49+
default: undefined,
50+
env: 'SERVER_HTTPS_KEY',
51+
},
52+
cert: {
53+
doc: 'The cert for HTTPS (only in node env)',
54+
schema: z.string().optional(),
55+
default: undefined,
56+
env: 'SERVER_HTTPS_CERT',
57+
},
58+
ca: {
59+
doc: 'The CA for HTTPS (only in node env)',
60+
schema: z.string().optional(),
61+
default: undefined,
62+
env: 'SERVER_HTTPS_CA',
63+
},
64+
pfx: {
65+
doc: 'The pfx for HTTPS (only in node env)',
66+
schema: z.string().optional(),
67+
default: undefined,
68+
env: 'SERVER_HTTPS_PFX',
69+
},
70+
passphrase: {
71+
doc: 'The passphrase of the PFX cert (only in node env)',
72+
schema: z.string().optional(),
73+
default: undefined,
74+
env: 'SERVER_HTTPS_PASSPHRASE',
75+
},
76+
},
3477
},
3578
notes: {
3679
maxEncryptedPayloadLength: {

packages/docs/src/self-hosting/configuration.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ Enclosed is configured primarily through environment variables. Below is a list
1212

1313
<div v-html="data" />
1414

15+
## Optional: Native HTTPS Configuration
16+
17+
If you want to use HTTPS without a reverse proxy, you can set the `SERVER_USE_HTTPS` environment variable to `true` and provide the necessary certificate and key files.
18+
19+
You can either use a single PFX file or separate key and certificate files. If you use separate files, you can provide the `SERVER_HTTPS_KEY`, `SERVER_HTTPS_CERT`, and `SERVER_HTTPS_CA` environment variables. If you use a PFX file, you can provide the `SERVER_HTTPS_PFX` and `SERVER_HTTPS_PASSPHRASE` environment variables.
20+
21+
To generate the necessary key and certificate files, you can use the following command:
22+
23+
```bash
24+
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -keyout private-key.pem -out certificate.pem
25+
```
26+
27+
And if you want to generate a PFX file, you can use the following command:
28+
29+
```bash
30+
openssl pkcs12 -certpbe AES-256-CBC -export -out test_cert.pfx -inkey private-key.pem -in certificate.pem -passout pass:sample
31+
```
32+
1533
## Applying Configuration Changes
1634

1735
To apply your configuration changes, ensure that you have exported the environment variables in your shell or included them in your environment configuration file. Then, restart your Enclosed instance to apply the changes.

0 commit comments

Comments
 (0)