summaryrefslogtreecommitdiff
path: root/lib/StarGit.pm
blob: 7bf301210eb5fa47ec37c3db5dbebd5bc428cdcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package StarGit;
use Dancer ':syntax';

use StarGit::Graph;
use StarGit::Info;
use Redis;

our $VERSION = '0.1';

set serializer => 'JSON';

get '/' => sub {
    template 'index';
};

get '/about' => sub {
    template 'about';
};

get '/api' => sub {
    template 'api';
};

get '/graph/local/:name' => sub {
    my $name = params->{'name'};

    my $redis_conf = setting('redis');
    my $redis = Redis->new( server => $redis_conf->{server} );
    $redis->auth( $redis_conf->{auth} ) if $redis_conf->{auth};

    if (my $cached_graph = $redis->get($name)){
        debug("cache hit for $name");
        return $cached_graph;
    }

    my $graph =
      StarGit::Graph->new( name => $name, mongodb_auth => setting('mongodb') );

    return send_error( "user " . $name . " doesn't exists", 404 )
      unless $graph->exists($name);

    $graph->neighbors( $name, 1 );
    $graph->remove_leaves();

    my $serialized_graph = to_json(_finalize($graph));
    $redis->set($name, $serialized_graph);
    return $serialized_graph;
};

get '/graph/attributes' => sub {
    my $graph_settings = setting('graph');
    my $attributes     = $graph_settings->{attributes};
    return { attributes => $attributes };
};

get '/profile/:login' => sub {
    my $login = params->{login};

    my $info =
      StarGit::Info->new( login => $login, mongodb_auth => setting('mongodb') )
      ->get();

    if ( !defined $info ) {
        return send_error( "no information for profile " . $login );
    }

    return $info;
};

sub _finalize {
    my $graph = shift;

    my @nodes = values %{ $graph->nodes };
    my @edges = values %{ $graph->edges };

    return { nodes => \@nodes, edges => \@edges, };
}

true;