-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.raku
115 lines (82 loc) · 2.41 KB
/
app.raku
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cro::WebApp::Template;
use Hub;
use Misc;
use Cro::HTTP::Client;
use JSON::Tiny;
my $application = route {
my %conf = get-webui-conf();
my $theme;
if %conf<ui> && %conf<ui><theme> {
$theme = %conf<ui><theme>
} else {
$theme = "lumen";
};
say "ui theme: <$theme> ...";
get -> 'search', Str :$q {
my @results = plugin-search($q||"all");
template 'templates/search.crotmp', %(
results => @results,
plg-cnt => @results.elems,
q => $q,
theme => $theme
)
}
get -> 'plugin', Str $name, Str $version {
my %plg = plugin-deploy($name,$version);
#%plg<html-doc> = html-doc(%plg<doc-file>);
%plg<theme> = $theme;
template 'templates/plugin.crotmp', %plg;
}
get -> {
template 'templates/main.crotmp', %(
plg-cnt => plugins-cnt,
theme => $theme
)
}
get -> 'repo', *@path {
my $allow = False;
for request.headers {
$allow = True if .name eq "User-Agent" and .value ~~ /^^ 'curl' \S*/;
}
if $allow {
cache-control :public, :max-age(300);
static 'repo', @path;
} else {
#return bad-request();
warn "not allowed";
redirect :permanent, "/";
}
}
get -> 'js', *@path {
cache-control :public, :max-age(300);
static 'js', @path;
}
get -> 'login', {
redirect :permanent, "https://github.com/login/oauth/authorize?client_id={%*ENV<OAUTH_CLIENT_ID>}&state={%*ENV<OAUTH_STATE>}"
}
get -> 'oauth2', :$state, :$code {
my $resp = await Cro::HTTP::Client.get: 'https://github.com/login/oauth/access_token',
headers => [
"Accept" => "application/json"
],
query => {
redirect_uri => "https://sparrowhub.io/oauth2",
client_id => %*ENV<OAUTH_CLIENT_ID>,
client_secret => %*ENV<OAUTH_CLIENT_SECRET>,
code => $code,
state => $state,
};
my $data = await $resp.body-text();
my %data = from-json($data);
redirect :permanent, "/?authenitcated={%data<access_token>:exists}";
}
}
my Cro::Service $service = Cro::HTTP::Server.new:
:host<localhost>, :port<5000>, :$application;
$service.start;
react whenever signal(SIGINT) {
$service.stop;
exit;
}