Muster.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Muster/LeafFile/gif.pm ↑ Summary Muster/Assemble.pm »
1 package Muster;
2 $Muster::VERSION = '0.96';
3 # ABSTRACT: web application for content management
4 =head1 NAME
5  
6 Muster - web application for content management
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 SYNOPSIS
13  
14 use Muster;
15  
16 =head1 DESCRIPTION
17  
18 Content management system; muster your pages.
19 This uses
20 Mojolicious
21 Mojolicious::Plugin::Foil
22  
23 =cut
24  
25 2 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 0 ▶use Muster::Assemble;
Subroutine BEGIN: called CC=1 SCAR=2.6
27 use Path::Tiny;
28 use File::ShareDir;
29  
30 # This method will run once at server start
31 sub startup {
32 my $self = shift;
33  
34 # -------------------------------------------
35 # Configuration
36 # check:
37 # * current working directory
38 # * relative to the calling program
39 # -------------------------------------------
40 my $the_prog = path($0)->absolute;
41 my $conf_basename = "muster.conf";
42 my $conf_file = path(Path::Tiny->cwd, $conf_basename);
43 if (! -f $conf_file)
44 {
45 $conf_file = path($the_prog->parent->stringify, $conf_basename);
46 if (! -f $conf_file)
47 {
48 $conf_file = path($the_prog->parent->parent->stringify, $conf_basename);
49 }
50 }
51 # the MUSTER_CONFIG environment variable overrides the default
52 if (defined $ENV{MUSTER_CONFIG} and -f $ENV{MUSTER_CONFIG})
53 {
54 $conf_file = $ENV{MUSTER_CONFIG};
55 }
56 print STDERR "Muster::VERSION=$Muster::VERSION CONFIG: $conf_file\n";
57 my $mojo_config = $self->plugin('Config' => { file => $conf_file });
58  
59 # -------------------------------------------
60 # New commands in Muster::Command namespace
61 # -------------------------------------------
62 push @{$self->commands->namespaces}, 'Muster::Command';
63  
64 # -------------------------------------------
65 # Append public directories
66 # Find the Muster "public" directory
67 # It could be relative to the CWD
68 # It could be relative to the calling program
69 # It could be in a FileShared location.
70 my $pubdir = path(Path::Tiny->cwd, "public");
71 if (!-d $pubdir)
72 {
73 $pubdir = path($the_prog->parent->stringify, "public");
74 if (!-d $pubdir)
75 {
76 # use File::ShareDir with the distribution name
77 my $dist = __PACKAGE__;
78 $dist =~ s/::/-/g;
79 my $dist_dir = path(File::ShareDir::dist_dir($dist));
80 $pubdir = $dist_dir;
81 }
82 }
83 if (-d $pubdir)
84 {
85 # this takes priority over the default public dir
86 unshift @{$self->static->paths}, $pubdir;
87 }
88 # -------------------------------------------
89 # Cache
90 # -------------------------------------------
91 if (!$mojo_config->{cache_dir})
92 {
93 $mojo_config->{cache_dir} = path(Path::Tiny->cwd, "cache")->stringify;
94 }
95 my $cache_dir = path($mojo_config->{cache_dir})->absolute;
96 # create the cache dir if it's not there
97 if (!-d $cache_dir)
98 {
99 if (-d -w $cache_dir->parent->stringify)
100 {
101 mkdir $cache_dir;
102 }
103 }
104 if (!-d -w $cache_dir)
105 {
106 die "cache dir '$cache_dir' not writable";
107 }
108 push @{$self->static->paths}, $cache_dir->stringify;
109  
110 # -------------------------------------------
111 # Pages
112 # -------------------------------------------
113 $self->plugin('Muster::PagesHelper');
114
115 $self->plugin('Foil');
116 #$self->plugin(NYTProf => $mojo_config);
117  
118 # -------------------------------------------
119 # Templates
120 # -------------------------------------------
121 push @{$self->renderer->classes}, __PACKAGE__;
122  
123 # -------------------------------------------
124 # secrets, cookies and defaults
125 # -------------------------------------------
126 $self->secrets([qw(aft3CoidIttenImtuj)]);
127 $self->sessions->cookie_name('muster');
128 $self->sessions->default_expiration(60 * 60 * 24 * 3); # 3 days
129 foreach my $key (keys %{$self->config->{defaults}})
130 {
131 $self->defaults($key, $self->config->{defaults}->{$key});
132 }
133  
134 # -------------------------------------------
135 # Rendering
136 # -------------------------------------------
137 $self->{assemble} = Muster::Assemble->new();
138  
139 my $do_pagelist = sub {
140 my $c = shift;
141 $c->render(template=>'pagelist');
142 };
143 my $do_page = sub {
144 my $c = shift;
145 $self->{assemble}->serve_page($c);
146 };
147 my $do_meta = sub {
148 my $c = shift;
149 $self->{assemble}->serve_meta($c);
150 };
151 my $do_debug = sub {
152 my $c = shift;
153  
154 my $pagename = $c->param('cpath');
155 $c->reply->exception("Debug" . (defined $pagename ? " $pagename" : ''));
156 };
157 my $r = $self->routes;
158  
159 $r->get('/' => $do_page);
160 $r->get('/pagelist' => $do_pagelist);
161 $r->get('/_debug' => $do_debug);
162 $r->get('/_debug/*cpath' => $do_debug);
163 $r->get('/_meta/*cpath' => $do_meta);
164 # anything else should be a page or file
165 $r->get('/*cpath' => $do_page);
166 }
167  
168 1; # end of Muster
169  
170 # Here come the TEMPLATES!
171  
172 __DATA__
« Muster/LeafFile/gif.pm ↑ Summary Muster/Assemble.pm »