├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── build.sbt
├── hystrix.png
├── project
├── build.properties
└── plugins.sbt
├── src
├── main
│ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── takezoe
│ │ │ └── resty
│ │ │ ├── Action.java
│ │ │ ├── Controller.java
│ │ │ └── Param.java
│ ├── resources
│ │ ├── logback.xml
│ │ └── public
│ │ │ └── vendors
│ │ │ └── swagger-ui
│ │ │ ├── css
│ │ │ ├── print.css
│ │ │ ├── reset.css
│ │ │ ├── screen.css
│ │ │ ├── style.css
│ │ │ └── typography.css
│ │ │ ├── fonts
│ │ │ ├── DroidSans-Bold.ttf
│ │ │ └── DroidSans.ttf
│ │ │ ├── images
│ │ │ ├── collapse.gif
│ │ │ ├── expand.gif
│ │ │ ├── explorer_icons.png
│ │ │ ├── favicon-16x16.png
│ │ │ ├── favicon-32x32.png
│ │ │ ├── favicon.ico
│ │ │ ├── logo_small.png
│ │ │ ├── pet_store_api.png
│ │ │ ├── throbber.gif
│ │ │ └── wordnik_api.png
│ │ │ ├── index.html
│ │ │ ├── lang
│ │ │ ├── ca.js
│ │ │ ├── el.js
│ │ │ ├── en.js
│ │ │ ├── es.js
│ │ │ ├── fr.js
│ │ │ ├── geo.js
│ │ │ ├── it.js
│ │ │ ├── ja.js
│ │ │ ├── ko-kr.js
│ │ │ ├── pl.js
│ │ │ ├── pt.js
│ │ │ ├── ru.js
│ │ │ ├── tr.js
│ │ │ ├── translator.js
│ │ │ └── zh-cn.js
│ │ │ ├── lib
│ │ │ ├── backbone-min.js
│ │ │ ├── es5-shim.js
│ │ │ ├── handlebars-4.0.5.js
│ │ │ ├── highlight.9.1.0.pack.js
│ │ │ ├── highlight.9.1.0.pack_extended.js
│ │ │ ├── jquery-1.8.0.min.js
│ │ │ ├── jquery.ba-bbq.min.js
│ │ │ ├── jquery.slideto.min.js
│ │ │ ├── jquery.wiggle.min.js
│ │ │ ├── js-yaml.min.js
│ │ │ ├── jsoneditor.min.js
│ │ │ ├── lodash.min.js
│ │ │ ├── marked.js
│ │ │ ├── object-assign-pollyfill.js
│ │ │ ├── sanitize-html.min.js
│ │ │ └── swagger-oauth.js
│ │ │ ├── o2c.html
│ │ │ ├── swagger-ui.js
│ │ │ └── swagger-ui.min.js
│ └── scala
│ │ └── com
│ │ └── github
│ │ └── takezoe
│ │ └── resty
│ │ ├── ActionResult.scala
│ │ ├── CORSSupport.scala
│ │ ├── HttpClientSupport.scala
│ │ ├── HystrixSupport.scala
│ │ ├── ParamInjector.scala
│ │ ├── Resty.scala
│ │ ├── RestyKernel.scala
│ │ ├── SwaggerController.scala
│ │ ├── model
│ │ ├── ActionDef.scala
│ │ ├── AppInfo.scala
│ │ ├── ControllerDef.scala
│ │ ├── ParamConverter.scala
│ │ └── ParamDef.scala
│ │ ├── servlet
│ │ ├── FileResourceServlet.scala
│ │ ├── InitializeListener.scala
│ │ ├── ResourceServlet.scala
│ │ ├── RestyServlet.scala
│ │ ├── SwaggerUIServlet.scala
│ │ ├── WebJarsServlet.scala
│ │ └── ZipkinBraveFilter.scala
│ │ └── util
│ │ ├── JsonUtils.scala
│ │ ├── ReflectionUtils.scala
│ │ ├── ScaladocUtils.scala
│ │ └── StringUtils.scala
└── test
│ └── scala
│ └── com
│ └── github
│ └── takezoe
│ └── resty
│ ├── RandomRequestTargetSpec.scala
│ └── util
│ └── ReflectionUtilsSpec.scala
└── swagger.png
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | *.log
3 | .ensime
4 | .ensime_cache
5 |
6 | # sbt specific
7 | dist/*
8 | target/
9 | lib_managed/
10 | src_managed/
11 | project/boot/
12 | project/plugins/project/
13 |
14 | # Scala-IDE specific
15 | .scala_dependencies
16 | .classpath
17 | .project
18 | .cache
19 | .settings
20 |
21 | # IntelliJ specific
22 | .idea/
23 | .idea_modules/
24 |
25 | # Metals specific
26 | .metals
27 | .bloop
28 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: scala
2 | script:
3 | - sbt test
4 | jdk:
5 | - openjdk8
6 | cache:
7 | directories:
8 | - $HOME/.ivy2/cache
9 | - $HOME/.sbt/boot
10 | - $HOME/.sbt/launchers
11 | - $HOME/.coursier
12 |
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Resty [](https://travis-ci.org/takezoe/resty) [](https://maven-badges.herokuapp.com/maven-central/com.github.takezoe/resty_2.12) [](https://github.com/takezoe/resty/blob/master/LICENSE)
2 | ========
3 |
4 | Super easy REST API framework for Scala
5 |
6 | You can run the sample project by hitting following commands:
7 |
8 | ```
9 | $ git clone https://github.com/takezoe/resty-sample.git
10 | $ cd resty-sample/
11 | $ sbt ~jetty:start
12 | ```
13 |
14 | Check APIs via Swagger UI at: `http://localhost:8080/swagger-ui/`.
15 |
16 | ## Getting started
17 |
18 | This is a simplest controller example:
19 |
20 | ```scala
21 | import com.github.takezoe.resty._
22 |
23 | class HelloController {
24 | @Action(method = "GET", path = "/hello/{name}")
25 | def hello(name: String): Message = {
26 | Message(s"Hello ${name}!")
27 | }
28 | }
29 |
30 | case class Message(message: String)
31 | ```
32 |
33 | Define a web listener that registers your controller.
34 |
35 | ```scala
36 | @WebListener
37 | class InitializeListener extends ServletContextListener {
38 | override def contextDestroyed(sce: ServletContextEvent): Unit = {
39 | }
40 | override def contextInitialized(sce: ServletContextEvent): Unit = {
41 | Resty.register(new HelloController())
42 | }
43 | }
44 | ```
45 |
46 | Let's test this controller.
47 |
48 | ```
49 | $ curl -XGET http://localhost:8080/hello/resty
50 | {"message": "Hello resty!" }
51 | ```
52 |
53 | ## Annotations
54 |
55 | Resty provides some annotations including `@Action`.
56 |
57 | ### @Controller
58 |
59 | You can add `@Controller` to the controller class to define the controller name and description. They are applied to Swagger JSON.
60 |
61 | |parameter |required |description |
62 | |------------|---------|------------------------------|
63 | |name |optional |name of the controller |
64 | |description |optional |description of the controller |
65 |
66 | ```scala
67 | @Controller(name = "hello", description = "HelloWorld API")
68 | class HelloController {
69 | ...
70 | }
71 | ```
72 |
73 | ### @Action
74 |
75 | We already looked `@Action` to annotate the action method. It has some more parameters to add more information about the action.
76 |
77 | |parameter |required |description |
78 | |------------|---------|-----------------------------------------------------|
79 | |method |required |GET, POST, PUT or DELETE |
80 | |path |required |path of the action (`{name}` defines path parameter) |
81 | |description |optional |description of the method |
82 | |deprecated |optional |if true then deprecated (default is false) |
83 |
84 | ```scala
85 | class HelloController {
86 | @Action(method = "GET", path = "/v1/hello",
87 | description = "Old version of HelloWorld API", deprecated = true)
88 | def hello() = {
89 | ...
90 | }
91 | }
92 | ```
93 |
94 | ### @Param
95 |
96 | `@Param` is added to the arguments of the action method to define advanced parameter binding.
97 |
98 | |parameter |required |description |
99 | |------------|---------|-----------------------------------------------------|
100 | |from |optional |query, path, header or body |
101 | |name |optional |parameter or header name (default is arg name) |
102 | |description |optional |description of the parameter |
103 |
104 | ```scala
105 | class HelloController {
106 | @Action(method = "GET", path = "/hello")
107 | def hello(
108 | @Param(from = "query", name = "user-name") userName: String,
109 | @Param(from = "header", name = "User-Agent") userAgent: String
110 | ) = {
111 | ...
112 | }
113 | }
114 | ```
115 |
116 | ## Types
117 |
118 | Resty supports following types as the parameter argument:
119 |
120 | - `Unit`
121 | - `String`
122 | - `Int`
123 | - `Long`
124 | - `Boolean`
125 | - `Option[T]`
126 | - `Seq[T]`
127 | - `Array[T]`
128 | - `Array[Byte]` (for Base64 encoded string)
129 | - `AnyRef` (for JSON in the request body)
130 |
131 | Also following types are supported as the return value of the action method:
132 |
133 | - `String` is responded as `text/plain; charset=UTF-8`
134 | - `Array[Byte]`, `InputStream`, `java.io.File` are responded as `application/octet-stream`
135 | - `AnyRef` is responded as `application/json`
136 | - `ActionResult[_]` is responded as specified status, headers and body
137 | - `Future[_]` is processed asynchronously using `AsyncContext`
138 |
139 | ## Servlet API
140 |
141 | You can access Servlet API by defining method arguments with following types:
142 |
143 | - `HttpServletRequest`
144 | - `HttpServletResponse`
145 | - `HttpSession`
146 | - `ServletContext`
147 |
148 | ```scala
149 | class HelloController {
150 | @Action(method = "GET", path = "/hello")
151 | def hello(request: HttpServletRequest): Message = {
152 | val name = request.getParameter("name")
153 | Message(s"Hello ${name}!")
154 | }
155 | }
156 | ```
157 |
158 | ## Validation
159 |
160 | It's possible to validate JSON properties by asserting properties in the constructor of the mapped case class.
161 |
162 | ```scala
163 | case class Message(message: String){
164 | assert(message.length < 10, "message must be less than 10 charactors.")
165 | }
166 | ```
167 |
168 | When the parameter value is invalid, Resty responds the following response with the `400 BadRequest` status:
169 |
170 | ```javascript
171 | {
172 | "errors": [
173 | "message must be less than 10 charactors."
174 | ]
175 | }
176 | ```
177 |
178 | ## HTTP client
179 |
180 | `HttpClientSupport` trait offers methods to send HTTP request. You can call other Web APIs easily using these methods.
181 |
182 | ```scala
183 | class HelloController extends HttpClientSupport {
184 | @Action(method = "GET", path = "/hello/{id}")
185 | def hello(id: Int): Message = {
186 | // Call other API using methods provided by HttpClientSupport
187 | val user: User = httpGet[User](s"http://localhost:8080/user/${id}")
188 | Message(s"Hello ${user.name}!")
189 | }
190 |
191 | @Action(method = "GET", path = "/hello-async/{id}")
192 | def helloAsync(id: Int): Future[Message] = {
193 | // HttpClientSupport also supports asynchronous communication
194 | val future: Future[Either[ErrorModel, User]] = httpGetAsync[User](s"http://localhost:8080/user/${id}")
195 | future.map {
196 | case Right(user) => Message(s"Hello ${user.name}!")
197 | case Left(error) => throw new ActionResultException(InternalServerError(error))
198 | }
199 | }
200 | }
201 | ```
202 |
203 | These methods have retrying ability and circuit breaker. You can configure these behavior by defining `HttpClientConfig` as an implicit value.
204 |
205 | ```scala
206 | class HelloController extends HttpClientSupport {
207 |
208 | implicit override val httpClientConfig = HttpClientConfig(
209 | maxRetry = 5, // max number of retry. default is 0 (no retry)
210 | retryInterval = 500, // interval of retry (msec). default is 0 (retry immediately)
211 | maxFailure = 3, // max number until open circuit breaker. default is 0 (disabling circuit breaker)
212 | resetInterval = 60000 // interval to reset closed circuit breaker (msec). default is 60000
213 | )
214 |
215 | ...
216 | }
217 | ```
218 |
219 | ## Swagger integration
220 |
221 | Resty provides [Swagger](http://swagger.io/) integration in default. Swagger JSON is provided at `http://localhost:8080/swagger.json` and also Swagger UI is available at `http://localhost:8080/swagger-ui/`.
222 |
223 | 
224 |
225 | Add following parameter to `web.xml` to enable Swagger integration:
226 |
227 | ```xml
228 |
229 | resty.swagger
230 | enable
231 |
232 | ```
233 |
234 | By enabling [runtime-scaladoc-reader](https://github.com/takezoe/runtime-scaladoc-reader) plugin in your project, Scaladoc of controller classes is reflected to Swagger JSON. For example, Scaladoc of the controller class is used as the tag description, and Scaladoc of the method is used as the operation description, the parameter description and the response description.
235 |
236 | ```scala
237 | addCompilerPlugin("com.github.takezoe" %% "runtime-scaladoc-reader" % "1.0.1")
238 | ```
239 |
240 | ## Hystrix integration
241 |
242 | Resty also provides [Hystrix](https://github.com/Netflix/Hystrix) integration in default. Metrics are published for each operations. The stream endpoint is available at `http://localhost:8080/hystrix.stream`. Register this endpoint to the Hystrix dashboard.
243 |
244 | 
245 |
246 | Add following parameter to `web.xml` to enable Hystrix integration:
247 |
248 | ```xml
249 |
250 | resty.hystrix
251 | enable
252 |
253 | ```
254 |
255 | ## Zipkin integration
256 |
257 | Furthermore, Resty supports [Zipkin](http://zipkin.io/) as well. You can send execution results to the Zipkin server by enabling Zipkin support and using `HttpClientSupport` for calling other APIs.
258 |
259 | Add following parameters to `web.xml` to enable Zipkin integration:
260 |
261 | ```xml
262 |
263 | resty.zipkin
264 | enable
265 |
266 |
267 | resty.zipkin.service.name
268 | resty-sample
269 |
270 |
271 | resty.zipkin.sample.rate
272 | 1.0
273 |
274 |
275 | resty.zipkin.server.url
276 | http://127.0.0.1:9411/api/v1/spans
277 |
278 | ```
279 |
280 | ## WebJars support
281 |
282 | [WebJars](http://www.webjars.org/) is a cool stuff to integrate frontend libraries with JVM based applications. Resty can host static files that provided by WebJars for frontend applications.
283 |
284 | Add a following parameter to `web.xml` to enable WebJars hosting:
285 |
286 | ```xml
287 |
288 | resty.wabjars
289 | enable
290 |
291 |
292 | resty.wabjars.path
293 | /public/assets/*
294 |
295 | ```
296 |
297 | You can add WebJars dependencies in your application as following:
298 |
299 | ```scala
300 | libraryDependencies += "org.webjars" % "jquery" % "3.1.1-1"
301 | ```
302 |
303 | Then import JavaScript library as following:
304 |
305 | ```html
306 |
307 | ```
308 |
309 | ## Static files hosting
310 |
311 | Resty is including some base servlets to host static files. You can provide a frontend application through Resty application from the classpath or the file system by defining following servlet based on these classes.
312 |
313 | ```scala
314 | // Host static files on the file system
315 | @WebServlet(name="FileResourceServlet", urlPatterns=Array("/public/*"))
316 | class MyFileResourceServlet extends FileResourceServlet("src/main/webapp")
317 |
318 | // Host static files in the classpath
319 | @WebServlet(name="ClasspathResourceServlet", urlPatterns=Array("/public/*"))
320 | class MyClasspathResourceServlet extends ResourceServlet("com/github/resty/sample/public")
321 | ```
322 |
323 | ## CORS support
324 |
325 | CORS support can be enabled by adding following parameters to `web.xml`:
326 |
327 | ```xml
328 |
329 | resty.cors
330 | enable
331 |
332 |
333 | resty.cors.allowedOrigins
334 | http://localhost:8080
335 |
336 |
337 | resty.cors.allowedMethods
338 | GET, POST
339 |
340 |
341 | resty.cors.allowedHeaders
342 | Content-Type
343 |
344 |
345 | resty.cors.allowCredentials
346 | true
347 |
348 |
349 | resty.cors.preflightMaxAge
350 | 1800
351 |
352 | ```
353 |
354 | Description about optional parameters:
355 |
356 | - `resty.cors.allowedOrigins`: Comma separated list of hosts and ports which will be allowed to make cross-origin requests (default is `*`).
357 | - `resty.cors.allowedMethods`: Comma separated list of HTTP methods will be allowed (default is `GET, POST, PUT, DELETE`).
358 | - `resty.cors.allowedHeaders`: Comma separated list of allowed HTTP headers (most headers are allowed in default).
359 | - `resty.cors.allowCredentials`: Set this parameter to true to allow cookies in CORS requests (default is `false`).
360 | - `resty.cors.preflightMaxAge`: Number of seconds that preflight request can be cached in the client (default is `0`).
361 |
--------------------------------------------------------------------------------
/build.sbt:
--------------------------------------------------------------------------------
1 | name := "resty"
2 |
3 | organization := "com.github.takezoe"
4 |
5 | version := "0.0.19-SNAPSHOT"
6 |
7 | scalaVersion := "2.12.8"
8 |
9 | libraryDependencies ++= Seq(
10 | "com.github.takezoe" %% "runtime-scaladoc-reader" % "1.0.1",
11 | "org.json4s" %% "json4s-scalap" % "3.5.4",
12 | "com.netflix.hystrix" % "hystrix-core" % "1.5.12",
13 | "com.netflix.hystrix" % "hystrix-metrics-event-stream" % "1.5.12",
14 | "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.5",
15 | "com.squareup.okhttp3" % "okhttp" % "3.10.0",
16 | "io.swagger" % "swagger-models" % "1.5.19",
17 | "io.zipkin.brave" % "brave" % "4.19.2",
18 | "io.zipkin.brave" % "brave-instrumentation-okhttp3" % "4.19.2",
19 | "io.zipkin.brave" % "brave-instrumentation-servlet" % "4.19.2",
20 | "io.zipkin.reporter2" % "zipkin-sender-okhttp3" % "2.6.0",
21 | "commons-io" % "commons-io" % "2.6",
22 | "org.webjars" % "webjars-locator" % "0.34",
23 | "ch.qos.logback" % "logback-classic" % "1.2.3",
24 | "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
25 | "org.scalatest" %% "scalatest" % "3.0.5" % "test"
26 | )
27 |
28 | scalacOptions := Seq("-deprecation")
29 |
30 | publishMavenStyle := true
31 |
32 | publishArtifact in Test := false
33 |
34 | publishTo := {
35 | val nexus = "https://oss.sonatype.org/"
36 | if (version.value.trim.endsWith("SNAPSHOT"))
37 | Some("snapshots" at nexus + "content/repositories/snapshots")
38 | else
39 | Some("releases" at nexus + "service/local/staging/deploy/maven2")
40 | }
41 |
42 | pomIncludeRepository := { _ => false }
43 |
44 | pomExtra := (
45 | https://github.com/takezoe/resty
46 |
47 |
48 | The Apache Software License, Version 2.0
49 | http://www.apache.org/licenses/LICENSE-2.0.txt
50 |
51 |
52 |
53 | https://github.com/takezoe/resty
54 | scm:git:https://github.com/takezoe/resty.git
55 |
56 |
57 |
58 | takezoe
59 | Naoki Takezoe
60 | takezoe_at_gmail.com
61 | +9
62 |
63 |
64 | )
65 |
--------------------------------------------------------------------------------
/hystrix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/hystrix.png
--------------------------------------------------------------------------------
/project/build.properties:
--------------------------------------------------------------------------------
1 | sbt.version = 1.2.8
2 |
--------------------------------------------------------------------------------
/project/plugins.sbt:
--------------------------------------------------------------------------------
1 | addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1")
2 |
--------------------------------------------------------------------------------
/src/main/java/com/github/takezoe/resty/Action.java:
--------------------------------------------------------------------------------
1 | package com.github.takezoe.resty;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Target({ElementType.METHOD})
6 | @Retention(RetentionPolicy.RUNTIME)
7 | @Documented
8 | public @interface Action {
9 |
10 | String path();
11 |
12 | /**
13 | * GET, POST, PUT or DELETE
14 | */
15 | String method();
16 |
17 | /**
18 | * Optional
19 | */
20 | String description() default "";
21 |
22 | /**
23 | * Optional (default is false)
24 | */
25 | boolean deprecated() default false;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/github/takezoe/resty/Controller.java:
--------------------------------------------------------------------------------
1 | package com.github.takezoe.resty;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Target({ElementType.METHOD})
6 | @Retention(RetentionPolicy.RUNTIME)
7 | @Documented
8 | public @interface Controller {
9 |
10 | /**
11 | *
12 | */
13 | String name();
14 |
15 | /**
16 | * Optional
17 | */
18 | String description() default "";
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/github/takezoe/resty/Param.java:
--------------------------------------------------------------------------------
1 | package com.github.takezoe.resty;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Target({ElementType.PARAMETER})
6 | @Retention(RetentionPolicy.RUNTIME)
7 | @Documented
8 | public @interface Param {
9 |
10 | /**
11 | * Optional (default is a parameter name)
12 | */
13 | String name() default "";
14 |
15 | /**
16 | * query, path, header or body
17 | */
18 | String from() default "";
19 |
20 | /**
21 | * Optional
22 | */
23 | String description() default "";
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5 |
6 |
7 |
8 | UTF-8
9 | logs/application.log
10 |
11 | logs/application.%d{yyyy-MM-dd}.log
12 | 7
13 |
14 |
15 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/css/reset.css:
--------------------------------------------------------------------------------
1 | a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0}
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/css/style.css:
--------------------------------------------------------------------------------
1 | .swagger-section #header a#logo{font-size:1.5em;font-weight:700;text-decoration:none;background:transparent url(../images/logo.png) no-repeat 0;padding:20px 0 20px 40px}#text-head{font-size:80px;font-family:Roboto,sans-serif;color:#fff;float:right;margin-right:20%}.navbar-fixed-top .navbar-brand,.navbar-fixed-top .navbar-nav,.navbar-header{height:auto}.navbar-inverse{background-color:#000;border-color:#000}#navbar-brand{margin-left:20%}.navtext{font-size:10px}.h1,h1{font-size:60px}.navbar-default .navbar-header .navbar-brand{color:#a2dfee}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2 a{color:#393939;font-family:Arvo,serif;font-size:1.5em}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2 a:hover{color:#000}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2{color:#525252;padding-left:0;display:block;clear:none;float:left;font-family:Arvo,serif;font-weight:700}.navbar-default .navbar-collapse,.navbar-default .navbar-form{border-color:#0a0a0a}.container1{width:1500px;margin:auto;margin-top:0;background-image:url(../images/shield.png);background-repeat:no-repeat;background-position:-40px -20px;margin-bottom:210px}.container-inner{width:1200px;margin:auto;background-color:hsla(192,8%,88%,.75);padding-bottom:40px;padding-top:40px;border-radius:15px}.header-content{padding:0;width:1000px}.title1{font-size:80px;font-family:Vollkorn,serif;color:#404040;text-align:center;padding-top:40px;padding-bottom:100px}#icon{margin-top:-18px}.subtext{font-size:25px;font-style:italic;color:#08b;text-align:right;padding-right:250px}.bg-primary{background-color:#00468b}.navbar-default .nav>li>a,.navbar-default .nav>li>a:focus,.navbar-default .nav>li>a:focus:hover,.navbar-default .nav>li>a:hover{color:#08b}.text-faded{font-size:25px;font-family:Vollkorn,serif}.section-heading{font-family:Vollkorn,serif;font-size:45px;padding-bottom:10px}hr{border-color:#00468b;padding-bottom:10px}.description{margin-top:20px;padding-bottom:200px}.description li{font-family:Vollkorn,serif;font-size:25px;color:#525252;margin-left:28%;padding-top:5px}.gap{margin-top:200px}.troubleshootingtext{color:hsla(0,0%,100%,.7);padding-left:30%}.troubleshootingtext li{list-style-type:circle;font-size:25px;padding-bottom:5px}.overlay{position:absolute;top:0;left:0;width:100%;height:100%;z-index:1}.block.response_body.json:hover{cursor:pointer}.backdrop{color:blue}#myModal{height:100%}.modal-backdrop{bottom:0;position:fixed}.curl{padding:10px;font-family:Anonymous Pro,Menlo,Consolas,Bitstream Vera Sans Mono,Courier New,monospace;font-size:.9em;max-height:400px;margin-top:5px;overflow-y:auto;background-color:#fcf6db;border:1px solid #e5e0c6;border-radius:4px}.curl_title{font-size:1.1em;margin:0;padding:15px 0 5px;font-family:Open Sans,Helvetica Neue,Arial,sans-serif;font-weight:500;line-height:1.1}.footer{display:none}.swagger-section .swagger-ui-wrap h2{padding:0}h2{margin:0;margin-bottom:5px}.markdown p,.swagger-section .swagger-ui-wrap .code{font-size:15px;font-family:Arvo,serif}.swagger-section .swagger-ui-wrap b{font-family:Arvo,serif}#signin:hover{cursor:pointer}.dropdown-menu{padding:15px}.navbar-right .dropdown-menu{left:0;right:auto}#signinbutton{width:100%;height:32px;font-size:13px;font-weight:700;color:#08b}.navbar-default .nav>li .details{color:#000;text-transform:none;font-size:15px;font-weight:400;font-family:Open Sans,sans-serif;font-style:italic;line-height:20px;top:-2px}.navbar-default .nav>li .details:hover{color:#000}#signout{width:100%;height:32px;font-size:13px;font-weight:700;color:#08b}
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/css/typography.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/css/typography.css
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/fonts/DroidSans-Bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/fonts/DroidSans-Bold.ttf
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/fonts/DroidSans.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/fonts/DroidSans.ttf
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/collapse.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/collapse.gif
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/expand.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/expand.gif
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/explorer_icons.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/explorer_icons.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/favicon-16x16.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/favicon-32x32.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/favicon.ico
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/logo_small.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/logo_small.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/pet_store_api.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/pet_store_api.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/throbber.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/throbber.gif
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/images/wordnik_api.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/resty/a96025f5742a78fca2ed63ca92019af0b27945c8/src/main/resources/public/vendors/swagger-ui/images/wordnik_api.png
--------------------------------------------------------------------------------
/src/main/resources/public/vendors/swagger-ui/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.",'Learn how to use',"
","
"+appName+" API requires the following scopes. Select which ones you want to grant to Swagger UI.