Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /root_dir/lib/Mojo_App.pm
- package Mojo_App;
- use Mojo::Base 'Mojolicious';
- # This method will run once at server start
- sub startup {
- my $self = shift;
- # Load configuration from hash returned by "my_app.conf"
- my $config = $self->plugin('Config');
- # Router
- my $r = $self->routes;
- # Normal route to controller
- $r->get('/')->to('main#index');
- #db conf
- my $db_conf= {
- db1 => {
- host => 'dbi:Sybase:server=ip01;database=db01',
- user => 'user01',
- pass => 'pass01'
- },
- db2 => {
- host => 'dbi:Sybase:server=ip02;database=db02',
- user => 'user02',
- pass => 'pass02'
- },
- db3 => {
- host => 'dbi:Sybase:server=ip03;database=db03',
- user => 'user03',
- pass => 'pass03'
- }
- };
- #db helper
- my $db;
- $self->helper(db => sub {
- use DBIx::Connector;
- my $self = shift;
- return 0 unless $self;
- if (exists($db_conf->{$self})) {
- my $db = DBIx::Connector->new($db_conf->{$self}->{host}, $db_conf->{$self}->{user}, $db_conf->{$self}->{pass}, {
- RaiseError => 1,
- AutoCommit => 1
- });
- return $db;
- } else {
- return "Connection not in config. \n" ;
- }
- });
- }
- 1;
- /root_dir/lib/Controller/Main.pm
- sub index {
- my $self = shift;
- my $dbh = $self->db->('db1')->dbh();
- my $test = $dbh->selectall_arrayref('select * from test1', { Slice => {} });
- $self->render();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement