1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Base classes used by `nitweb`.
19 import popcorn
::pop_config
20 import popcorn
::pop_repos
21 import popcorn
::pop_json
23 import commands
::commands_http
25 import templates
::json_commands
26 import templates
::html_commands
32 redef fun default_db_name
do return "nitweb"
37 # MModule used to flatten model.
38 var mainmodule
: MModule
40 # Modelbuilder used to access sources.
41 var modelbuilder
: ModelBuilder
43 # The JSON API does not filter anything by default.
44 var filter
: nullable ModelFilter
46 # Catalog to pass to handlers.
50 # Specific handler for the nitweb API.
51 abstract class APIHandler
55 var config
: NitwebConfig
57 # Find the MEntity ` with `full_name`.
58 fun find_mentity
(full_name
: nullable String): nullable MEntity do
59 if full_name
== null then return null
60 var mentity
= config
.model
.mentity_by_full_name
(full_name
.from_percent_encoding
, config
.filter
)
61 if mentity
== null then return null
63 var filter
= config
.filter
64 if filter
== null or filter
.accept_mentity
(mentity
) then return mentity
68 # Try to load the mentity from uri with `/:id`.
70 # Send 400 if `:id` is null.
71 # Send 404 if no entity is found.
72 # Return null in both cases.
73 fun mentity_from_uri
(req
: HttpRequest, res
: HttpResponse): nullable MEntity do
74 var id
= req
.param
("id")
76 res
.api_error
(400, "Expected mentity full name")
79 var mentity
= find_mentity
(id
)
80 if mentity
== null then
81 res
.api_error
(404, "MEntity `{id}` not found")
86 # Paginate a json array
88 # Returns only a subset of `results` depending on the current `page` and the
89 # number of elements to return set by `limit`.
91 # Transforms the json array into an object:
101 fun paginate
(results
: JsonArray, count
: Int, page
, limit
: nullable Int): JsonObject do
102 if page
== null or page
<= 0 then page
= 1
103 if limit
== null or limit
<= 0 then limit
= 20
105 var max
= count
/ limit
109 else if page
> max
then
113 var lstart
= (page
- 1) * limit
115 if lstart
+ lend
> count
then lend
= count
- lstart
117 var res
= new JsonObject
120 res
["results"] = new JsonArray.from
(results
.subarray
(lstart
, lend
))
127 # A Rooter dedicated to APIHandlers.
132 var config
: NitwebConfig
135 redef class HttpResponse
137 # Return an HTTP error response with `status`
139 # Like the rest of the API, errors are formated as JSON:
141 # { "status": 404, "message": "Not found" }
143 fun api_error
(status
: Int, message
: String) do
144 json
(new APIError(status
, message
), status
)
147 # Return `serializable` as a json string
149 # Uses `req` to define serialization options.
150 fun api_json
(req
: HttpRequest, serializable
: nullable Serializable, status
: nullable Int, plain
, pretty
: nullable Bool) do
151 json
(serializable
, status
, plain
, req
.bool_arg
("pretty"))
155 # An error returned by the API.
157 # Can be serialized to json.
164 # Response error message
170 # URL to `self` within the web interface.
171 fun web_url
: String do return "/doc/" / full_name
173 # URL to `self` within the JSON api.
174 fun api_url
: String do return "/api/entity/" / full_name
176 redef fun html_url
do return web_url
178 redef fun core_serialize_to
(v
) do
180 v
.serialize_attribute
("web_url", web_url
)
181 v
.serialize_attribute
("api_url", api_url
)
185 redef class MEntityRef
186 redef fun core_serialize_to
(v
) do
188 v
.serialize_attribute
("web_url", mentity
.web_url
)
189 v
.serialize_attribute
("api_url", mentity
.api_url
)
193 redef class MClassDef
194 redef fun web_url
do return "{mclass.web_url}/lin#{full_name}"
198 redef fun web_url
do return "{mproperty.web_url}/lin#{full_name}"
202 redef fun core_serialize_to
(v
) do
204 v
.serialize_attribute
("web_url", web_url
)
205 v
.serialize_attribute
("api_url", api_url
)
209 redef class MClassType
210 redef var web_url
= mclass
.web_url
is lazy
211 redef var api_url
= mclass
.api_url
is lazy
214 redef class MNullableType
215 redef var web_url
= mtype
.web_url
is lazy
216 redef var api_url
= mtype
.api_url
is lazy
219 redef class MParameterType
220 redef var web_url
= mclass
.web_url
is lazy
221 redef var api_url
= mclass
.api_url
is lazy
224 redef class MVirtualType
225 redef var web_url
= mproperty
.web_url
is lazy
226 redef var api_url
= mproperty
.api_url
is lazy
229 redef class HtmlightVisitor
230 redef fun hrefto
(mentity
) do return mentity
.html_url
233 redef class CmdLicenseFile
234 redef var file_url
is lazy
do
235 var mentity
= self.mentity
236 if mentity
== null then return super
237 return "{mentity.web_url}/license"
241 redef class CmdContribFile
242 redef var file_url
is lazy
do
243 var mentity
= self.mentity
244 if mentity
== null then return super
245 return "{mentity.web_url}/contrib"