NAME
Geo::OGC::Service - Perl extension for geospatial web services
SYNOPSIS
In a service.psgi file write something like this
use strict;
use warnings;
use Plack::Builder;
use Geo::OGC::Service;
use Geo::OGC::Service::WFS;
my $app = Geo::OGC::Service->new({
config => '/var/www/etc/test.conf',
services => {
test => 'Geo::OGC::Service::Test',
});
builder {
mount "/wfs" => $app->to_mount;
mount "/" => $defaul_app;
};
The bones of a service class are
package Geo::OGC::Service::Test;
sub process_request {
my ($self, $responder) = @_;
my $writer = $responder->([200, [ 'Content-Type' => 'text/plain',
'Content-Encoding' => 'UTF-8' ]]);
$writer->write("I'm ok!");
$writer->close;
}
DESCRIPTION
This module provides psgi_app and respond methods for booting a web service.
SERVICE CONFIGURATION
Setting up a PSGI service consists typically of three things:
1) write a service.psgi file (see above) and put it somewhere like
/var/www/service/service.psgi
2) Set up starman service and add to its init-file line something like
exec starman --daemonize --error-log /var/log/starman/log --l localhost:5000 /var/www/service/service.psgi
3) Add a proxy service to your apache configuration
<Location /TestApp>
ProxyPass http://localhost:5000
ProxyPassReverse http://localhost:5000
</Location>
Setting up a geospatial web service configuration requires a configuration file, for example
/var/www/etc/test.conf
(make sure this file is not served by apache)
The configuration must be in JSON format. I.e., something like
{
"CORS": "*",
"Content-Type": "text/xml",
"version": "1.1.0",
"TARGET_NAMESPACE": "http://ogr.maptools.org/"
}
The keys and structure of this file depend on the type of the service you are setting up. "CORS" is the only one that is recognized by this module. "CORS" is either a string denoting the allowed origin or a hash of "Allow-Origin", "Allow-Methods", "Allow-Headers", and "Max-Age".
EXPORT
None by default.
METHODS
new
This creates a new Geo::OGC::Service app. You need to call it in the psgi file as a class method with a named parameter hash reference. The parameters are
config, services
config is a path to a file or a reference to an anonymous hash containing the configuration.
services is a reference to a hash of service names associated with names of classes, which will process those service requests.
psgi_app
This method returns a psgi_app code reference for running this app.
respond
This subroutine is called for each request from the Internet. The call is responded during the execution of the subroutine. First, the configuration file is read and decoded. If the request is "OPTIONS" the call is responded with the following headers
Content-Type = text/plain
Access-Control-Allow-Origin = ""
Access-Control-Allow-Methods = "GET,POST"
Access-Control-Allow-Headers = "origin,x-requested-with,content-type"
Access-Control-Max-Age = 60*60*24
The values of Access-Control-* keys can be set in the configuration file. Above are the default ones.
In the default case this method constructs a new service object and calls its process_request method with PSGI style $responder object as a parameter.
This subroutine may fail due to an error in the configuration file, while interpreting the request, or while processing the request.
service
This subroutine does a preliminary interpretation of the request and converts it into a service object. The contents of the configuration is merged into the object.
The returned service object contains
config => a clone of the configuration for this service
env => many values from the PSGI environment
and may contain
posted => XML::LibXML DOM document element of the posted data
filter => XML::LibXML DOM document element of the filter
parameters => hash of rquest parameters obtained from Plack::Request
Note: all keys in request parameters are converted to lower case in parameters.
This subroutine may fail due to a request for an unknown service.
XMLWriter
A helper class for writing XML.
SYNOPSIS
my $writer = Geo::OGC::Service::XMLWriter::Caching->new();
$writer->open_element(
'wfs:WFS_Capabilities',
{ 'xmlns:gml' => "http://www.opengis.net/gml" });
$writer->element('ows:ServiceProvider',
[['ows:ProviderName'],
['ows:ProviderSite', {'xlink:type'=>"simple", 'xlink:href'=>""}],
['ows:ServiceContact']]);
$writer->close_element;
$writer->stream($responder);
or
use Capture::Tiny ':all';
my $writer = Geo::OGC::Service::XMLWriter::Streaming->new($responder);
for (@a_very_very_long_list) {
my $stdout = capture_stdout {
say something;
};
$writer->write($stdout);
}
DESCRIPTION
The classes Geo::OGC::Service::XMLWriter (abstract), Geo::OGC::Service::XMLWriter::Streaming (concrete), and Geo::OGC::Service::XMLWriter::Caching (concrete) are provided as a convenience for writing XML to the client.
The element method has the syntax
$writer->xml_element($tag[, $attributes][, $content])
$attributes is a reference to a hash
$content is a reference to a list of xml elements (tag...)
SEE ALSO
Discuss this module on the Geo-perl email list.
https://list.hut.fi/mailman/listinfo/geo-perl
For PSGI/Plack see
REPOSITORY
https://github.com/ajolma/Geo-OGC-Service
AUTHOR
Ari Jolma, <ari.jolma at gmail.com>
COPYRIGHT AND LICENSE
Copyright (C) 2015 by Ari Jolma
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.22.0 or, at your option, any later version of Perl 5 you may have available.