-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed a sbt plugin bug / added example and documentation on how to us…
…e the plugin fixes #99 (#106) * fix sbt plugin to recreate swagger.json file rather than writing over it. * added an example * update documentation to use sbt plugin instead * added domain package to example * fixed swagger json test in sbt plugin * make swagger.json available for run * added the ability to set a different routes file in sbt plugin
- Loading branch information
1 parent
739b8b6
commit ba42b9d
Showing
40 changed files
with
1,092 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ project/plugins/project/ | |
# Scala-IDE specific | ||
.scala_dependencies | ||
.worksheet | ||
|
||
.DS_Store | ||
.idea | ||
|
||
*.sw* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
In short you need to create a controller that uses the library to generate the swagger spec and make it available as an endpoint. | ||
|
||
#### Step 1 | ||
For play 2.5 add Swagger API dependency to your sbt | ||
```scala | ||
resolvers += Resolver.jcenterRepo | ||
|
||
libraryDependencies += "com.iheart" %% "play-swagger" % "0.4.0" //find the latest version in the download badge at the top | ||
``` | ||
|
||
For play 2.4 please use a special release build with play 2.4 binary. | ||
```scala | ||
libraryDependencies += "com.iheart" %% "play-swagger" % "0.4.0-PLAY2.4" //find the latest version in the download badge at the top | ||
``` | ||
|
||
#### Step 2 | ||
Add a controller that uses Play swagger as a library to generates a swagger spec json and serves it as an endpoint. | ||
|
||
```scala | ||
import play.api.libs.concurrent.Execution.Implicits._ | ||
import com.iheart.playSwagger.SwaggerSpecGenerator | ||
|
||
class ApiSpecs @Inject() (cached: Cached) extends Controller { | ||
implicit val cl = getClass.getClassLoader | ||
|
||
// The root package of your domain classes, play-swagger will automatically generate definitions when it encounters class references in this package. | ||
// In our case it would be "com.iheart", play-swagger supports multiple domain package names | ||
val domainPackage = "YOUR.DOMAIN.PACKAGE" | ||
val secondDomainPackage = "YOUR.OtherDOMAIN.PACKAGE" | ||
private lazy val generator = SwaggerSpecGenerator(domainPackage, secondDomainPackage) | ||
|
||
def specs = cached("swaggerDef") { //it would be beneficial to cache this endpoint as we do here, but it's not required if you don't expect much traffic. | ||
Action.async { _ => | ||
Future.fromTry(generator.generate()).map(Ok(_)) //generate() can also taking in an optional arg of the route file name. | ||
} | ||
} | ||
|
||
} | ||
``` | ||
|
||
#### Step 3 | ||
add an end point to the routes file | ||
``` | ||
### | ||
# summary: swagger definition | ||
# description: for swagger UI to consume | ||
### | ||
GET /assets/swagger.json @controllers.swagger.ApiSpecs.specs | ||
``` | ||
|
||
|
||
Then follow Step 2 through Step 3 in the sbt-play-swagger setup in the main README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
logs | ||
target | ||
/.idea | ||
/.idea_modules | ||
/.classpath | ||
/.project | ||
/.settings | ||
/RUNNING_PID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This software is licensed under the Apache 2 license, quoted below. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with | ||
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
language governing permissions and limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is an example Play Application using play-swagger with sbt-play-swagger plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import javax.inject._ | ||
import play.api._ | ||
import play.api.http.HttpFilters | ||
import play.api.mvc._ | ||
|
||
import filters.ExampleFilter | ||
|
||
/** | ||
* This class configures filters that run on every request. This | ||
* class is queried by Play to get a list of filters. | ||
* | ||
* Play will automatically use filters from any class called | ||
* `Filters` that is placed the root package. You can load filters | ||
* from a different class by adding a `play.http.filters` setting to | ||
* the `application.conf` configuration file. | ||
* | ||
* @param env Basic environment settings for the current application. | ||
* @param exampleFilter A demonstration filter that adds a header to | ||
* each response. | ||
*/ | ||
@Singleton | ||
class Filters @Inject() ( | ||
env: Environment, | ||
exampleFilter: ExampleFilter) extends HttpFilters { | ||
|
||
override val filters = { | ||
// Use the example filter if we're running development mode. If | ||
// we're running in production or test mode then don't use any | ||
// filters at all. | ||
if (env.mode == Mode.Dev) Seq(exampleFilter) else Seq.empty | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import com.google.inject.AbstractModule | ||
import java.time.Clock | ||
|
||
import services.{ApplicationTimer, AtomicCounter, Counter} | ||
|
||
/** | ||
* This class is a Guice module that tells Guice how to bind several | ||
* different types. This Guice module is created when the Play | ||
* application starts. | ||
* Play will automatically use any class called `Module` that is in | ||
* the root package. You can create modules in other locations by | ||
* adding `play.modules.enabled` settings to the `application.conf` | ||
* configuration file. | ||
*/ | ||
class Module extends AbstractModule { | ||
|
||
override def configure() = { | ||
// Use the system clock as the default implementation of Clock | ||
bind(classOf[Clock]).toInstance(Clock.systemDefaultZone) | ||
// Ask Guice to create an instance of ApplicationTimer when the | ||
// application starts. | ||
bind(classOf[ApplicationTimer]).asEagerSingleton() | ||
// Set AtomicCounter as the implementation for Counter. | ||
bind(classOf[Counter]).to(classOf[AtomicCounter]) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package controllers | ||
|
||
import akka.actor.ActorSystem | ||
import javax.inject._ | ||
import models.Message | ||
import play.api._ | ||
import play.api.libs.json.Json | ||
import play.api.mvc._ | ||
import scala.concurrent.{ExecutionContext, Future, Promise} | ||
import scala.concurrent.duration._ | ||
|
||
/** | ||
* This controller creates an `Action` that demonstrates how to write | ||
* simple asynchronous code in a controller. It uses a timer to | ||
* asynchronously delay sending a response for 1 second. | ||
* | ||
* @param actorSystem We need the `ActorSystem`'s `Scheduler` to | ||
* run code after a delay. | ||
* @param exec We need an `ExecutionContext` to execute our | ||
* asynchronous code. | ||
*/ | ||
@Singleton | ||
class AsyncController @Inject() (actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends Controller { | ||
implicit val fmt = Json.format[Message] | ||
/** | ||
* Create an Action that returns a plain text message after a delay | ||
* of 1 second. | ||
* | ||
* The configuration in the `routes` file means that this method | ||
* will be called when the application receives a `GET` request with | ||
* a path of `/message`. | ||
*/ | ||
def message = Action.async { | ||
getFutureMessage(1.second).map { msg => Ok(Json.toJson(msg)) } | ||
} | ||
|
||
private def getFutureMessage(delayTime: FiniteDuration): Future[Message] = { | ||
val promise: Promise[Message] = Promise[Message]() | ||
actorSystem.scheduler.scheduleOnce(delayTime) { promise.success(Message("Hi!")) } | ||
promise.future | ||
} | ||
|
||
} |
Oops, something went wrong.