Snippets

Brian Medley smallish example

Created by Brian Medley last modified
use Mojolicious::Lite;

use v5.20;
use experimental 'signatures';

get '/' => 'index';

under (sub {
    my $self = shift;

    unless ($self->req->json) {
        $self->render(json => {status => "error", data => { message => "No JSON found" }});

        return undef;
    }

    my $username = $self->req->json->{username};
    my $api_key = $self->req->json->{api_key};

    unless ($username) {
        $self->render(json => {status => "error", data => { message => "No username found" }});

        return undef;
    }

    unless ($api_key) {
        $self->render(json => {status => "error", data => { message => "No API Key found" }});

        return undef;
    }

    unless ("fnord" eq $username) {
        $self->render(json => {status => "error", data => { message => "Credentials mis-match" }});

        return undef;
    }

    unless ("68b329da9893e34099c7d8ad5cb9c940" eq $api_key) {
        $self->render(json => {status => "error", data => { message => "Credentials mis-match" }});

        return undef;
    }

    return 1;
});

post '/api/v1/hello' => sub ($c) {
    return($c->render(json => {status => "success"}));
};

put '/api/v1/hello' => sub ($c) {
    return($c->render(json => {status => "success is a put"}));
};

get '/api/v1/hello' => sub ($c) {
    return($c->render(json => {status => "we like get"}));
};

app->start;

__DATA__

@@ index.html.ep

Try:<br>

    POST /api/v1/hello

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.