Skip to content

Commit

Permalink
Refactor options handling at CLI (#27)
Browse files Browse the repository at this point in the history
 - Validate database connectivity before trying to start
 - Use refined to restrict port options to positive integers
 - Move files to correct directories
 - Small docs fixes
  • Loading branch information
Chris Brown authored Nov 26, 2019
1 parent 44f2e55 commit 22e2776
Show file tree
Hide file tree
Showing 34 changed files with 476 additions and 236 deletions.
4 changes: 4 additions & 0 deletions api/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<logger name="org.http4s.server.blaze" level="INFO"/>
<logger name="org.http4s.server.middleware" level="INFO" />
<logger name="org.http4s.blaze" level="WARN"/>
<logger name="org.http4s.blaze.channel.nio1" level="WARN"/>
<logger name="org.http4s" level="INFO"/>
<root level="WARN">
<appender-ref ref="STDOUT" />
Expand Down
170 changes: 0 additions & 170 deletions api/src/main/scala/com/azavea/franklin/Server.scala

This file was deleted.

110 changes: 110 additions & 0 deletions api/src/main/scala/com/azavea/franklin/api/Server.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.azavea.franklin.api

import cats.effect._
import cats.implicits._
import com.azavea.franklin.api.commands.{ApiConfig, Commands, DatabaseConfig}
import com.azavea.franklin.api.endpoints.{
CollectionEndpoints,
CollectionItemEndpoints,
LandingPageEndpoints,
SearchEndpoints
}
import com.azavea.franklin.api.services.{
CollectionItemsService,
CollectionsService,
LandingPageService,
SearchService
}
import doobie.hikari.HikariTransactor
import doobie.util.ExecutionContexts
import org.http4s.implicits._
import org.http4s.server.blaze._
import org.http4s.server.middleware._
import org.http4s.server.{Router, Server => HTTP4sServer}
import tapir.docs.openapi._
import tapir.openapi.circe.yaml._
import tapir.swagger.http4s.SwaggerHttp4s

object Server extends IOApp {

private val banner: List[String] =
"""
$$$$$$$$$
$$$$$$$$$$$$ ________ __ __ __
$$$$$$$$$$$$ / | / | / |/ |
$$ $$$$$$$$/______ ______ _______ $$ | __ $$ |$$/ _______
$$$$$$$$ $$ |__ / \ / \ / \ $$ | / |$$ |/ |/ \
$$$$$$$$ $$ |/$$$$$$ |$$$$$$ |$$$$$$$ |$$ |_/$$/ $$ |$$ |$$$$$$$ |
$$$$$$$ $$$$$/ $$ | $$/ / $$ |$$ | $$ |$$ $$< $$ |$$ |$$ | $$ |
$ $$ | $$ | /$$$$$$$ |$$ | $$ |$$$$$$ \ $$ |$$ |$$ | $$ |
$$$$$$ $$ | $$ | $$ $$ |$$ | $$ |$$ | $$ |$$ |$$ |$$ | $$ |
$$$$$ $$/ $$/ $$$$$$$/ $$/ $$/ $$/ $$/ $$/ $$/ $$/ $$/
$$$$
""".split("\n").toList

private def createServer(
apiConfig: ApiConfig,
dbConfig: DatabaseConfig
): Resource[IO, HTTP4sServer[IO]] =
for {
connectionEc <- ExecutionContexts.fixedThreadPool[IO](2)
transactionEc <- ExecutionContexts.cachedThreadPool[IO]
xa <- HikariTransactor.newHikariTransactor[IO](
"org.postgresql.Driver",
dbConfig.jdbcUrl,
dbConfig.dbUser,
dbConfig.dbPass,
connectionEc,
transactionEc
)
allEndpoints = LandingPageEndpoints.endpoints ++ CollectionEndpoints.endpoints ++ CollectionItemEndpoints.endpoints ++ SearchEndpoints.endpoints
docs = allEndpoints.toOpenAPI("Franklin", "0.0.1")
docRoutes = new SwaggerHttp4s(docs.toYaml, "open-api", "spec.yaml").routes[IO]
landingPageRoutes = new LandingPageService[IO](apiConfig).routes
searchRoutes = new SearchService[IO](apiConfig, xa).routes
collectionRoutes = new CollectionsService[IO](xa).routes <+> new CollectionItemsService[IO](
xa
).routes
router = CORS(
Router(
"/" -> ResponseLogger.httpRoutes(false, false)(
landingPageRoutes <+> collectionRoutes <+> searchRoutes <+> docRoutes
)
)
).orNotFound
server <- {
BlazeServerBuilder[IO]
.bindHttp(apiConfig.internalPort.value, "0.0.0.0")
.withBanner(banner)
.withHttpApp(router)
.resource
}
} yield {
server
}

override def run(args: List[String]): IO[ExitCode] = {
import Commands._

applicationCommand.parse(args) map {
case RunServer(apiConfig, dbConfig) =>
createServer(apiConfig, dbConfig)
.use(_ => IO.never)
.as(ExitCode.Success)
case RunMigrations(config) => runMigrations(config)
case RunImport(catalogRoot, dbConfig) =>
runImport(catalogRoot, dbConfig).compile.drain map { _ =>
ExitCode.Success
}
} match {
case Left(e) =>
IO {
println(e.toString())
} map { _ =>
ExitCode.Error
}
case Right(s) => s
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.azavea.franklin.endpoints
package com.azavea.franklin.api.endpoints

import com.azavea.franklin.api.error.NotFound
import com.azavea.franklin.error.NotFound
import io.circe._
import tapir._
import tapir.json.circe._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.azavea.franklin.endpoints
package com.azavea.franklin.api.endpoints

import com.azavea.franklin.api.error.NotFound
import com.azavea.franklin.error.NotFound
import io.circe._
import tapir._
import tapir.json.circe._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.azavea.franklin.endpoints
package com.azavea.franklin.api.endpoints

import io.circe._
import tapir._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.azavea.franklin.endpoints
package com.azavea.franklin.api.endpoints

import io.circe._
import tapir._
Expand Down
13 changes: 13 additions & 0 deletions api/src/main/scala/com/azavea/franklin/api/implicits/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.azavea.franklin.api

import eu.timepit.refined.types.string.NonEmptyString

package object implicits {

implicit class combineNonEmptyString(s: NonEmptyString) {

def +(otherString: String): NonEmptyString =
NonEmptyString.unsafeFrom(s.value.concat(otherString))
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.azavea.franklin.services
package com.azavea.franklin.api.services

import cats.effect._
import cats.implicits._
import com.azavea.franklin.api.error.{NotFound => NF}
import com.azavea.franklin.api.endpoints.CollectionItemEndpoints
import com.azavea.franklin.error.{NotFound => NF}
import com.azavea.franklin.database.StacItemDao
import com.azavea.franklin.endpoints.CollectionItemEndpoints
import doobie.util.transactor.Transactor
import doobie._
import doobie.implicits._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.azavea.franklin.services
package com.azavea.franklin.api.services

import cats.effect._
import cats.implicits._
import com.azavea.franklin.api.error.{NotFound => NF}
import com.azavea.franklin.api.endpoints.CollectionEndpoints
import com.azavea.franklin.error.{NotFound => NF}
import com.azavea.franklin.database.StacCollectionDao
import com.azavea.franklin.datamodel.CollectionsResponse
import com.azavea.franklin.endpoints.CollectionEndpoints
import doobie.util.transactor.Transactor
import doobie._
import doobie.implicits._
Expand Down
Loading

0 comments on commit 22e2776

Please sign in to comment.