Firewall-Controller

 view release on metacpan or  search on metacpan

lib/Firewall/Controller/Ff.pm  view on Meta::CPAN

  };

  # 返回计算结果
  $self->render( json => $result );
}

#------------------------------------------------------------------------------
# initFirewall 初始化防火墙,必须携带 jsonStr
#------------------------------------------------------------------------------
sub initFirewall {
  my $self = shift;

  # 初始化变量
  my $result;

  # 实例化 Pg
  my $dbi = Firewall::DBI::Pg->new( $self->app->config->{db}{main} );

  # 尝试联机初始化防火墙
  try {
    # 检查 http 请求是否携带 jsonStr
    my $json  = $self->param('jsonStr');
    my $param = decode_json $json;

    if ( not defined $json ) {
      $result = {
        status  => 'error',
        type    => 'text',
        content => "缺少输入参数 "
      };
    }
    # 进行防火墙初始化
    else {
      my $initFW     = Firewall::Config::InitFirewall->new( dbi => $dbi );
      my $initResult = $initFW->initFirewall($param);
      # 写入数据结构
      $result = {
        status  => 'ok',
        type    => 'json',
        content => $initResult
      };
    }
  }
  # 捕捉异常信息
  catch {
    print $_->to_string;
    $result = {
      status  => 'error',
      type    => 'text',
      content => $_->to_string
    };
  };

  # 返回计算结果
  $self->render( json => $result );
}

#------------------------------------------------------------------------------
# updateNetwork 更新网络信息,必须携带 fwId
#------------------------------------------------------------------------------
sub updateNetwork {
  my $self = shift;

  # 初始化变量
  my $result;

  # 实例化 Pg 数据库对象
  my $dbi = Firewall::DBI::Pg->new( $self->app->config->{db}{main} );

  # 尝试联机更新防火墙
  try {
    # 检查 http 请求是否携带 fwId
    my $fwId = $self->param('fwId');

    if ( not defined $fwId ) {
      $result = {
        status  => 'error',
        type    => 'text',
        content => "缺少输入参数 "
      };
    }
    # 更新防火墙
    else {
      my $initFW     = Firewall::Config::InitFirewall->new( dbi => $dbi );
      my $initResult = $initFW->updateNetwork($fwId);
      # 填充数据结构
      $result = {
        status  => 'ok',
        type    => 'json',
        content => $initResult
      };
    }
  }
  # 捕捉异常信息
  catch {
    print $_->to_string;
    $result = {
      status  => 'error',
      type    => 'text',
      content => $_->to_string
    };
  };

  # 返回计算结果
  $self->render( json => $result );
}

1;



( run in 0.775 second using v1.01-cache-2.11-cpan-e1769b4cff6 )