Skip to content

Commit

Permalink
#5 and #6
Browse files Browse the repository at this point in the history
Include error code other than 404 #5
Include Swagger API Docs #6
  • Loading branch information
plee committed Mar 18, 2020
1 parent b2acd4c commit fac1662
Show file tree
Hide file tree
Showing 48 changed files with 227 additions and 532 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/sbt__net_liftweb_lift_json_2_12_3_4_1_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/sbt__org_scala_lang_scalap_2_12_10_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/libraries/sbt__org_webjars_swagger_ui_3_25_0_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/modules/Truss.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 1 addition & 38 deletions .idea/scala_compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 5 additions & 40 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-http4s-server" %
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "0.12.23"
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "0.12.23"
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "0.12.23"
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-akka-http" % "0.12.23"
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % "0.12.23"
libraryDependencies += "com.softwaremill.sttp.client" %% "core" % "2.0.4"
libraryDependencies += "com.softwaremill.sttp" %% "core" % "1.0.2"
Expand All @@ -52,43 +53,7 @@ libraryDependencies ++= Seq(
"io.circe" %% "circe-parser"
).map(_ % circeVersion)

scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-encoding",
"utf-8", // Specify character encoding used by source files.
"-explaintypes", // Explain type errors in more detail.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-language:existentials", // Existential types (besides wildcard types) can be written and inferred
"-language:experimental.macros", // Allow macro definition (besides implementation and application)
"-language:higherKinds", // Allow higher-kinded types
"-language:implicitConversions", // Allow definition of implicit functions called views
"-language:postfixOps", // Allow postfix operator
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-Xcheckinit", // Wrap field accessors to throw an exception on uninitialized access.
"-Xfatal-warnings", // Fail the compilation if there are any warnings.
"-Xlint:adapted-args", // Warn if an argument list is modified to match the receiver.
"-Xlint:constant", // Evaluation of a constant arithmetic expression results in an error.
"-Xlint:delayedinit-select", // Selecting member of DelayedInit.
"-Xlint:doc-detached", // A Scaladoc comment appears to be detached from its element.
"-Xlint:inaccessible", // Warn about inaccessible types in method signatures.
"-Xlint:infer-any", // Warn when a type argument is inferred to be `Any`.
"-Xlint:missing-interpolator", // A string literal appears to be missing an interpolator id.
"-Xlint:nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'.
"-Xlint:nullary-unit", // Warn when nullary methods return Unit.
"-Xlint:option-implicit", // Option.apply used implicit view.
"-Xlint:package-object-classes", // Class or object defined in package object.
"-Xlint:poly-implicit-overload", // Parameterized overloaded implicit methods are not visible as view bounds.
"-Xlint:private-shadow", // A private field (or class parameter) shadows a superclass field.
"-Xlint:stars-align", // Pattern sequence wildcard must align with sequence component.
"-Xlint:type-parameter-shadow", // A local type parameter shadows a type already in scope.
"-Xlint:unused", // Unused imports,privates,locals,implicits
"-Ywarn-dead-code", // Warn when dead code is identified.
"-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined.
// "-Ywarn-unused:implicits", // Warn if an implicit parameter is unused.
// "-Ywarn-unused:locals", // Warn if a local definition is unused.
// "-Ywarn-unused:params", // Warn if a value parameter is unused.
// "-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused.
// "-Ywarn-unused:privates", // Warn if a private member is unused.
"-Ywarn-value-discard", // Warn when non-Unit expression results are unused.
"-Ylog-classpath"
)
val silencerVersion = "1.4.2"

libraryDependencies += "com.github.ghik" % "silencer-plugin_2.12" % "1.4.2"
libraryDependencies += "net.liftweb" %% "lift-json" % "3.4.1"
1 change: 1 addition & 0 deletions project/target/config-classes/$afd2e5b1b91f4fcd6afb.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.internal.DslEntry
1 change: 1 addition & 0 deletions project/target/config-classes/$cfc17c23607d8617e69f.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.internal.DslEntry

Large diffs are not rendered by default.

68 changes: 49 additions & 19 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
import RouteGenerator._
import cats.effect.{IO, _}
import cats.implicits._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import io.circe.generic.auto._
import sttp.tapir._
import sttp.tapir.docs.openapi._
import sttp.tapir.json.circe._
import sttp.tapir.openapi.OpenAPI
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.server.akkahttp._
import sttp.tapir.swagger.akkahttp.SwaggerAkka
import com.github.ghik.silencer.silent
import java.util.concurrent.atomic.AtomicReference

object Main extends IOApp {
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

object Main extends App {

// the endpoints' routes
val helloWorldRoutes = new Endpoints("name", "hello", "get").getRoute
val byeWorldRoutes = new Endpoints("name", "bye", "post").getRoute
val streamRoutes = new StreamEndpoints("text", "stream", "get").getRoute
val helloWorldInit = new Endpoints("name", "hello", "get")
val helloWorldEndpoint = helloWorldInit.aEndpoint
val helloWorldRoute = helloWorldInit.aRoute

val byeWorldInit = new Endpoints("name", "bye", "post")
val byeWorldEndpoint = byeWorldInit.aEndpoint
val byeWorldRoute = byeWorldInit.aRoute

val streamInit = new StreamEndpoints("text", "stream", "get")
val streamEndpoint = streamInit.streamingEndpoint
val streamRoute = streamInit.streamingRoute

val route = Router(
("/" -> helloWorldRoutes),
("/" -> byeWorldRoutes),
("/" -> streamRoutes)).orNotFound
// generating the documentation in yml; extension methods come from imported packages
val openApiDocs: OpenAPI = List(helloWorldEndpoint, byeWorldEndpoint, streamEndpoint).toOpenAPI("The tapir library", "1.0.0")
val openApiYml: String = openApiDocs.toYaml

// starting the server
override def run(args: List[String]): IO[ExitCode] =
BlazeServerBuilder[IO]
.bindHttp(8080)
.withHttpApp(route)
.serve
.compile.drain.as(ExitCode.Success)
implicit val actorSystem: ActorSystem = ActorSystem()
import actorSystem.dispatcher

val routes = {
import akka.http.scaladsl.server.Directives._
helloWorldRoute ~ byeWorldRoute ~ streamRoute ~ new SwaggerAkka(openApiYml).routes
}

val bindAndCheck = Http().bindAndHandle(routes, "localhost", 8080).map { _ =>
// testing
println("Go to: http://localhost:8080/docs")
println("Press any key to exit ...")
scala.io.StdIn.readLine()
}

// cleanup
Await.result(bindAndCheck.transformWith { r =>
actorSystem.terminate().transform(_ => r)
}, 1.minute)

}
Loading

0 comments on commit fac1662

Please sign in to comment.