1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Display `nitrpg` data as a website.
23 # A custom action forn `nitrpg`.
27 # Root URL is used as a prefix for all URL generated by the actions.
30 # Github oauth token used for GithubAPI.
31 var auth
: String is lazy
do return get_github_oauth
33 # API client used to import data from Github.
34 var api
: GithubAPI is lazy
do
35 var api
= new GithubAPI(auth
)
42 print
"Error: Invalid Github oauth token!"
47 # Return an Error reponse page.
48 fun bad_request
(msg
: String): HttpResponse do
49 var rsp
= new HttpResponse(400)
50 var page
= new NitRpgPage(root_url
)
51 var error
= new ErrorPanel(msg
)
52 page
.flow_panels
.add error
53 rsp
.body
= page
.write_to_string
58 # An action that require a game.
63 var page
: NitRpgPage is noinit
66 var game
: Game is noinit
68 redef fun answer
(request
, url
) is abstract
70 # Check errors and prepare response.
71 private fun prepare_response
(request
: HttpRequest, url
: String): HttpResponse do
72 var owner
= request
.param
("owner")
73 var repo_name
= request
.param
("repo")
74 if owner
== null or repo_name
== null then
75 var msg
= "Bad request: should look like /repos/:owner/:repo."
76 return bad_request
(msg
)
78 var repo
= new Repo(api
, "{owner}/{repo_name}")
79 game
= new Game(api
, repo
)
80 game
.root_url
= root_url
82 var msg
= api
.last_error
.message
83 return bad_request
("Repo Error: {msg}")
85 var response
= new HttpResponse(200)
86 page
= new NitRpgPage(root_url
)
87 page
.side_panels
.add
new GameStatusPanel(game
)
88 page
.breadcrumbs
= new Breadcrumbs
89 page
.breadcrumbs
.add_link
(game
.url
, game
.name
)
90 prepare_pagination
(request
)
94 # Parse pagination related parameters.
95 private fun prepare_pagination
(request
: HttpRequest) do
96 var args
= request
.get_args
97 list_from
= args
.get_or_default
("pfrom", "0").to_i
98 list_limit
= args
.get_or_default
("plimit", "10").to_i
101 # Limit of events to display in lists.
104 # From where to start the display of events related lists.
108 # Repo overview page.
112 redef fun answer
(request
, url
) do
113 var rsp
= prepare_response
(request
, url
)
114 page
.side_panels
.add
new ShortListPlayersPanel(game
)
115 page
.flow_panels
.add
new PodiumPanel(game
)
116 page
.flow_panels
.add
new EventListPanel(game
, list_limit
, list_from
)
117 rsp
.body
= page
.write_to_string
126 redef fun answer
(request
, url
) do
127 var rsp
= prepare_response
(request
, url
)
128 page
.breadcrumbs
.add_link
(game
.url
/ "players", "players")
129 page
.flow_panels
.add
new ListPlayersPanel(game
)
130 rsp
.body
= page
.write_to_string
135 # Player details page.
139 redef fun answer
(request
, url
) do
140 var rsp
= prepare_response
(request
, url
)
141 var name
= request
.param
("player")
143 var msg
= "Bad request: should look like /:owner/:repo/:players/:name."
144 return bad_request
(msg
)
146 var player
= game
.load_player
(name
)
147 if player
== null then
148 return bad_request
("Request Error: unknown player {name}.")
150 page
.breadcrumbs
.add_link
(game
.url
/ "players", "players")
151 page
.breadcrumbs
.add_link
(player
.url
, name
)
152 page
.side_panels
.clear
153 page
.side_panels
.add
new PlayerStatusPanel(game
, player
)
154 page
.flow_panels
.add
new PlayerReviewsPanel(game
, player
)
155 page
.flow_panels
.add
new EventListPanel(player
, list_limit
, list_from
)
156 rsp
.body
= page
.write_to_string
161 if args
.length
!= 3 then
162 print
"Error: missing argument"
165 print
"web <host> <port> <root_url>"
173 var iface
= "{host}:{port}"
174 var vh
= new VirtualHost(iface
)
175 vh
.routes
.add
new Route("/styles/", new FileServer("www/styles"))
176 vh
.routes
.add
new Route("/games/:owner/:repo/players/:player", new PlayerHome(root
))
177 vh
.routes
.add
new Route("/games/:owner/:repo/players", new ListPlayers(root
))
178 vh
.routes
.add
new Route("/games/:owner/:repo", new RepoHome(root
))
180 var fac
= new HttpFactory.and_libevent
181 fac
.config
.virtual_hosts
.add vh
183 print
"Launching server on http://{iface}/"