Permalink
Browse files

update readme, enhance cli option

  • Loading branch information...
1 parent de9105f commit 5c0bbbcc65562950542a13a9588cba1f7e29fbf2 @rhiokim committed Feb 9, 2012
Showing with 158 additions and 60 deletions.
  1. +3 −0 .gitignore
  2. +7 −2 Makefile
  3. +14 −3 README.md
  4. +73 −7 bin/_locally
  5. +1 −17 bin/locally
  6. +57 −28 lib/locally.js
  7. +3 −3 package.json
View
@@ -0,0 +1,3 @@
+.DS_Store
+Makefile
+
View
@@ -1,2 +1,7 @@
-init:
- echo "a" >>
+build:
+ echo '#!/usr/bin/env node \n' >> .tmp
+ cat ./lib/locally.js >> .tmp
+ chmod 755 .tmp
+ mv .tmp ./bin/_locally
+
+.PHONY: build
View
@@ -1,5 +1,15 @@
+#intro
+locally 는 웹 개발 시 로컬 서버를 손쉽게 생성해서 테스트 개발환경을 구축하기 위한 Node.js 기반의 미들웨어이다. 이 프로젝트는 Python 의 SimpleHTTPServer 에서 아이디어를 얻었다.
+
+언제, 어느 작업 디렉토리에 있던 내가 원하는 로컬 서버를 설정하기 위해서 사용된다.
+
+뿐만 아니라 최근의 웹 기술중 서버측 css인 less 를 지원한다. 그리고 향후 유용한 서버측 환경설정 프리셋을 제공하여 손쉽게 마크업 개발 환경을 만들 예정이다.
+
+#locally middleware
+locally 는 connect 모듈을 기반으로 동작하는데 connect 모듈은 다양한 미들웨어를 추가해서 high class 기능등을 사용할 수 있다. 뿐만 아니라 손쉽게 로컬 웹 서버를 구축해서 마크업 환경을 설정할 수 있다.
#usage
+**directory structure**
```
./
@@ -14,6 +24,8 @@
```
+**basic command**
+
```
//simple command
$ locally -p 8080
@@ -25,11 +37,13 @@ $ curl http://localhost:8080/
$ curl http://localhost:8080/about
```
+**python**
```python
$ python -m SimpleHTTPServer -p 8080
```
+**help cli**
```
$ node lib/locally.js --help
@@ -49,6 +63,3 @@ $ node lib/locally.js --help
-d, --public <directory> directory for serving public files
```
-http://localhost:8080/
-http://localhost:8080/about/
-
View
@@ -1,16 +1,82 @@
-#!/usr/bin/env node
+#!/usr/bin/env node
var connect = require('connect'),
- program = require('commander');
+ program = require('commander'),
+ fs = require('fs');
+/**
+ * process.argv pre process
+ */
program
- .version('0.0.1')
+ .version('0.1.0')
.option('-s, --static <directory>', 'directory for serving static files', 'static')
.option('-d, --public <directory>', 'directory for serving public files', 'public')
+ .option('-p, --port <port>', 'public port', 8080)
+ .option('-f, --file <configuration>', 'set configuration file', '.locally')
+ .option('-v, --vhost <virtual host>', 'set vhost information', 'localhost')
+ .option('-i, --init', 'interactive locally project initialize')
.parse(process.argv);
-program.on('static', function() {
- console.log('setting static directory');
- process.exit();
-});
+if ( program.init || !program.file ) {
+ program.prompt('document root [default: ./public] :', function(dir) {
+ var conf = '';
+
+ console.log('document root %s', __dirname +'/'+ dir) ;
+ conf += '-d '+ dir +'\n';
+
+ program.prompt('port [default: 8080] :', Number, function(port) {
+ if (port) {
+ conf += '-p '+ port +'\n';
+ }
+ console.log(port);
+
+ process.stdin.destroy();
+ console.log('end process')
+ fs.writeFile('.locally', conf, 'utf8', function(err) {
+ loadConfiguration();
+ createServer();
+ });
+ });
+ });
+}
+
+function loadConfiguration() {
+ // locally.opt support
+ try {
+ var opts = fs.readFileSync(__dirname +'/'+ program.file , 'utf8')
+ .trim()
+ .split(/\s+/);
+
+ process.argv = process.argv
+ .slice(0,2)
+ .concat(opts.concat(process.argv.slice(2)));
+ } catch(err) {
+
+ }
+}
+
+
+/**
+ * express binding
+ */
+function createServer() {
+ var app = connect(
+ connect.static(__dirname +'/'+ program.public),
+ connect.bodyParser()
+ );
+
+ app.use(function(req, res, next) {
+ if('GET' != req.method) {
+ return next();
+ }
+
+ res.statusCode = 302;
+ res.setHeader('Location', req.url);
+ res.end();
+ });
+
+ app.listen(program.port);
+
+ console.info('Serving started at http://localhost:'+ program.port);
+}
View
@@ -8,21 +8,5 @@
var spawn = require('child_process').spawn
, args = [ __dirname + '/_locally' ];
-process.argv.slice(2).forEach(function (arg) {
- switch (arg) {
- case '-s':
- case '--static':
- args.unshift('static');
- break;
- case '-www':
- case '-public':
- args.unshift('--public');
- break;
- default:
- args.push(arg);
- break;
- }
-});
-
-var proc = spawn(process.argv[0], args, { customFds: [0,1,2] });
+var proc = spawn(process.argv[0], args);
proc.on('exit', process.exit);
View
@@ -6,46 +6,75 @@ var connect = require('connect'),
* process.argv pre process
*/
program
- .version('0.0.1')
+ .version('0.1.0')
.option('-s, --static <directory>', 'directory for serving static files', 'static')
- .option('-d, --public <directory>', 'directory for serving public files', 'public');
+ .option('-d, --public <directory>', 'directory for serving public files', 'public')
+ .option('-p, --port <port>', 'public port', 8080)
+ .option('-f, --file <configuration>', 'set configuration file', '.locally')
+ .option('-v, --vhost <virtual host>', 'set vhost information', 'localhost')
+ .option('-i, --init', 'interactive locally project initialize')
+ .parse(process.argv);
-program.on('--static', function() {
- console.log('setting static directory');
- process.exit();
-});
+if ( program.init || !program.file ) {
+ program.prompt('document root [default: ./public] :', function(dir) {
+ var conf = '';
-program
- .command('init')
- .description('locally setup webserver configuration')
- .action(function() {
- program.prompt('static directory :', function(dir) {
- console.log('static -> %s', dir);
- });
+ console.log('document root %s', __dirname +'/'+ dir) ;
+ conf += '-d '+ dir +'\n';
+
+ program.prompt('port [default: 8080] :', Number, function(port) {
+ if (port) {
+ conf += '-p '+ port +'\n';
+ }
+ console.log(port);
- program.prompt('public directory :', function(dir) {
- console.log('public -> %s', dir);
+ process.stdin.destroy();
+ console.log('end process')
+ fs.writeFile('.locally', conf, 'utf8', function(err) {
+ loadConfiguration();
+ createServer();
+ });
});
});
+}
+function loadConfiguration() {
+ // locally.opt support
+ try {
+ var opts = fs.readFileSync(__dirname +'/'+ program.file , 'utf8')
+ .trim()
+ .split(/\s+/);
-// locally.opt support
-try {
- var opts = fs.readFileSync(__dirname +'/locally.opt', 'utf8')
- .trim()
- .split(/\s+/);
-
- process.argv = process.argv
- .slice(0,2)
- .concat(opts.concat(process.argv.slice(2)));
-} catch(err) {
+ process.argv = process.argv
+ .slice(0,2)
+ .concat(opts.concat(process.argv.slice(2)));
+ } catch(err) {
+ }
}
-program.parse(process.argv);
-
/**
- * connect binding
+ * express binding
*/
+function createServer() {
+ var app = connect(
+ connect.static(__dirname +'/'+ program.public),
+ connect.bodyParser()
+ );
+
+ app.use(function(req, res, next) {
+ if('GET' != req.method) {
+ return next();
+ }
+
+ res.statusCode = 302;
+ res.setHeader('Location', req.url);
+ res.end();
+ });
+
+ app.listen(program.port);
+
+ console.info('Serving started at http://localhost:'+ program.port);
+}
View
@@ -1,11 +1,11 @@
{
"author": "Rhio Kim <rhio.kim@gmail.com> (http://about.me/rhio)",
- "name": "localy",
- "description": "simple, locally webserver",
+ "name": "locally",
+ "description": "simple, static file web server",
"version": "0.0.0",
"homepage": "about.me/rhio",
"repository": {
- "url": ""
+ "url": "git@github.com:rhiokim/locally.git"
},
"engines": {
"node": "~0.6.9"

0 comments on commit 5c0bbbc

Please sign in to comment.