parser: add `ASTDump` and `ANode::dump_tree`
[nit.git] / contrib / nitrpg / src / web.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Display `nitrpg` data as a website.
18 module web
19
20 import nitcorn
21 import templates
22
23 # A custom action forn `nitrpg`.
24 class RpgAction
25 super Action
26
27 # Root URL is used as a prefix for all URL generated by the actions.
28 var root_url: String
29
30 # Github oauth token used for GithubAPI.
31 var auth: String is lazy do return get_github_oauth
32
33 # API client used to import data from Github.
34 var api: GithubAPI is lazy do
35 var api = new GithubAPI(auth)
36 return api
37 end
38
39 init do
40 super
41 if auth.is_empty then
42 print "Error: Invalid Github oauth token!"
43 exit 1
44 end
45 end
46
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
54 return rsp
55 end
56 end
57
58 # An action that require a game.
59 class GameAction
60 super RpgAction
61
62 # Response page stub.
63 var page: NitRpgPage is noinit
64
65 # Target game.
66 var game: Game is noinit
67
68 redef fun answer(request, url) is abstract
69
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)
77 end
78 var repo = new Repo(api, "{owner}/{repo_name}")
79 game = new Game(api, repo)
80 game.root_url = root_url
81 if api.was_error then
82 var msg = api.last_error.message
83 return bad_request("Repo Error: {msg}")
84 end
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 return response
91 end
92 end
93
94 # Repo overview page.
95 class RepoHome
96 super GameAction
97
98 redef fun answer(request, url) do
99 var rsp = prepare_response(request, url)
100 page.side_panels.add new ShortListPlayersPanel(game)
101 page.flow_panels.add new PodiumPanel(game)
102 rsp.body = page.write_to_string
103 return rsp
104 end
105 end
106
107 # Repo players list.
108 class ListPlayers
109 super GameAction
110
111 redef fun answer(request, url) do
112 var rsp = prepare_response(request, url)
113 page.breadcrumbs.add_link(game.url / "players", "players")
114 page.flow_panels.add new ListPlayersPanel(game)
115 rsp.body = page.write_to_string
116 return rsp
117 end
118 end
119
120 # Player details page.
121 class PlayerHome
122 super GameAction
123
124 redef fun answer(request, url) do
125 var rsp = prepare_response(request, url)
126 var name = request.param("player")
127 if name == null then
128 var msg = "Bad request: should look like /:owner/:repo/:players/:name."
129 return bad_request(msg)
130 end
131 var player = game.load_player(name)
132 if player == null then
133 return bad_request("Request Error: unknown player {name}.")
134 end
135 page.breadcrumbs.add_link(game.url / "players", "players")
136 page.breadcrumbs.add_link(player.url, name)
137 page.side_panels.clear
138 page.side_panels.add new PlayerStatusPanel(game, player)
139 page.flow_panels.add new PlayerReviewsPanel(game, player)
140 rsp.body = page.write_to_string
141 return rsp
142 end
143 end
144
145 if args.length != 3 then
146 print "Error: missing argument"
147 print ""
148 print "Usage:"
149 print "web <host> <port> <root_url>"
150 exit 1
151 end
152
153 var host = args[0]
154 var port = args[1]
155 var root = args[2]
156
157 var iface = "{host}:{port}"
158 var vh = new VirtualHost(iface)
159 vh.routes.add new Route("/styles/", new FileServer("www/styles"))
160 vh.routes.add new Route("/games/:owner/:repo/players/:player", new PlayerHome(root))
161 vh.routes.add new Route("/games/:owner/:repo/players", new ListPlayers(root))
162 vh.routes.add new Route("/games/:owner/:repo", new RepoHome(root))
163
164 var fac = new HttpFactory.and_libevent
165 fac.config.virtual_hosts.add vh
166
167 print "Launching server on http://{iface}/"
168 fac.run