| 1 | package XAS::Apps::Service::Testd; | ||
| 2 | |||
| 3 | 1 | ▶ | use Template; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use JSON::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Web::Machine; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Plack::Builder; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Plack::App::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Plack::App::URLMap; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use XAS::Service::Server; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use XAS::Class | ||
| 12 | version => '0.01', | ||
| 13 | base => 'XAS::Lib::App::Service', | ||
| 14 | mixin => 'XAS::Lib::Mixins::Configs', | ||
| 15 | filesystem => 'File', | ||
| 16 | accessors => 'cfg', | ||
| 17 | vars => { | ||
| 18 | SERVICE_NAME => 'XAS_TESTD', | ||
| 19 | SERVICE_DISPLAY_NAME => 'XAS test micro service', | ||
| 20 | SERVICE_DESCRIPTION => 'This process is a test micro service' | ||
| 21 | } | ||
| 22 | ; | ||
| 23 | |||
| 24 | # ---------------------------------------------------------------------- | ||
| 25 | # Public Methods | ||
| 26 | # ---------------------------------------------------------------------- | ||
| 27 | |||
| 28 | sub build_app { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | # define base, name and description | ||
| 32 | |||
| 33 | my $base = $self->cfg->val('app', 'base', '/home/kevin/dev/XAS-Service/trunk/web'); | ||
| 34 | my $name = $self->cfg->val('app', 'name', 'WEB Services'); | ||
| 35 | my $description = $self->cfg->val('app', 'description', 'Test api using RESTFUL HAL-JSON'); | ||
| 36 | |||
| 37 | # Template config | ||
| 38 | |||
| 39 | my $config = { | ||
| 40 | INCLUDE_PATH => File($base, 'root')->path, # or list ref | ||
| 41 | INTERPOLATE => 1, # expand "$var" in plain text | ||
| 42 | }; | ||
| 43 | |||
| 44 | # create various objects | ||
| 45 | |||
| 46 | my $template = Template->new($config); | ||
| 47 | my $json = JSON::XS->new->utf8(); | ||
| 48 | |||
| 49 | # allow variables with preceeding _ | ||
| 50 | |||
| 51 | $Template::Stash::PRIVATE = undef; | ||
| 52 | |||
| 53 | # handlers, using URLMap for routing | ||
| 54 | |||
| 55 | my $builder = Plack::Builder->new(); | ||
| 56 | my $urlmap = Plack::App::URLMap->new(); | ||
| 57 | |||
| 58 | $urlmap->mount('/' => Web::Machine->new( | ||
| 59 | resource => 'XAS::Service::Resource', | ||
| 60 | resource_args => [ | ||
| 61 | alias => 'root', | ||
| 62 | template => $template, | ||
| 63 | json => $json, | ||
| 64 | app_name => $name, | ||
| 65 | app_description => $description | ||
| 66 | ] ) | ||
| 67 | ); | ||
| 68 | |||
| 69 | # static files | ||
| 70 | |||
| 71 | $urlmap->mount('/js' => Plack::App::File->new( | ||
| 72 | root => $base . '/root/js' ) | ||
| 73 | ); | ||
| 74 | |||
| 75 | $urlmap->mount('/css' => Plack::App::File->new( | ||
| 76 | root => $base . '/root/css') | ||
| 77 | ); | ||
| 78 | |||
| 79 | $urlmap->mount('/yaml' => Plack::App::File->new( | ||
| 80 | root => $base . '/root/yaml/yaml') | ||
| 81 | ); | ||
| 82 | |||
| 83 | return $builder->to_app($urlmap->to_app); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | sub setup { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | my $interface = XAS::Service::Server->new( | ||
| 91 | -alias => 'interface', | ||
| 92 | -port => $self->cfg->val('system', 'port', 9507), | ||
| 93 | -address => $self->cfg->val('system', 'address', 'localhost'), | ||
| 94 | -app => $self->build_app, | ||
| 95 | ); | ||
| 96 | |||
| 97 | $self->service->register('interface'); | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | sub main { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | $self->log->info_msg('startup'); | ||
| 105 | |||
| 106 | $self->setup(); | ||
| 107 | $self->service->run(); | ||
| 108 | |||
| 109 | $self->log->info_msg('shutdown'); | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | # ---------------------------------------------------------------------- | ||
| 114 | # Private Methods | ||
| 115 | # ---------------------------------------------------------------------- | ||
| 116 | |||
| 117 | sub init { | ||
| 118 | my $class = shift; | ||
| 119 | |||
| 120 | my $self = $class->SUPER::init(@_); | ||
| 121 | |||
| 122 | $self->load_config(); | ||
| 123 | |||
| 124 | return $self; | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | __END__ | ||