1 | 'use strict';
|
2 |
|
3 | const fs=require('fs');
|
4 |
|
5 | const passedArgs = process.argv.splice(2),
|
6 | argCount = passedArgs.length,
|
7 | args = {};
|
8 |
|
9 | const defaults = {
|
10 | port : 8080,
|
11 | root : process.cwd(),
|
12 | domain : '0.0.0.0',
|
13 | index : 'index.html',
|
14 | log : false
|
15 | };
|
16 |
|
17 | for(let i=0; i<argCount; i++){
|
18 | const data=passedArgs[i].split('=');
|
19 | args[data[0]]=data[1];
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | class Config{
|
27 | constructor(userConfig){
|
28 | Object.assign(this,defaultConfigs);
|
29 |
|
30 | if(userConfig){
|
31 | for(const k in userConfig){
|
32 | this[k]=userConfig[k];
|
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 | const defaultConfigs={
|
59 | verbose : (args.verbose=='true')||false,
|
60 | port : args.port||defaults.port,
|
61 | root : args.root||defaults.root,
|
62 | domain : args.domain||defaults.domain,
|
63 | log : false,
|
64 | logFunction : serverLogging,
|
65 | domains : {
|
66 |
|
67 | },
|
68 | server : {
|
69 | index : args.index||defaults.index,
|
70 | noCache : args.noCache=='false' ? false : true,
|
71 | timeout : 30000
|
72 | },
|
73 | https:{
|
74 | ca:'',
|
75 | privateKey:'',
|
76 | certificate:'',
|
77 | passphrase:false,
|
78 | port:443,
|
79 | only:false
|
80 | },
|
81 | contentType : {
|
82 | html : 'text/html',
|
83 | css : 'text/css',
|
84 | js : 'text/javascript',
|
85 | json : 'application/json',
|
86 | txt : 'text/plain',
|
87 | jpeg : 'image/jpeg',
|
88 | jpg : 'image/jpeg',
|
89 | png : 'image/png',
|
90 | gif : 'image/gif',
|
91 | ico : 'image/x-icon',
|
92 | appcache: 'text/cache-manifest'
|
93 | },
|
94 | restrictedType: {
|
95 |
|
96 | },
|
97 | errors:{
|
98 | headers : {
|
99 | 'Content-Type' : 'text/plain'
|
100 | },
|
101 | 404: '404 MIA',
|
102 | 415: '415 File type not supported',
|
103 | 403: '403 Access Denied',
|
104 | 500: '500 {{err}}'
|
105 | }
|
106 | };
|
107 |
|
108 | function serverLogging(data){
|
109 | fs.exists(
|
110 | this.log,
|
111 | function serverLogExsits(exists){
|
112 | data.timestamp=new Date().getTime();
|
113 |
|
114 | const JSONData=JSON.stringify(data);
|
115 | let method='appendFile';
|
116 | if(!exists){
|
117 | method='writeFile';
|
118 | }
|
119 | fs[method](
|
120 | this.log,
|
121 | JSONData,
|
122 | function fsMethod(err) {
|
123 | if(err){
|
124 | console.log(err);
|
125 | }
|
126 | }
|
127 | );
|
128 | }.bind(this)
|
129 | );
|
130 | }
|
131 |
|
132 | module.exports=Config;
|