diff --git a/examples/helloruntime/HelloWorker/logger.js b/examples/helloruntime/HelloWorker/logger.js new file mode 100644 index 0000000000..e8ad708c06 --- /dev/null +++ b/examples/helloruntime/HelloWorker/logger.js @@ -0,0 +1,91 @@ +/** +* Copyright 2011 Microsoft Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var azure = require('azure') + , path = require('path') + , util = require('./util.js') + , fs = require('fs'); + +var RoleEnvironment = azure.RoleEnvironment; + +var LogFileKey = "DiagnosticLog"; +var DefaultLogFileName = "DiagnosticLog.txt"; + +exports = module.exports = Logger; + +/** +* Construct a logger that writes to the diagnostics store. +* +* @param {string} logFile: The name of the log file to write to. +*/ +function Logger() { + this.setUp = false; +} + +/** +* Write a message to the log. +* +* @param {string} message: The message to write. +*/ +Logger.prototype.write = function(message) { + this._setupLog(function(error) { + this.outStream.write(message); + }); +} + +/** +* End the log file stream. +*/ +Logger.prototype.end = function() { + this.outStream.end(); +} + +/** +* Get a readable stream of the log file contents. +* +* @return {ReadableStream}: The log file contents. +*/ +Logger.prototype.getReadableStream = function() { + return fs.createReadStream(this.logFilePath); +} + +/** +* Set up log file streaming based on configuration stored in the service definition. +* +* @param {function(error)} callback: The completion callback. +*/ +Logger.prototype._setupLog = function(callback) { + if (!this.setUp) { + util.getConfigurationSetting(LogFileKey, function(error, logFileName) { + if (!logFileName || logFileName.length < 1) { + logFileName = DefaultLogFileName; + } + this.logFileName = logFileName; + util.getDiagnosticPath(function( error, logPath) { + this.setUp = true; + if (error) { + this.logFilePath = "console"; + console.log("Error getting diagnostic store, using stdout instead: " + error + "\r\n"); + this.outStream = process.stdout; + callback(error); + } else { + this.logFilePath = path.join(logPath, this.logFileName); + this.outStream = fs.createWriteStream(this.logFilePath); + callback(null); + } + }); + }); + } +} diff --git a/examples/helloruntime/HelloWorker/package.json b/examples/helloruntime/HelloWorker/package.json new file mode 100644 index 0000000000..17435de59c --- /dev/null +++ b/examples/helloruntime/HelloWorker/package.json @@ -0,0 +1,10 @@ +{ + "name": "HelloWorker" + , "version": "0.0.1" + , "private": true + , "dependencies": { + "azure": ">=0.5.4" + , "express": "2.5.8" + , "jade": ">= 0.0.1" + } +} \ No newline at end of file diff --git a/examples/helloruntime/HelloWorker/public/stylesheets/style.css b/examples/helloruntime/HelloWorker/public/stylesheets/style.css new file mode 100644 index 0000000000..30e047daef --- /dev/null +++ b/examples/helloruntime/HelloWorker/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} \ No newline at end of file diff --git a/examples/helloruntime/HelloWorker/routes/index.js b/examples/helloruntime/HelloWorker/routes/index.js new file mode 100644 index 0000000000..fd69215b27 --- /dev/null +++ b/examples/helloruntime/HelloWorker/routes/index.js @@ -0,0 +1,8 @@ + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'Express' }) +}; \ No newline at end of file diff --git a/examples/helloruntime/HelloWorker/server.js b/examples/helloruntime/HelloWorker/server.js new file mode 100644 index 0000000000..d1bcb46f73 --- /dev/null +++ b/examples/helloruntime/HelloWorker/server.js @@ -0,0 +1,58 @@ +/** +* Copyright 2011 Microsoft Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/** + * Module dependencies. + */ + +var express = require('express') + , Logger = require('./logger.js') + , routes = require('./routes') + , utility = require('./util.js'); + +var app = module.exports = express.createServer(); + +var Endpoint = "HttpIn"; + +var log = new Logger(); + +// Configuration + +app.configure(function(){ + app.set('views', __dirname + '/views'); + app.set('view engine', 'jade'); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(app.router); + app.use(express.static(__dirname + '/public')); +}); + +app.configure('development', function(){ + app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); +}); + +app.configure('production', function(){ + app.use(express.errorHandler()); +}); + +// Routes + +app.get('/', routes.index); + +utility.getListenAddress(Endpoint, function(error, host, port) { + app.listen(port, host); + console.log("Express server listening on hostname %s port %d in %s mode", host, port, app.settings.env); + log.write("Express server listening on hostname " + host +", port " + port + " in " + app.settings.env + "mode"); +}); diff --git a/examples/helloruntime/HelloWorker/util.js b/examples/helloruntime/HelloWorker/util.js new file mode 100644 index 0000000000..fd2362cae8 --- /dev/null +++ b/examples/helloruntime/HelloWorker/util.js @@ -0,0 +1,87 @@ +/** +* Copyright 2011 Microsoft Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var azure = require('azure'); + +var RoleEnvironment = azure.RoleEnvironment; + +var DiagnosticStoreKey = "DiagnosticStore"; +var PathKey = "path"; + +exports = module.exports; + +/** +* Return the path to the diagnostic store. +* +* @param {function(error, path)} callback: The completion callback. +*/ +exports.getDiagnosticPath = function(callback) { + exports.getLocalResourcePath(DiagnosticStoreKey, callback); +} + +/** +* Returne the path to the named local resource. +* +* @param {string} resourceName: Name of the local storage resource. +* @param {function(error, path)} callback: The completion callback. +*/ +exports.getLocalResourcePath = function(resourceName, callback) { + RoleEnvironment.getLocalResources(function( error, resources) { + if (error) { + callback(error, null); + } + else { + if (resources[resourceName] && resources[resourceName][PathKey]) { + callback(null, resources[resourceName][PathKey]); + } else { + callback("Could not find resource '"+ resourceName + "'\r\n"); + } + } + }); +} + +/** +* Get a specific role configuration setting. +* +* @param {string} key: The name of the configuration setting. +* @param {function(error, setting)} callback: The completion callback. +*/ +exports.getConfigurationSetting = function(key, callback) { + RoleEnvironment.getConfigurationSettings( function(error, settings) { + if (!error) { + callback(null, settings[key]); + } else { + callback(error, null); + } + + }); +} + +/** +* Get the hostname and port associated with the given endpoint for this role instance. +* +* @param {string} endpointName: The name of the endpoint in the service definition file. +* @param {function(error, host, port)} callback: The completion callback. +*/ +exports.getListenAddress = function(endpointName, callback) { + RoleEnvironment.getCurrentRoleInstance(function( inErr, instance) { + if (inErr || !instance['endpoints'] || ! instance['endpoints'][endpointName]) { + callback("Error, could not get current role instance endpoint '"+ endpointName + "', error: " + inErr + "\r\n", null, null); + } else { + var currentEndpoint = instance['endpoints'][endpointName]; + callback(null, currentEndpoint['address'], currentEndpoint['port']); + } + }); +} \ No newline at end of file diff --git a/examples/helloruntime/HelloWorker/views/index.jade b/examples/helloruntime/HelloWorker/views/index.jade new file mode 100644 index 0000000000..c9c35fa996 --- /dev/null +++ b/examples/helloruntime/HelloWorker/views/index.jade @@ -0,0 +1,2 @@ +h1= title +p Welcome to #{title} \ No newline at end of file diff --git a/examples/helloruntime/HelloWorker/views/layout.jade b/examples/helloruntime/HelloWorker/views/layout.jade new file mode 100644 index 0000000000..1a36941265 --- /dev/null +++ b/examples/helloruntime/HelloWorker/views/layout.jade @@ -0,0 +1,6 @@ +!!! +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body!= body \ No newline at end of file diff --git a/examples/helloruntime/ServiceConfiguration.Cloud.cscfg b/examples/helloruntime/ServiceConfiguration.Cloud.cscfg new file mode 100644 index 0000000000..9c6bb69515 --- /dev/null +++ b/examples/helloruntime/ServiceConfiguration.Cloud.cscfg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/examples/helloruntime/ServiceConfiguration.Local.cscfg b/examples/helloruntime/ServiceConfiguration.Local.cscfg new file mode 100644 index 0000000000..9c6bb69515 --- /dev/null +++ b/examples/helloruntime/ServiceConfiguration.Local.cscfg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/examples/helloruntime/ServiceDefinition.csdef b/examples/helloruntime/ServiceDefinition.csdef new file mode 100644 index 0000000000..c0ad4db1e5 --- /dev/null +++ b/examples/helloruntime/ServiceDefinition.csdef @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file