@@ -5,33 +5,62 @@ const express = require("express");
5
5
const path = require ( "path" ) ;
6
6
const fs = require ( "fs" ) ;
7
7
const dotenv = require ( "dotenv" ) . config ( ) ;
8
- const {
8
+ const { updateDbFile } = require ( "./API/settingsApi" ) ;
9
+ const app = globalAPI ;
10
+ const log = console . log ;
11
+ var envConfigFilename = "env_config.json" ;
12
+ var envConfigFilePath = path . join ( __dirname , envConfigFilename ) ;
13
+
14
+ app . use ( express . static ( path . join ( __dirname , "build" ) ) ) ;
15
+
16
+ log ( "INFO: Checking for config file" ) ;
17
+
18
+ let configStatus = "" ;
19
+ try {
20
+ configStatus = fs . accessSync ( envConfigFilePath ) ;
21
+ } catch ( e ) {
22
+ log ( "ERROR: No config file found. Falling back to config creation module" ) ;
23
+ const configData = [
24
+ {
25
+ databaseFile : path . join ( __dirname , "database/repo-datastore.json" ) ,
26
+ port : 9001 ,
27
+ } ,
28
+ ] ;
29
+ log (
30
+ "INFO: Creating config file with default config -> " +
31
+ JSON . stringify ( configData )
32
+ ) ;
33
+ fs . writeFileSync ( envConfigFilePath , JSON . stringify ( configData ) , {
34
+ flag : "w" ,
35
+ } ) ;
36
+ }
37
+
38
+ let {
9
39
DATABASE_FILE ,
10
40
GITCONVEX_PORT ,
11
41
} = require ( "./global/envConfigReader" ) . getEnvData ( ) ;
12
-
13
- const app = globalAPI ;
14
42
const port = GITCONVEX_PORT ;
15
43
16
- app . use ( express . static ( path . join ( __dirname , "build" ) ) ) ;
44
+ log ( "INFO: Config file is present" ) ;
45
+ log ( "INFO: Reading from config file " + envConfigFilePath ) ;
17
46
18
47
app . get ( "/*" , ( req , res ) => {
19
48
res . sendFile ( path . join ( __dirname , "build" , "index.html" ) ) ;
20
49
} ) ;
21
50
22
51
globalAPI . listen ( port || 9001 , async ( err ) => {
23
52
if ( err ) {
24
- console . log ( err ) ;
53
+ log ( err ) ;
25
54
}
26
55
27
- console . log ( "GitConvex API connected!" ) ;
56
+ log ( "GitConvex API connected!" ) ;
28
57
29
- console . log ( "\n#Checking data file availability..." ) ;
58
+ log ( "\n#Checking data file availability..." ) ;
30
59
31
60
await fs . promises
32
61
. access ( DATABASE_FILE )
33
62
. then ( ( ) => {
34
- console . log (
63
+ log (
35
64
`INFO: Data file ${ DATABASE_FILE } is present and it will be used as the active data file!\n\n## You can change this under the settings menu
36
65
`
37
66
) ;
@@ -41,41 +70,40 @@ globalAPI.listen(port || 9001, async (err) => {
41
70
return await fs . promises
42
71
. writeFile ( DATABASE_FILE , "[]" )
43
72
. then ( ( res ) => {
44
- console . log (
73
+ log (
45
74
"\nINFO: New data file created and it will be used as the active file\n\n## You can change this under the settings menu"
46
75
) ;
47
76
} )
48
77
. catch ( ( err ) => {
49
- console . log (
78
+ log (
50
79
"INFO: New data file creation failed!\nINFO: Falling back to directory creation module"
51
80
) ;
52
81
} ) ;
53
82
} ;
54
83
55
- console . log (
84
+ log (
56
85
`INFO: Data file is missing\nCreating new file under ${ DATABASE_FILE } `
57
86
) ;
58
87
59
88
await dataFileCreator ( ) ;
60
89
61
- if ( fs . existsSync ( ) ) {
62
- } else {
63
- console . log ( "INFO: Database directory is missing" ) ;
90
+ if ( ! fs . existsSync ( DATABASE_FILE ) ) {
91
+ log ( "INFO: Database directory is missing" ) ;
64
92
await fs . promises
65
- . mkdir ( ". /database")
93
+ . mkdir ( path . join ( __dirname , "." , " /database") )
66
94
. then ( async ( ) => {
67
- console . log (
95
+ log (
68
96
"INFO: Created database directory\nINFO: Setting up new data file in database directory"
69
97
) ;
70
98
await dataFileCreator ( ) ;
71
99
} )
72
100
. catch ( ( err ) => {
73
- console . log ( "ERROR: database directory creation failed!" ) ;
101
+ log ( "ERROR: database directory creation failed!" ) ;
74
102
} ) ;
75
103
}
76
104
} ) ;
77
105
78
- console . log (
106
+ log (
79
107
`\n## Gitconvex is running on port ${ port }
80
108
81
109
Open http://localhost:${ port } / to access gitconvex
0 commit comments