· 7 years ago · Sep 03, 2018, 03:50 PM
1/** MySQL Table structure:
2--
3-- Table structure for table `actors`
4--
5
6CREATE TABLE IF NOT EXISTS `actors` (
7 `id` int(11) NOT NULL AUTO_INCREMENT,
8 `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT 'test',
9 `creation` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
10 PRIMARY KEY (`id`)
11) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
12
13--
14-- Dumping data for table `actors`
15--
16
17INSERT INTO `actors` (`id`, `name`, `creation`) VALUES
18(1, 'test', '2012-01-11 04:27:10'),
19(2, 'test', '2012-01-11 04:27:20'),
20(3, 'test', '2012-01-11 04:27:20'),
21(4, 'test', '2012-01-11 04:27:20');
22
23npm install async mysql memcached colors trycatch
24 */
25var http = require("http"),
26 async = require("async"),
27 fs = require("fs"),
28 mysql = require("mysql"),
29 memcached = require("memcached"),
30 colors = require("colors"),
31 trycatch = require("trycatch");
32
33// We put our server instances (local server and connections to remote ones) in here
34var server = {};
35
36// I want the query to fail one out of five times. see below
37var incr = 0;
38
39// Getting some data from memcached and mysql
40function getData(cb){
41
42 console.log("getting mysql data".green);
43
44 server.mysql.query("select * from actors", function(err, mySqlData){
45
46 console.log("got mysql data".green);
47
48 if(err)
49 throw err;
50
51 console.log("getting memcached data".green);
52
53 server.memcached.get("test", function(err, memcachedData){
54
55 console.log("Formatting data".green);
56
57 // Increment the number of time this function has been called
58 incr++;
59
60 // Here we want the call to fail. We simulate that something
61 // went wrong, and expect it to bubble back to the top
62 if(incr % 5 == 0)
63 throw new Error("Congrats, you are the 5th user!");
64
65 if(err)
66 throw err;
67
68 var data = {
69 mysql : mySqlData,
70 memcached : memcachedData
71 }
72
73 console.log("returning data".green);
74
75 cb(data);
76 });
77 });
78}
79
80// We setup membase connection, mysql connection, and a web server on
81// a local socket file (I was using nGinx as a fronted)
82async.waterfall([
83
84 // Set MySQL
85 function(cb){
86
87 console.log("Connecting to MySQL...".grey);
88
89 server.mysql = mysql.createClient({
90 user: 'user',
91 password: 'pass',
92 });
93
94 server.mysql.query("USE errors", function(err){
95 if(err)
96 return cb("Could not initialize MySQL Database: " + err)
97
98 console.log("MySQL connected.".grey);
99 return cb(null);
100 });
101 },
102 // Set Memcached
103 function(cb){
104 console.log("Connecting to Membase...".grey);
105 server.memcached = new memcached('localhost:11211');
106 console.log("Membase connected.".grey);
107 return cb(null);
108 },
109 // Set Web Server
110 function(cb){
111
112 console.log("Creating Web Server...".grey);
113
114 server.http = http.createServer(function(req, res){
115
116 console.log("Receiving call...".green);
117
118 trycatch(function(){
119
120 console.log("Calling getData...".green);
121
122 // We add a timestamp to the response object
123 res.timestamp = Date.now();
124 console.log(res.timestamp);
125
126 getData(function dataReturn(data){
127 console.log("Sending data...".green);
128 res.end(JSON.stringify(data));
129 });
130 },
131 function(err, res){
132 console.log("Sending error data...", arguments);
133
134 // We print the timestamp; if you notice, this is the timestamp
135 // of the first request, not the current.
136 console.log(res.timestamp);
137
138 res.writeHead(500);
139 res.end("nok");
140
141 console.error(err.stack);
142 }, res);
143 });
144
145 server.http.listen("server.sock", function(err){
146 if(err){
147 console.error("Could not set unix socket: ".red.bold);
148 return cb(err);
149 }
150
151 // Make the socket writable
152 fs.chmodSync("server.sock", "0777");
153
154 console.log("Web Server created succesfully.".grey);
155 cb(null);
156 });
157 },
158], function(err){
159 if(err){
160 console.error("Exiting?".red.bold, err);
161 throw err;
162 }
163 else
164 console.log("Setup complete".green);
165});