Skip to content

Commit

Permalink
Merge pull request #482 from gemini-hlsw/pr/ember
Browse files Browse the repository at this point in the history
Use Ember in demo
  • Loading branch information
milessabin authored Oct 11, 2023
2 parents 8c12337 + 86ee0b0 commit 21bc5ec
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 55 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ val doobieVersion = "1.0.0-RC4"
val flywayVersion = "9.22.2"
val fs2Version = "3.9.2"
val http4sVersion = "0.23.23"
val http4sBlazeVersion = "0.23.15"
val jnrUnixsocketVersion = "0.38.21"
val kindProjectorVersion = "0.13.2"
val literallyVersion = "1.1.0"
Expand Down Expand Up @@ -238,8 +237,8 @@ lazy val demo = project
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres" % doobieVersion,
"org.tpolecat" %% "doobie-hikari" % doobieVersion,
"org.http4s" %% "http4s-blaze-server" % http4sBlazeVersion,
"org.http4s" %% "http4s-blaze-client" % http4sBlazeVersion,
"org.http4s" %% "http4s-ember-server" % http4sVersion,
"org.http4s" %% "http4s-ember-client" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.flywaydb" % "flyway-core" % flywayVersion,
Expand Down
26 changes: 13 additions & 13 deletions demo/src/main/scala/demo/DemoServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

package demo

import cats.effect.Async
import cats.implicits._
import fs2.Stream
import cats.effect.IO
import cats.effect.Resource
import cats.syntax.all._
import com.comcast.ip4s._
import org.http4s.HttpRoutes
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.middleware.Logger
import org.http4s.server.staticcontent.resourceServiceBuilder

// #server
object DemoServer {
def stream[F[_]: Async](graphQLRoutes: HttpRoutes[F]): Stream[F, Nothing] = {
def resource(graphQLRoutes: HttpRoutes[IO]): Resource[IO, Unit] = {
val httpApp0 = (
// Routes for static resources, i.e. GraphQL Playground
resourceServiceBuilder[F]("/assets").toRoutes <+>
resourceServiceBuilder[IO]("/assets").toRoutes <+>
// GraphQL routes
graphQLRoutes
).orNotFound

val httpApp = Logger.httpApp(true, false)(httpApp0)

// Spin up the server ...
for {
exitCode <- BlazeServerBuilder[F]
.bindHttp(8080, "0.0.0.0")
.withHttpApp(httpApp)
.serve
} yield exitCode
}.drain
EmberServerBuilder.default[IO]
.withHost(ip"0.0.0.0")
.withPort(port"8080")
.withHttpApp(httpApp)
.build.void
}
}
// #server
2 changes: 1 addition & 1 deletion demo/src/main/scala/demo/GraphQLService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package demo

import cats.effect.Concurrent
import cats.implicits._
import cats.syntax.all._
import edu.gemini.grackle.Mapping
import io.circe.{Json, ParsingFailure, parser}
import org.http4s.circe._
Expand Down
31 changes: 13 additions & 18 deletions demo/src/main/scala/demo/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

