![]()
Pike ScriptRunner allows you to run Pike scripts and PSP files under non-pike based web servers. Pike ScriptRunner is a FastCGI application that will (hopefully) run under pretty much any web server that supports FastCGI.Because ScriptRunner uses FastCGI, its performance should be close to embedded language solutions like mod_perl, but with a lot less worries about security and overflows. With ScriptRunner you can introduce all of your friends to Pike without forcing them to suffer bad performance or the (more likely) attachment to Apache. In addition, ScriptRunner does a lot of the unpleasant work of parsing incoming requests, allowing you to get right to the task of writing your code.Right now, it's pretty early on in its development, so there are a lot of niceties present in Roxen or Caudium that aren't available, and it's not terribly well tested, so it could have some nasty bugs. Enough raw functionality is available to be highly useful; hopefully there will be interest in improving things in this area. ScriptRunner contains code written by others over the years; this is really more of a gathering of snippets into something more readily useful to others.Requirements:- Pike 7.6+ (available from http://pike.ida.liu.se)
www.mysite.com/path/to/ScriptRunner.fcgi www.mysite.com/some/other/path.pike www.mysite.com/path/to/ScriptRunner.fcgi/some/other/path.pike AddHandler scriptrunner-pike-script .pike AddHandler scriptrunner-pike-script .psp Action scriptrunner-pike-script /path/to/ScriptRunner.fcgi mapping parse(object id) { // note that all of the most commonly used pieces of the request are // assembled in the request id object. if(!id->variables->input) id->variables->input = "Default Text"; // the last argument is set to 1 to ensure we get a font back, even // if we don't have the one we're looking for. object fnt = Image.Fonts.load_font("Times New Roman", 72, 0, 1); object img = fnt->write(id->variables->input); string output = Image.PNG.encode(img); return (["data": output, "type": "image/png"]); } object sql; string sql_url = "mysql://user:pass@localhost/mydb";string parse(object id) { if(!sql) sql = connect_sql(sql_url); // note that we use 'sprintf' like features here. the replacement variables // get automatically quoted as appropriate for the underlying database. // we're also using the 'simple query' interface that returns the result // as an array of mappings (hashes) array sql_result = sql->query("SELECT firstname, lastname, phone FROM addresses WHERE lastname like %s", "Smith%"); if(!sizeof(sql_result)) return "sorry, there were no entries."; String.Buffer r = String.Buffer(); // In addition to standard object oriented method calls, // Pike supports operator overloading, which allows objects to // appear as though they were simple datatypes. This allows us to // start with strings, and very simply replace the more efficient // string buffer object, with only 2 changes to our code r += "<table>"; // let's loop through the result set foreach(sql_result; int index; mapping row) r += sprintf("<tr><td>%s, %s</td><td>%s</td></tr>", row->lastname, row->firstname, row->phone); r+="</table>"; // we could have just as easily cast the buffer to a string. return r->get(); }object connect_sql(string url) { object s; if(catch(s = Sql.Sql(url))) throw(Error.Generic("Unable to connect to the database.n")); return s; } Powered by PikeWiki2 |
|||
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University |