Skip to content

Commit

Permalink
port to Mojolicious
Browse files Browse the repository at this point in the history
  • Loading branch information
uwe committed Apr 29, 2012
1 parent 130e65a commit 77b4260
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 551 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,4 +3,5 @@
config-local.pl
test
*.tgz
tmp

9 changes: 0 additions & 9 deletions TODO

This file was deleted.

28 changes: 0 additions & 28 deletions app.psgi

This file was deleted.

12 changes: 0 additions & 12 deletions bin/cronjob-ec2.sh

This file was deleted.

10 changes: 0 additions & 10 deletions bin/cronjob-mail-ec2.sh

This file was deleted.

8 changes: 8 additions & 0 deletions bin/cronjob-mail.sh
@@ -0,0 +1,8 @@
#!/bin/bash

cd /home/uwe/mittag
rm -f mail/*.txt
/opt/perl/bin/perl bin/mail-download.pl
#tar -czf backup/mail-`date +%Y-%m-%d-%H-%M`.tgz mail
/opt/perl/bin/perl bin/mail-extract.pl

10 changes: 10 additions & 0 deletions bin/cronjob.sh
@@ -0,0 +1,10 @@
#!/bin/bash

export LANG='de_DE.utf8'

cd /home/uwe/mittag
rm -f data/*
/opt/perl/bin/perl bin/download.pl
#tar -czf backup/`date +%Y-%m-%d-%H-%M`.tgz data
/opt/perl/bin/perl bin/extract.pl

39 changes: 39 additions & 0 deletions lib/Mittag/Web.pm
@@ -0,0 +1,39 @@
package Mittag::Web;

use Mojo::Base 'Mojolicious';

use Mittag::DB::Schema;


has schema => sub {
my $config = {
db_name => 'mittag',
db_user => 'root',
db_pass => 'root',
};
Mittag::DB::Schema->connect_with_config($config);
};


sub rs {
return (shift)->schema->resultset('Mittag::DB::Schema::' . shift);
}

sub startup {
my ($self) = @_;

$self->plugin('tt_renderer');

my $r = $self->routes;

$r->route('/' )->to('day#today');
$r->route('/day' )->to('day#today');
$r->route('/day/:date')->to('day#date')->name('day');
$r->route('/day/today')->to('day#date')->name('today');

# compatibility with old mobile URLs
$r->route('/day/:date/1')->to('day#date');
}


1;
102 changes: 9 additions & 93 deletions lib/Mittag/WebNano/Controller.pm → lib/Mittag/Web/Day.pm
@@ -1,52 +1,19 @@
package Mittag::WebNano::Controller;
package Mittag::Web::Day;

use strict;
use warnings;

use base qw/WebNano::Controller/;
use Mojo::Base 'Mojolicious::Controller';

use DateTime;
use JSON::XS;

use Mittag::Places;


sub redirect {
my ($self, $url) = @_;

my $res = $self->req->new_response;
$res->redirect($url);
return $res;
}

sub index_action {
my ($self) = @_;

return $self->app->render('index.html');
}

sub place_action {
my ($self, $place_id) = @_;

my $place = Mittag::Places->place_by_id($place_id);

return $self->redirect('/places') unless $place;

return $self->app->render('place.html', {place => $place});
sub today {
return (shift)->redirect_to('today');
}

sub places_action {
sub date {
my ($self) = @_;

return $self->app->render('places.html');
}

sub day_action {
my ($self, $input_date, $mobile) = @_;

my $date = eval {
$input_date ||= '';
my @date = split /-/, $input_date;
my @date = split /-/, $self->param('date') || '';
DateTime->new(
year => $date[0],
month => $date[1],
Expand All @@ -61,11 +28,7 @@ sub day_action {
# go back if no data
$today = $self->_prev_date(DateTime->today) unless $today;
}

my $url = '/day/' . $today->ymd('-');
$url .= '/1' if $mobile;

return $self->redirect($url);
$date = $today;
}

my @offers = $self->_get_offers($date);
Expand All @@ -75,64 +38,17 @@ sub day_action {
# if there is no future date, we try backwards
$next_date ||= $self->_prev_date($date, 1);

my $res = $self->req->new_response;
$res->redirect('/day/' . $next_date->ymd('-'));
return $res;
return $self->redirect_to(day => date => $next_date->ymd('-'));
}

my $vars = {
$self->stash(
OFFERS => \@offers,
date => $date,
prev_date => $self->_prev_date($date->clone->subtract(days => 1)) || undef,
next_date => $self->_next_date($date->clone->add( days => 1)) || undef,
};

my $template = 'day.html';
if ($mobile) {
$vars->{mobile} = 1;
$template = 'mobile.html';
}

return $self->app->render($template, $vars);
}

sub rest_action {
my ($self, $input_date) = @_;

my @date = split /-/, $input_date;
my $date = DateTime->new(
year => $date[0],
month => $date[1],
day => $date[2],
);

# only allow narrow date range
my $today = DateTime->today;
my $delta = $today->delta_days($date)->days;
if ($delta > 3) {
return encode_json {};
}

my @offers = map {
{
place => $_->place->name,
meal => $_->name,
price => $_->price,
}
} $self->_get_offers($date);

my $prev_date = $self->_prev_date($date->clone->subtract(days => 1));
my $next_date = $self->_next_date($date->clone->add( days => 1));

return encode_json {
offers => \@offers,
prev_date => $prev_date->ymd('-'),
next_date => $next_date->ymd('-'),
};
}

# --------------------------------------------------------------------------------

# get daily and weekly offers (sorted by place and price)
sub _get_offers {
my ($self, $date) = @_;
Expand Down
68 changes: 0 additions & 68 deletions lib/Mittag/WebNano.pm

This file was deleted.

2 changes: 2 additions & 0 deletions log/.gitignore
@@ -0,0 +1,2 @@
*.log

5 changes: 3 additions & 2 deletions modules.txt
Expand Up @@ -8,7 +8,8 @@ File::Slurp
HTML::TableExtract
IPC::Run3
Module::Find
Plack
Template
Template::Plugin::Komma
WebNano
Mojolicious
Mail::POP3Client
Email::MIME
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions script/mittag_web
@@ -0,0 +1,21 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use File::Basename 'dirname';
use File::Spec;

use lib join '/', File::Spec->splitdir(dirname(__FILE__)), 'lib';
use lib join '/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib';

# Check if Mojolicious is installed;
die <<EOF unless eval 'use Mojolicious::Commands; 1';
It looks like you don't have the Mojolicious framework installed.
Please visit http://mojolicio.us for detailed installation instructions.
EOF

# Application
$ENV{MOJO_APP} ||= 'Mittag::Web';

# Start commands
Mojolicious::Commands->start;

0 comments on commit 77b4260

Please sign in to comment.