Hosting Go (or any CGI apps) on Windows Azure Websites
Run Process on Website
It’s a little known fact, but you can spawn some server-side processes from your Windows Azure Website.
For example, this Node server will execute any command you pass in on the end of the URL:
var childProcess = require('child_process'); var http = require('http'); http.createServer(function(req,res){ var cmd = req.url.replace("/","").replace(/%20/g, " "); childProcess.exec(cmd, function(err, stdout, stderr){ if (err) res.end(JSON.stringify(err)); else res.end(stdout); }); }).listen(process.env.port);
…so running a command like this:
http://xxx.azurewebsites.net/dir
Will give you something like this:
Volume in drive C has no label. Volume Serial Number is 7C6E-015C Directory of C:\DWASFiles\Sites\xxx\VirtualDirectory0\site\wwwroot 08/29/2013 01:10 PM . 08/29/2013 01:10 PM .. 08/29/2013 12:46 PM 44 .gitignore 08/29/2013 12:46 PM 915 iisnode.yml 08/29/2013 12:46 PM 366 server.js 08/29/2013 01:33 PM 640 web.config 4 File(s) 1,288,915 bytes 2 Dir(s) 1,069,420,544 bytes free
In fact, you can bring your own application and run that. Just add the .exe, and any dependencies, and start the process as shown in the first example.
This is exactly how CGI works.
Create a CGI Host
There is a node module which will host CGI applications using Node’s HTTP server: https://github.com/TooTallNate/node-cgi
So we can use Node as a CGI host for any application which compiles to Windows!
To create a server, install the cgi module:
$ npm install cgi
And create a server.js file like this:
var cgi = require('cgi'); var http = require('http'); var path = require('path'); var script = path.resolve("./", 'NAME_OF_YOUR_APP.EXE'); http.createServer( cgi(script) ).listen(process.env.port);
Just replace NAME_OF_YOUR_APP.EXE with the name of your application, this executable should be included in your git repository (assuming you’re using git to push to Website!).
Create a CGI Application
To test this, I created a CGI application in Go (test.go):
package main import ( "net/http" "net/http/cgi" ) func myHandler(rw http.ResponseWriter, req *http.Request) { rw.Header().Add("Content-Type", "text/plain") rw.Write([]byte("Hello World!")) } func main() { http.HandleFunc("/", myHandler) cgi.Serve(nil) }
To compile, just use:
> go build test.go
I added the test.exe file to my repository, switched the ‘NAME_OF_YOUR_APP.EXE’ to ‘test.exe’, and pushed up the changes:
It works! And shows that we can host CGI apps in Windows Azure Websites using languages outside the subset that have primary support on Azure.
Ok, CGI is not the fastest piece of technology in the world, and web programming has moved on, but it could help people move legacy applications up to the Cloud.
Reply
You must be logged in to post a comment.