Skip to content

Instantly share code, notes, and snippets.

@scottoffen
Created February 4, 2015 15:49
Show Gist options
  • Save scottoffen/764729ce1822be695b18 to your computer and use it in GitHub Desktop.
Save scottoffen/764729ce1822be695b18 to your computer and use it in GitHub Desktop.
Perl Swagger Resource Listing Generator
#!C:\lang\perl\bin\perl.exe
#!/usr/bin/perl -T
use strict;
use warnings;
use Fcntl qw(:flock);
use Try::Tiny;
use Cwd;
use JSON::PP;
#########################################||#########################################
# #
# Perl Swagger Resource Listing Generator #
# © Copyright 2014 Robot Scott LCC (http://www.robotscott.com) #
# #
# Specification : https://github.com/wordnik/swagger-core/wiki/Resource-Listing #
# #
#########################################||#########################################
#----------------------------------------------------------------------------------#
# Script Initialization #
#----------------------------------------------------------------------------------#
BEGIN
{
$| = 1;
unshift(@INC, 'C:/vhosts/staging/assets');
}
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# External Dependencies #
#----------------------------------------------------------------------------------#
use Common::Storage qw(GetFileName);
use Common::Config;
my $config = (exists GetConfig()->{'swagger'}) ? GetConfig()->{'swagger'} : { 'api-version' => '1.0.0', 'swagger-version' => '1.2' };
my $VERSION = $config->{'api-version'};
my $SWAGGER = $config->{'swagger-version'};
my $filename = GetFileName(__FILE__);
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# Initialize resource listing data structure #
#----------------------------------------------------------------------------------#
my $resources =
{
"apiVersion" => $VERSION,
"swaggerVersion" => $SWAGGER,
"apis" => []
};
if (defined $config->{info})
{
$resources->{info} = $config->{info};
}
if (defined $config->{authorizations})
{
$resources->{authorizations} = $config->{authorizations};
}
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# Retrieve a list of all services files (.rsv) in the current working directory #
#----------------------------------------------------------------------------------#
my ($servicedir, @files);
{
$servicedir = cwd();
opendir(DIR, $servicedir);
@files = readdir(DIR);
closedir DIR;
}
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# Add description and path for all services #
#----------------------------------------------------------------------------------#
foreach my $file (sort @files)
{
my $path = join("/", ($servicedir, $file));
next if ((-d $path) || ($file =~ /$filename/i));
local $/;
open(FILE, "<" . join("/", ($servicedir, $file)));
flock (FILE, LOCK_SH);
my $code = <FILE>;
close(FILE);
if ($code =~ /^#!/)
{
my $description = ($code =~ /=begin description(.+)=end description/s) ? $1 : '';
$description =~ s/^(\r?\n)+//;
$description =~ s/(\r?\n)+$//;
push(@{$resources->{apis}}, { "path" => "/../$file", "description" => $description });
}
}
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# Output the resourse listing #
#----------------------------------------------------------------------------------#
{
try
{
print "Content-type: application/json\n\n" if (exists $ENV{'HTTP_HOST'});
print encode_json($resources);
}
catch
{
print "Content-type: text/html\n\n";
print "Error creating api-docs : $_";
};
}
#----------------------------------------------------------------------------------#
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment