Snippets

Brian Medley Mojolicious example api

Created by Brian Medley
use Mojolicious::Lite;

get '/' => sub {
    my $c = shift;

    $c->render(text => "Try the API");
};

under (sub {
    my $self = shift;

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

    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 '/v1/example' => sub {
    my $c = shift;

    $c->render(json => {status => "success"});
};

app->start;

Comments (0)

HTTPS SSH

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