Skip to content

Instantly share code, notes, and snippets.

@yadavan88
Created August 2, 2016 12:08
Show Gist options
  • Save yadavan88/faab0ff69fe40fe4a646813451d6e63f to your computer and use it in GitHub Desktop.
Save yadavan88/faab0ff69fe40fe4a646813451d6e63f to your computer and use it in GitHub Desktop.
Swagger Akka-http sample
import javax.ws.rs.Path
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.{Directives, Route}
import akka.stream.ActorMaterializer
import com.github.swagger.akka.model.Info
import com.github.swagger.akka.{HasActorSystem, SwaggerHttpService}
import com.myapp.tm.ExtendedJsonUtilities
import io.swagger.annotations.{Api, ApiImplicitParam, ApiImplicitParams, ApiOperation}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.reflect.runtime.{universe => ru}
import scala.collection.mutable.ListBuffer
/**
* Created by yadu on 2/8/16.
*/
case class Department(id: Long, name: String)
case class Employee(id: Long, name: String, deptId: Long)
@Path("/departments")
@Api(value = "/departments", produces = "application/json")
class DepartmentRest extends Directives with ExtendedJsonUtilities {
@ApiOperation(value = "Get all departments", nickname = "getAllDepartments", httpMethod = "GET", response = classOf[Department], responseContainer = "List")
def getDepts = get {
complete(depts.asJson)
}
@ApiOperation(value = "Save a new department", nickname = "saveDept", httpMethod = "POST", response = classOf[Department], responseContainer = "List")
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "dept", required = true, dataType = "Department", paramType = "body", value = "Json format of a department")
))
def saveDept = post {
entity(as[String]) { json =>
val entity = extractEntity[Department](json)
depts += entity
complete(depts.asJson)
}
}
@ApiOperation(value = "Get all the employees in a particular department", nickname = "getEmpByDepartment", httpMethod = "GET", response = classOf[Employee], responseContainer = "List")
def getEmpByDept(id: Long) = {
val filter = emp.filter(_.deptId == id)
complete(filter.asJson)
}
val depts = ListBuffer(Department(1, "Engineering"),
Department(2, "Finance"),
Department(3, "Sales")
)
val emp = List(
Employee(1, "Yadu", 1),
Employee(2, "Krishnan", 2),
Employee(3, "Sachin", 3)
)
val deptRoutes = path("departments") {
getDepts ~ saveDept
} ~ path("departments" / LongNumber / "employees") { deptId =>
getEmpByDept(deptId)
}
}
class SwaggerDocumentService(address: String, port: Int, system: ActorSystem) extends SwaggerHttpService with HasActorSystem {
override implicit val actorSystem: ActorSystem = system
override implicit val materializer: ActorMaterializer = ActorMaterializer()
override val apiTypes = Seq(ru.typeOf[DepartmentRest])
override val host = address + ":" + port
override val info = Info(version = "1.0")
}
object AppBoot extends App with Directives {
implicit val system = ActorSystem("SwaggerSystem")
implicit val mat = ActorMaterializer()
def url: String = "localhost"
def port: String = "9000"
val swaggerUIRoute = pathPrefix("swagger") {
getFromResourceDirectory("swagger") ~
pathSingleSlash(get(redirect("swagger/index.html", StatusCodes.PermanentRedirect)))
}
val fullRoute:Route = swaggerUIRoute ~ new SwaggerDocumentService(url, port.toInt, system).routes ~ new DepartmentRest().deptRoutes
val routeBinding = Http()(system).bindAndHandle(fullRoute, interface = url, port = port.toInt)
routeBinding.map { x => println("Successfully Bound to " + x.localAddress) }.recover { case _ => println("Failed to Bind ") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment