OSDN Git Service

init.jsを自動的に実行するようにした
authorkonekoneko <jbh03215@hotmail.co.jp>
Sat, 1 Jun 2013 09:03:47 +0000 (18:03 +0900)
committerkonekoneko <jbh03215@hotmail.co.jp>
Sat, 1 Jun 2013 09:03:47 +0000 (18:03 +0900)
init.js
main.js
readme.html

diff --git a/init.js b/init.js
index 2fffa8f..6bf7bd2 100644 (file)
--- a/init.js
+++ b/init.js
 var util = require("util");
 var config = require("./configure.js");
-var async = require("async");
 
-var MySQLPool = new require("./mysql_pool.js");
-var pool = new MySQLPool({
-                       host     : config.db_host,
-                       user     : config.db_user,
-                       password : config.db_password,
-                       port     : config.db_port,
-                       database : config.db_name,
-               });
-
-if("name_hash" in config.alias)
-{
-       if(config.alias["name_hash"].type != "unsignednumber")
-               throw "name_hashの型はunsignednumberでなければなりません";
-}else{
-       throw "name_hashが存在しません";
-}
-
-if("name" in config.alias)
-{
-       if(config.alias["name"].type != "text")
-               throw "nameの型はtextでなければなりません";
-}else{
-       throw "nameが存在しません";
-}
-
-if("password" in config.alias)
-{
-       if(config.alias["password"].type != "password")
-               throw "nameの型はpasswordでなければなりません";
-}else{
-       throw "passwordが存在しません";
-}
-
-if("lastmodified" in config.alias)
-{
-       if(config.alias["lastmodified"].type != "datetime")
-               throw "lastmodifiedの型はtextでなければなりません";
-}else{
-       throw "lastmodifiedが存在しません";
-}
-
-async.waterfall([
-       function(next){
-               var query = GetDropTableQuery("profilelist"); 
-               pool.query(query,null,next);
-       },
-       function(result,next){
-               var query = GetCreateQuery(config.alias,"profilelist"); 
-               pool.query(query,null,next);
-       },
-       function(result,next){
-               var query = GetDropTableQuery("ipbanlist"); 
-               pool.query(query,null,next);
-       },
-       function(result,next){
-               var def = {
-                       ip:{
-                               type : "text",
-                               length : 64,
-                               primary : true,
-                       },
-                       type:{
-                               type : "text",
-                               length : 1,
-                               primary : true,
-                       },
-               };
-               var query = GetCreateQuery(def,"ipbanlist"); 
-               pool.query(query,null,next);
-       },
-       function(result,next){
-               var query = GetDropTableQuery("rooms"); 
-               pool.query(query,null,next);
-       },
-       function(result,next){
-               var def = {
-                       number:
-                               {
-                                       type : "unsignednumber",
-                                       length:2,
-                                       isnotempty : true,
+module.exports = function(callback){
+       var async = require("async");
+       
+       var MySQLPool = new require("./mysql_pool.js");
+       var pool = new MySQLPool({
+                               host     : config.db_host,
+                               user     : config.db_user,
+                               password : config.db_password,
+                               port     : config.db_port,
+                               database : config.db_name,
+                       });
+       
+       if("name_hash" in config.alias)
+       {
+               if(config.alias["name_hash"].type != "unsignednumber")
+                       throw "name_hashの型はunsignednumberでなければなりません";
+       }else{
+               throw "name_hashが存在しません";
+       }
+       
+       if("name" in config.alias)
+       {
+               if(config.alias["name"].type != "text")
+                       throw "nameの型はtextでなければなりません";
+       }else{
+               throw "nameが存在しません";
+       }
+       
+       if("password" in config.alias)
+       {
+               if(config.alias["password"].type != "password")
+                       throw "nameの型はpasswordでなければなりません";
+       }else{
+               throw "passwordが存在しません";
+       }
+       
+       if("lastmodified" in config.alias)
+       {
+               if(config.alias["lastmodified"].type != "datetime")
+                       throw "lastmodifiedの型はtextでなければなりません";
+       }else{
+               throw "lastmodifiedが存在しません";
+       }
+       
+       async.waterfall([
+               function(next){
+                       var query = GetDropTableQuery("profilelist"); 
+                       pool.query(query,null,next);
+               },
+               function(result,next){
+                       var query = GetCreateQuery(config.alias,"profilelist"); 
+                       pool.query(query,null,next);
+               },
+               function(result,next){
+                       var query = GetDropTableQuery("ipbanlist"); 
+                       pool.query(query,null,next);
+               },
+               function(result,next){
+                       var def = {
+                               ip:{
+                                       type : "text",
+                                       length : 64,
                                        primary : true,
                                },
-                       password:{
-                               type : "text",
-                               length : 16,
-                       },
-                       hiddenlog:{
-                               type : "bool",
-                       },
-               };
-               var query = GetCreateQuery(def,"rooms"); 
-               pool.query(query,null,next);
-       },
-],function(err){
-       if(err != null)
-               console.log(err);
-       process.exit();
-});
-
+                               type:{
+                                       type : "text",
+                                       length : 1,
+                                       primary : true,
+                               },
+                       };
+                       var query = GetCreateQuery(def,"ipbanlist"); 
+                       pool.query(query,null,next);
+               },
+               function(result,next){
+                       var query = GetDropTableQuery("rooms"); 
+                       pool.query(query,null,next);
+               },
+               function(result,next){
+                       var def = {
+                               number:
+                                       {
+                                               type : "unsignednumber",
+                                               length:2,
+                                               isnotempty : true,
+                                               primary : true,
+                                       },
+                               password:{
+                                       type : "text",
+                                       length : 16,
+                               },
+                               hiddenlog:{
+                                       type : "bool",
+                               },
+                       };
+                       var query = GetCreateQuery(def,"rooms"); 
+                       pool.query(query,null,next);
+               },
+       ],callback);
+}
 
 function GetDropTableQuery(tablename)
 {
diff --git a/main.js b/main.js
index dc87c33..88b3c58 100644 (file)
--- a/main.js
+++ b/main.js
@@ -47,10 +47,36 @@ app.configure("production", function(){
   app.use(express.errorHandler()); \r
 });\r
 \r
-var server = http.createServer(app).listen(config.port);\r
-console.log("Express server listening on port %d in %s mode", config.port, app.settings.env);\r
+var async = require("async");\r
+var fs = require("fs");\r
 \r
-require("./chat")(app,server,express,sessionStore);\r
-\r
-if(config.enable_profile)\r
-       require("./profile")(app);\r
+async.waterfall([\r
+       function(next){\r
+               fs.exists("inited",function(exists){\r
+                       next(null,exists);\r
+               });\r
+       },\r
+       function(exists,next){\r
+               if(exists)\r
+                       next(null);\r
+               else\r
+                       fs.open("inited","a",function(err,fd){\r
+                               fs.closeSync(fd);\r
+                               var init = require("./init");\r
+                               init(next);\r
+                       });\r
+       }\r
+       ],function(err){\r
+               if(err != null)\r
+               {\r
+                       console.log(err);\r
+                       process.exit();\r
+               }\r
+               var server = http.createServer(app).listen(config.port);\r
+               console.log("Express server listening on port %d in %s mode", config.port, app.settings.env);\r
+               \r
+               require("./chat")(app,server,express,sessionStore);\r
+               \r
+               if(config.enable_profile)\r
+                       require("./profile")(app);\r
+       });\r
index 4a7c0bd..3d58509 100644 (file)
@@ -21,7 +21,6 @@
                <li><a href="http://nodejs.org/">ここ</a>のDownloadページからをnode.jsダウンロードし、インストールする</li>\r
                <li><a href="http://www-jp.mysql.com/">ここ</a>のダウンロードからMySQLをダウンロードし、インストールする</li>\r
                <li>mysql -u root -p &lt; init.sqlを実行する</li>\r
-               <li>node init.jsを実行する</li>\r
                <li>npm installを実行する</li>\r
                <li>configue.jsのusernameとpasswordを適切なものに変更する</li>\r
                <li>configue.jsのredis~を変更する(ローカル内でRedisSeverを起動する場合は不要)</li>\r
@@ -51,7 +50,6 @@
                <li>configue.jsのredis~を変更する(ローカル内でRedisSeverを起動する場合は不要)</li>\r
                <li>configue.jsのdb_~を変更する(ローカル内でRedisSeverを起動する場合は不要)</li>\r
                <li>mysql -u root -p &lt; init.sqlを実行する</li>\r
-               <li>node init.jsを実行する</li>\r
                <li>main.jsを起動する</li>\r
        </ol>\r
        <h2>チャットの管理</h2>\r