diff options
Diffstat (limited to 'src/internal-server/start.ts')
-rw-r--r-- | src/internal-server/start.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/internal-server/start.ts b/src/internal-server/start.ts new file mode 100644 index 000000000..392f7cf41 --- /dev/null +++ b/src/internal-server/start.ts | |||
@@ -0,0 +1,51 @@ | |||
1 | /* | ||
2 | |-------------------------------------------------------------------------- | ||
3 | | Http server | ||
4 | |-------------------------------------------------------------------------- | ||
5 | | | ||
6 | | This file bootstraps Adonisjs to start the HTTP server. You are free to | ||
7 | | customize the process of booting the http server. | ||
8 | | | ||
9 | | """ Loading ace commands """ | ||
10 | | At times you may want to load ace commands when starting the HTTP server. | ||
11 | | Same can be done by chaining `loadCommands()` method after | ||
12 | | | ||
13 | | """ Preloading files """ | ||
14 | | Also you can preload files by calling `preLoad('path/to/file')` method. | ||
15 | | Make sure to pass a relative path from the project root. | ||
16 | */ | ||
17 | |||
18 | import fold from '@adonisjs/fold'; | ||
19 | import { Ignitor } from '@adonisjs/ignitor'; | ||
20 | import fs from 'fs-extra'; | ||
21 | import path from 'path'; | ||
22 | import { LOCAL_HOSTNAME } from '../config'; | ||
23 | import { isWindows } from '../environment'; | ||
24 | |||
25 | process.env.ENV_PATH = path.join(__dirname, 'env.ini'); | ||
26 | |||
27 | export const server = async (userPath: string, port: number) => { | ||
28 | const dbPath = path.join(userPath, 'server.sqlite'); | ||
29 | const dbTemplatePath = path.join(__dirname, 'database', 'template.sqlite'); | ||
30 | |||
31 | if (!fs.existsSync(dbPath)) { | ||
32 | // Manually copy file | ||
33 | // We can't use copyFile here as it will cause the file to be readonly on Windows | ||
34 | const dbTemplate = await fs.readFile(dbTemplatePath); | ||
35 | await fs.writeFile(dbPath, dbTemplate); | ||
36 | |||
37 | // Change permissions to ensure to file is not read-only | ||
38 | if (isWindows) { | ||
39 | // eslint-disable-next-line no-bitwise | ||
40 | fs.chmodSync(dbPath, fs.statSync(dbPath).mode | 146); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | // Note: These env vars are used by adonis as env vars | ||
45 | process.env.DB_PATH = dbPath; | ||
46 | process.env.USER_PATH = userPath; | ||
47 | process.env.HOST = LOCAL_HOSTNAME; | ||
48 | process.env.PORT = port.toString(); | ||
49 | |||
50 | new Ignitor(fold).appRoot(__dirname).fireHttpServer().catch(console.error); | ||
51 | }; | ||