MToken

 view release on metacpan or  search on metacpan

lib/MToken.pm  view on Meta::CPAN

        }
    }

    # Get size
    my $size = $self->option("size") ||
        int(rand(MToken::Const::KEYMAXSIZE - MToken::Const::KEYMINSIZE)) + MToken::Const::KEYMINSIZE;

    my %exest = $self->execmd($self->get_opensslbin, "rand", "-out", $file_out, $size);
    unless ($exest{status} && -e $file_out) {
        $self->error(sprintf("Can't generate rand key file %s", $file_out));
        return 0;
    }
    say cyan($exest{output}) if $exest{output};

    # Ok
    return yep("File %s successfully generated", $file_out);
});

__PACKAGE__->register_handler(
    handler     => "server",
    description => "MToken HTTP server",
    code => sub {
### CODE:
    my ($self, $meta, @arguments) = @_;

    # Dash k
    my $dash_k = shift(@arguments) || "status";
    unless (grep {$_ eq $dash_k} qw/start status stop restart reload/) {
        $self->error("Incorrect LSB command! Please use start, status, stop, restart or reload");
        return 0;
    }

    # Get permisions by names
    my $uid = $>; # Effect. UID
    my $gid = $); # Effect. GID
    if (IS_ROOT) {
        $uid = getpwnam(USERNAME) || do {
            $self->error("getpwnam failed - $!");
            return 0;
        };
        $gid = getgrnam(GROUPNAME) || do {
            $self->error("getgrnam failed - $!\n");
            return 0;
        };
    }

    # Prepare DataDir if not specified
    unless ($self->option("datadir")) {
        if (IS_ROOT) { # /var/lib/mtoken
            $self->datadir(File::Spec->catdir(sharedstatedir(), PROJECTNAMEL));
        } else { #~/.local/share/mtoken/www
            $self->datadir(File::Spec->catdir(File::HomeDir->my_data(), PROJECTNAMEL, "www"));
        }
        unless (-e $self->datadir) {
            unless (preparedir($self->datadir)) {
                $self->error(sprintf("Can't prepare directory %s", $self->datadir));
                return 0;
            }
        }
        # Set permisions (GID and UID) for work directory
        chown($uid, $gid, $self->datadir) if IS_ROOT && File::stat::stat($self->datadir)->uid != $uid;
    }

    # Prepare tempdir
    $self->tempdir(File::Spec->catdir(File::Spec->tmpdir(), PROJECTNAMEL));
    unless (preparedir( $self->tempdir, 0777 )) {
        $self->error(sprintf("Can't prepare temp directory: %s", $self->tempdir));
        return 0;
    }
    chown($uid, $gid, $self->tempdir) if IS_ROOT && File::stat::stat($self->tempdir)->uid != $uid;
    $self->debug(sprintf("Temp dir: %s", $self->tempdir));

    # Prepare log directory
    if (IS_ROOT) {
        my $logdir = $self->logdir;
        unless (preparedir( $logdir, 0777 )) {
            $self->error(sprintf("Can't prepare log directory: %s", $logdir));
            return 0;
        }
        # Set permisions (GID and UID) for log directory
        chown($uid, $gid, $logdir) if File::stat::stat($logdir)->uid != $uid;
        $self->debug(sprintf("Log dir: %s", $self->logdir));
    } else {
        $self->logfile(File::Spec->catfile($self->tempdir(), sprintf("%s.log", PROJECTNAMEL)));
        $self->debug(sprintf("Log file: %s", $self->logfile));
    }

    # Prepare pid directory and file
    my $piddir = IS_ROOT ? File::Spec->catdir( rundir(), PROJECTNAMEL) : $self->tempdir();
    my $pidfile = File::Spec->catfile($piddir, sprintf("%s.pid", PROJECTNAMEL));
    unless (preparedir($piddir)) {
        $self->error(sprintf("Can't prepare pid directory: %s", $piddir));
        return 0;
    }
    # Set permisions (GID and UID) for pid directory
    chown($uid, $gid, $piddir) if IS_ROOT && File::stat::stat($piddir)->uid != $uid;
    $self->debug(sprintf("Pid file: %s", $pidfile));

    # Hypnotoad variables
    my $upgrade = 0;
    my $reload = 0;
    my $upgrade_timeout = UPGRADE_TIMEOUT;

    # Mojolicious Application
    my $app = MToken::Server->new(ctk => $self);
       $app->attr(ctk => sub { $self }); # has ctk => sub { CTKx->instance->ctk };
    my $prefork = Mojo::Server::Prefork->new( app => $app ); # app => $self
       $prefork->pid_file($pidfile);

    # Hypnotoad Pre-fork settings
    $prefork->max_clients(tv2int(value($self->conf("clients")))) if defined $self->conf("clients");
    $prefork->max_requests(tv2int(value($self->conf("requests")))) if defined $self->conf("requests");
    $prefork->accepts(tv2int(value($self->conf("accepts")))) if defined $self->conf("accepts");
    $prefork->spare(tv2int(value($self->conf("spare")))) if defined $self->conf("spare");
    $prefork->workers(tv2int(value($self->conf("workers")))) if defined $self->conf("workers");

    # Make Listen
    my $cfg_listen = value($self->conf("listen"));
    my $tls_on = isTrueFlag(value($self->conf("tls")));
    my $listen = $tls_on ? "https://" : "http://";
    if ($cfg_listen) {
        $listen .= $cfg_listen;
    } else {
        $listen .= sprintf("%s:%d",
                value($self->conf("listenaddr")) || SERVER_LISTEN_ADDR,
                tv2int16(value($self->conf("listenport"))) || SERVER_LISTEN_PORT,
            );
    }
    my $_resolve_cf = sub {
        my $f = shift;
        return $f if File::Spec->file_name_is_absolute($f);
        return File::Spec->catfile($self->root, $f);
    };
    if ($tls_on) {
        my @p = ();
        foreach my $k (qw/ciphers version/) {
            my $v = value($self->conf("tls_$k")) // '';
            next unless length $v;
            push @p, sprintf("%s=%s", $k, $v);
        }
        foreach my $k (qw/ca cert key/) {
            my $v = value($self->conf("tls_$k")) // '';
            next unless length $v;
            push @p, sprintf("%s=%s", $k, $_resolve_cf->($v));
        }
        push @p, sprintf("%s=%s", "verify", value($self->conf("tls_verify")) || '0x00')
            if value($self->conf("tls_verify"));
        $listen .= sprintf("?%s", join('&', @p));
    }
    $prefork->listen([$listen]);

    # Working with Dash k
    if ($dash_k eq 'start') {
        if (my $pid = $prefork->check_pid()) {
            say "Already running $pid";
            return 1;



( run in 0.682 second using v1.01-cache-2.11-cpan-71847e10f99 )