summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgi-bin/shader_api.js92
-rwxr-xr-xcgi-bin/view29
2 files changed, 113 insertions, 8 deletions
diff --git a/cgi-bin/shader_api.js b/cgi-bin/shader_api.js
new file mode 100644
index 0000000..dd43130
--- /dev/null
+++ b/cgi-bin/shader_api.js
@@ -0,0 +1,92 @@
+;var ShaderAPI = {}
+ShaderAPI.limit = 24
+
+// info - fetch a single shader
+// id: shader id
+ShaderAPI.info = function(id, cb){
+ ShaderAPI.fetch({
+ f: "info",
+ id: id
+ }, cb)
+}
+
+// all - fetch all shaders
+ShaderAPI.all = function(cb){
+ ShaderAPI.fetch({
+ f: "all"
+ }, cb)
+}
+
+// range - fetch a pageful of results
+// limit: number of shaders to fetch
+// offset: number of results to skip
+ShaderAPI.range = function(limit, offset, cb){
+ ShaderAPI.fetch({
+ f: "range",
+ limit: limit || ShaderAPI.limit,
+ last: offset
+ }, cb)
+}
+
+// latest - get the latest N shaders
+// limit: number of shaders to fetch
+ShaderAPI.latest = function(limit, cb){
+ ShaderAPI.fetch({
+ f: "range",
+ limit: limit || ShaderAPI.limit
+ }, cb)
+}
+
+// page - get a page of N results
+// page: page number, start counting at 1
+// limit: number of shaders to fetch
+ShaderAPI.page = function(page, limit, cb){
+ ShaderAPI.fetch({
+ f: "range",
+ last: (page-1) * limit,
+ limit: limit || ShaderAPI.limit
+ }, cb)
+}
+
+// history - get all previous versions of a shader
+// id: shader id
+ShaderAPI.history = function(id, cb){
+ ShaderAPI.fetch({
+ f: "history",
+ id: id
+ }, cb)
+}
+
+// username - get all ids by a user
+ShaderAPI.username = function(username, cb){
+ ShaderAPI.fetch({
+ f: "username",
+ username: username
+ }, cb)
+}
+
+// list_users - list all users
+ShaderAPI.list_users = function(list_users, cb){
+ ShaderAPI.fetch({
+ f: "list_users"
+ }, cb)
+}
+
+// originals - get the earliest version of all named shaders
+ShaderAPI.originals = function(cb){
+ ShaderAPI.fetch({
+ f: "originals"
+ }, cb)
+}
+
+// fetch - AJAX wrapper
+ShaderAPI.fetch = function(params, cb){
+ $.getJSON("/cgi-bin/im/shader/view", params, function(data){
+ if (data.SUCCESS) {
+ cb(null, data.data)
+ }
+ else if (data.ERROR) {
+ cb(data.ERROR, data.data)
+ }
+ })
+}
diff --git a/cgi-bin/view b/cgi-bin/view
index 526d268..2e9a540 100755
--- a/cgi-bin/view
+++ b/cgi-bin/view
@@ -15,13 +15,25 @@ my $sql_passwd = "gTYgT&M6q";
sub not_specified_error{
-my $param = shift;
- print $json->pretty->encode ({ ERROR => sprintf("Function parameter %s not defined", $param ) });
+ my $param = shift;
+ my $callback = shift;
+ response({ ERROR => sprintf("Function parameter %s not defined", $param ) }, $callback);
exit(1);
}
+sub response{
+ my $res = $json->pretty->encode(shift);
+ my $callback = shift;
+ if (defined $callback){
+ print $callback + "(" + $res + ")";
+ }
+ else {
+ print $res;
+ }
+}
our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd)
or die "Couldn't connect to database: " . DBI->errstr;
+
sub mysql_retrieve{
my ($statement, @args) = @_;
my $sth = $dbh->prepare($statement)
@@ -37,11 +49,11 @@ sub mysql_retrieve{
push(@rows, $data);
}
if ($sth->rows == 0) {
- return $json->pretty->encode ({ ERROR => "No ids matched `$data_id'.\n\n" });
+ return { ERROR => "No ids matched `$data_id'.\n\n" };
}
$sth->finish;
- return $json->pretty->encode({ SUCCESS => 1, data => \@rows });
+ return { SUCCESS => 1, data => \@rows };
}
sub main{
@@ -53,9 +65,10 @@ sub main{
$params->{$key} = $value;
}
+ my $callback = $params->{'callback'} || undef;
unless(defined $params->{f}){
- not_specified_error("f");
+ not_specified_error("f", $callback);
}
my $res;
@@ -89,12 +102,12 @@ sub main{
when(/originals/){
$res = mysql_retrieve('select shaders.* from shader_ids cross join shaders on shader_ids.id = shaders.shader_id');
}
- default {
- print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"});
+ default {
+ response({ ERROR => "Function parameter f improperly defined"}, $callback);
exit(1);
};
}
- print $res;
+ response($res, $callback);
}
main();
exit(0);