import cats.effect.{ExitCode, IO, IOApp, Resource}
import cats.implicits._
import cats.syntax.all._
import demo.starwars.{StarWarsData, StarWarsMapping}
import demo.world.WorldMapping
import doobie.hikari.HikariTransactor
Expand All @@ -21,22 +21,17 @@ import org.flywaydb.core.Flyway
object Main extends IOApp {

def run(args: List[String]): IO[ExitCode] = {
container.use { connInfo =>
for {
_ <- dbMigration(connInfo)
_ <- transactor(connInfo).use { xa =>
val worldGraphQLRoutes = GraphQLService.routes(
"world",
GraphQLService.fromMapping(WorldMapping.mkMappingFromTransactor(xa))
)
val starWarsGraphQLRoutes = GraphQLService.routes[IO](
"starwars",
GraphQLService.fromMapping(new StarWarsMapping[IO] with StarWarsData[IO])
)
DemoServer.stream[IO](worldGraphQLRoutes <+> starWarsGraphQLRoutes).compile.drain
}
} yield ()
}.as(ExitCode.Success)
container.evalTap(dbMigration(_)).flatMap(transactor(_)).flatMap { xa =>
val worldGraphQLRoutes = GraphQLService.routes(
"world",
GraphQLService.fromMapping(WorldMapping.mkMappingFromTransactor(xa))
)
val starWarsGraphQLRoutes = GraphQLService.routes[IO](
"starwars",
GraphQLService.fromMapping(new StarWarsMapping[IO] with StarWarsData[IO])
)
DemoServer.resource(worldGraphQLRoutes <+> starWarsGraphQLRoutes)
}.useForever
}

case class PostgresConnectionInfo(host: String, port: Int) {
Expand Down Expand Up @@ -91,7 +86,7 @@ object Main extends IOApp {

def dbMigration(connInfo: PostgresConnectionInfo): IO[Unit] = {
import connInfo._
IO {
IO.blocking {
val flyway = Flyway
.configure()
.dataSource(jdbcUrl, username, password)
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/scala/demo/starwars/StarWarsMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package demo.starwars

import cats.implicits._
import cats.syntax.all._
import edu.gemini.grackle.Predicate._
import edu.gemini.grackle.Query._
import edu.gemini.grackle.QueryCompiler._
Expand Down
1 change: 0 additions & 1 deletion demo/src/main/scala/demo/world/WorldMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package demo.world

import _root_.doobie.{Meta, Transactor}
import cats.effect.Sync
import cats.implicits._
import edu.gemini.grackle.Predicate._
import edu.gemini.grackle.Query._
import edu.gemini.grackle.QueryCompiler._
Expand Down
34 changes: 25 additions & 9 deletions docs/src/main/paradox/tutorial/db-backed-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ sbt:gsp-graphql> reStart
[info] Application demo not yet started
[info] Starting application demo in the background ...
demo Starting demo.Main.main()
[success] Total time: 0 s, completed 11-Dec-2019 16:55:35
sbt:gsp-graphql> demo [ioapp-compute-0] INFO o.h.b.c.n.NIO1SocketServerGroup - [...]
demo [ioapp-compute-0] INFO o.h.s.b.BlazeServerBuilder -
demo _ _ _ _ _
demo | |_| |_| |_ _ __| | | ___
demo | ' \ _| _| '_ \_ _(_-<
demo |_||_\__|\__| .__/ |_|/__/
demo |_|
demo [ioapp-compute-0] INFO o.h.s.b.BlazeServerBuilder - [...]
demo[ERROR] Picked up JAVA_TOOL_OPTIONS: -Xmx3489m
[success] Total time: 0 s, completed Sep 3, 2023, 5:01:11 AM
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersionOnly
demo[ERROR] INFO: Flyway Community Edition 9.22.0 by Redgate
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersion
demo[ERROR] INFO: See release notes here: https://rd.gt/416ObMi
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersion
demo[ERROR] INFO:
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.FlywayExecutor execute
demo[ERROR] INFO: Database: jdbc:postgresql://0.0.0.0:32771/test (PostgreSQL 11.8)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory allAppliedMigrations
demo[ERROR] INFO: Schema history table "public"."flyway_schema_history" does not exist yet
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbValidate validate
demo[ERROR] INFO: Successfully validated 1 migration (execution time 00:00.059s)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory create
demo[ERROR] INFO: Creating Schema History table "public"."flyway_schema_history" ...
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate migrateGroup
demo[ERROR] INFO: Current version of schema "public": << Empty Schema >>
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate doMigrateGroup
demo[ERROR] INFO: Migrating schema "public" to version "1 - WorldSetup"
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor printWarnings
demo[ERROR] WARNING: DB: there is already a transaction in progress (SQL State: 25001 - Error Code: 0)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate logSummary
demo[ERROR] INFO: Successfully applied 1 migration to schema "public", now at version v1 (execution time 00:00.110s)
demo [io-compute-2] INFO o.h.e.s.EmberServerBuilderCompanionPlatform - Ember-Server service bound to address: [::]:8080
```

This application hosts the demo services for in-memory and db-backend models, as well as a web-based GraphQL client
Expand Down
34 changes: 25 additions & 9 deletions docs/src/main/paradox/tutorial/in-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@ sbt:gsp-graphql> reStart
[info] Application demo not yet started
[info] Starting application demo in the background ...
demo Starting demo.Main.main()
[success] Total time: 0 s, completed 11-Dec-2019 16:55:35
sbt:gsp-graphql> demo [ioapp-compute-0] INFO o.h.b.c.n.NIO1SocketServerGroup - [...]
demo [ioapp-compute-0] INFO o.h.s.b.BlazeServerBuilder -
demo _ _ _ _ _
demo | |_| |_| |_ _ __| | | ___
demo | ' \ _| _| '_ \_ _(_-<
demo |_||_\__|\__| .__/ |_|/__/
demo |_|
demo [ioapp-compute-0] INFO o.h.s.b.BlazeServerBuilder - [...]
demo[ERROR] Picked up JAVA_TOOL_OPTIONS: -Xmx3489m
[success] Total time: 0 s, completed Sep 3, 2023, 5:01:11 AM
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersionOnly
demo[ERROR] INFO: Flyway Community Edition 9.22.0 by Redgate
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersion
demo[ERROR] INFO: See release notes here: https://rd.gt/416ObMi
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.license.VersionPrinter printVersion
demo[ERROR] INFO:
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.FlywayExecutor execute
demo[ERROR] INFO: Database: jdbc:postgresql://0.0.0.0:32771/test (PostgreSQL 11.8)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory allAppliedMigrations
demo[ERROR] INFO: Schema history table "public"."flyway_schema_history" does not exist yet
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbValidate validate
demo[ERROR] INFO: Successfully validated 1 migration (execution time 00:00.059s)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory create
demo[ERROR] INFO: Creating Schema History table "public"."flyway_schema_history" ...
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate migrateGroup
demo[ERROR] INFO: Current version of schema "public": << Empty Schema >>
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate doMigrateGroup
demo[ERROR] INFO: Migrating schema "public" to version "1 - WorldSetup"
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor printWarnings
demo[ERROR] WARNING: DB: there is already a transaction in progress (SQL State: 25001 - Error Code: 0)
demo[ERROR] Sep 03, 2023 5:01:18 AM org.flywaydb.core.internal.command.DbMigrate logSummary
demo[ERROR] INFO: Successfully applied 1 migration to schema "public", now at version v1 (execution time 00:00.110s)
demo [io-compute-2] INFO o.h.e.s.EmberServerBuilderCompanionPlatform - Ember-Server service bound to address: [::]:8080
```

This application hosts the demo services for in-memory and db-backend models, as well as a web-based GraphQL client
Expand Down

0 comments on commit 21bc5ec

Please sign in to comment.