Skip to content

Commit c4d98fe

Browse files
committed
init and published
1 parent 30145e7 commit c4d98fe

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
11
sleep-server
22
============
33

4-
Send your server to sleep via HTTP
4+
It's easy to wake your computer with [Wake-On-Lan](http://en.wikipedia.org/wiki/Wake-on-LAN)
5+
6+
Now you can send your computer back to sleep via HTTP
7+
8+
### Install
9+
10+
```
11+
npm install -g sleep-server
12+
```
13+
14+
### Help
15+
16+
```
17+
Usage: sleep-server [options]
18+
19+
Options:
20+
21+
-h, --help output usage information
22+
-V, --version output the version number
23+
-p, --port <port> Port to run server on (default: 57339)
24+
-H, --host <host> Interface to run server on (default: 0.0.0.0)
25+
-u, --url <url> URL to trigger sleep (default: sleep)
26+
-d, --delay <ms> Delay in milliseconds (default: 1000)
27+
```
28+
29+
#### MIT License
30+
31+
Copyright &copy; 2014 Jaime Pillora &lt;dev@jpillora.com&gt;
32+
33+
Permission is hereby granted, free of charge, to any person obtaining
34+
a copy of this software and associated documentation files (the
35+
'Software'), to deal in the Software without restriction, including
36+
without limitation the rights to use, copy, modify, merge, publish,
37+
distribute, sublicense, and/or sell copies of the Software, and to
38+
permit persons to whom the Software is furnished to do so, subject to
39+
the following conditions:
40+
41+
The above copyright notice and this permission notice shall be
42+
included in all copies or substantial portions of the Software.
43+
44+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bin/sleep-server

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('../');

package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "sleep-server",
3+
"version": "0.1.0",
4+
"main": "./server",
5+
"bin": {
6+
"sleep-server": "bin/sleep-server"
7+
},
8+
"dependencies": {
9+
"commander": "^2.2.0",
10+
"wake-event": "0.0.1"
11+
}
12+
}

server.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
var exec = require('child_process').exec;
3+
var wake = require('wake-event');
4+
var os = require('os');
5+
var http = require('http');
6+
7+
var command;
8+
switch(os.platform()) {
9+
case "win32":
10+
command = "rundll32.exe powrprof.dll,SetSuspendState 0,1,0";
11+
break;
12+
case "darwin":
13+
command = "pmset sleepnow";
14+
break;
15+
default:
16+
console.error("Operating system not supported. "+
17+
"Please send a pull request to http://github.com/jpillora/sleep-server");
18+
process.exit(1);
19+
}
20+
21+
var pkg = require('./package.json');
22+
var program = require('commander');
23+
24+
program
25+
.version(pkg.version)
26+
.option('-p, --port <port>', 'Port to run server on (default: 57339)', 57339)
27+
.option('-H, --host <host>', 'Interface to run server on (default: 0.0.0.0)', '0.0.0.0')
28+
.option('-u, --url <url>', 'URL to trigger sleep (default: sleep)', 'sleep')
29+
.option('-d, --delay <ms>', 'Delay in milliseconds (default: 1000)', 1000)
30+
.parse(process.argv);
31+
32+
//add a slash
33+
if(program.url.charAt(0) !== '/')
34+
program.url = '/'+program.url;
35+
36+
http.createServer(function (request,response) {
37+
if(request.url.indexOf(program.url) !== 0)
38+
return response.end();
39+
40+
console.log("======\nSleeping in %smn\n Time: %s\n IP address: %s",
41+
program.delay, new Date(), request.connection.remoteAddress);
42+
43+
setTimeout(function() {
44+
exec(command);
45+
}, program.delay);
46+
response.end('ok');
47+
}).listen(program.port, function() {
48+
console.log('Listening for sleep requests at http://%s:%s%s', program.host, program.port, program.url);
49+
});
50+
51+
wake(function() {
52+
console.log("======\nWoke\n Time: %s", program.delay, new Date());
53+
});
54+

0 commit comments

Comments
 (0)