michael 4 лет назад
Родитель
Сommit
fd43a74ae0
4 измененных файлов: 40 добавлений и 19 удалений
  1. +3
    -0
      .gitignore
  2. +6
    -0
      config/srvconfig.json
  3. +25
    -11
      src/index.ts
  4. +6
    -8
      stop.sh

+ 3
- 0
.gitignore Просмотреть файл

@@ -26,6 +26,9 @@
node_modules
bower_components

# Log directory
logs

# Built files .js
dist/**/*.js
dist/**/*.js.map


+ 6
- 0
config/srvconfig.json Просмотреть файл

@@ -1,5 +1,11 @@
{
"port": 8088,
"logOpts": {
"logFilePath": "/home/michael/jsworkspace/nas-webdav/logs/nas-webdav.log",
"timestampFormat": "YYYY-MM-DD HH:mm:ss.SSS",
"level": "debug"

},
"realm": "WebDAV-manas",
"mounts": {
"Music": "/media/hd1/Music",


+ 25
- 11
src/index.ts Просмотреть файл

@@ -1,6 +1,10 @@
import * as fs from 'fs';
import * as path from 'path';
import * as http from 'http';
import { v2 as webdav } from 'webdav-server';
import * as simpleLogger from 'simple-node-logger';

// const simpleLogger = require('simple-node-logger');

function getConfig(name: string) : any {
let config = {};
@@ -23,12 +27,22 @@ let server: webdav.WebDAVServer;
// @ts-ignore
let httpd: http.Server;

let log;

(async ()=>{
let PIDFILE = ',,/pid';
let PIDFILE = path.normalize(path.resolve(__dirname, '../logs/pid'));
try {
const config = getConfig('srvconfig');
const users = getConfig('users');
if (config.logOpts === undefined)
log = console;
else {
if (process.stdout.isTTY)
log = simpleLogger.createSimpleLogger(config.logOpts);
else
log = simpleLogger.createSimpleFileLogger(config.logOpts);
}
log.debug(PIDFILE);
if (typeof config.port === undefined)
config.port = 8080;
if (typeof config.realm === undefined)
@@ -40,7 +54,7 @@ let httpd: http.Server;
mountNames.push(mount);
}

console.log(config['mounts']);
log.info(config['mounts']);
// User manager (tells who are the users)
const userManager: webdav.SimpleUserManager = new webdav.SimpleUserManager();
@@ -60,7 +74,7 @@ let httpd: http.Server;
}
}
}
console.log(user.username);
log.info(user.username);
}
const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm);
@@ -74,7 +88,7 @@ let httpd: http.Server;

server = new webdav.WebDAVServer(serverOptions);
server.beforeRequest((ctx: webdav.HTTPRequestContext, next) => {
console.log('beforeRequest', ctx.request.method);
log.debug('beforeRequest: ', ctx.request.method, ' Headers recv:', ctx.request.rawHeaders);
if (ctx.request.method === 'OPTIONS') {
ctx.response.setHeader('DAV', '1,2');
ctx.response.setHeader('Access-Control-Allow-Origin', '*');
@@ -108,7 +122,6 @@ let httpd: http.Server;
}
});
server.afterRequest((_ctx: webdav.HTTPRequestContext, next) => {
console.log('After request');
// Display the method, the URI, the returned status code and the returned message
/*
console.log(
@@ -119,7 +132,7 @@ let httpd: http.Server;
// If available, display the body of the response
console.log('RESPONSEBODY', _arg.responseBody);
*/
console.log('Headers',_ctx.response.getHeaders(), 'Headers sent', _ctx.response.headersSent);
log.debug(`After request: Status code: ${_ctx.response.statusCode} Headers sent: (${_ctx.response.headersSent})`,_ctx.response.getHeaders());
next();
});
@@ -128,10 +141,10 @@ let httpd: http.Server;
let realPath = config['mounts'][fsEntry];
// @ts-ignore
let bOk = await server.setFileSystemAsync('/' + fsEntry, new webdav.PhysicalFileSystem(realPath));
console.log(`Mounted (${bOk}) /${fsEntry} -> ${realPath}`);
log.info(`Mounted (${bOk}) /${fsEntry} -> ${realPath}`);
}
let onexit = async () => {
console.log("Terminated");
log.info("Terminated");
await server.stopAsync();
if (fs.existsSync(PIDFILE))
fs.unlinkSync(PIDFILE);
@@ -141,10 +154,11 @@ let httpd: http.Server;
process.on('SIGINT', onexit);
httpd = await server.startAsync(config.port);
fs.writeFileSync(PIDFILE, '' + process.pid);
console.log("WebDAV server is listening on port " + config.port);
log.info("WebDAV server is listening on port " + config.port);
}
catch (err) {
console.error(err);
log.error(err);
process.exit(1);
}
})();


+ 6
- 8
stop.sh Просмотреть файл

@@ -5,20 +5,18 @@ PIDVAL=""

pidfile() {
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PIDFILE="$DIR/pid"
PIDFILE="$DIR/logs/pid"
if [ ! -f $PIDFILE ]; then
return PIDFILE
else
return ""
fi
PIDFILE=""
fi
}

pidval() {
pidfile
if [ ! -z $PIDFILE ]; then
PIDVAL =`pgrep -F "$PIDFILE"`
PIDVAL=`pgrep -F "$PIDFILE"`
else
PIDVAL = ""
PIDVAL=""
fi
}

@@ -32,7 +30,7 @@ if [ ! -z "$PIDVAL" ]; then
COUNTER=0
else
sleep 0.1
let ((--COUNTER))
((--COUNTER))
fi
done
if [ ! -z $PIDVAL ]; then


Загрузка…
Отмена
Сохранить