-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor options handling at CLI (#27)
- 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
Showing
34 changed files
with
476 additions
and
236 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
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
api/src/main/scala/com/azavea/franklin/api/Server.scala
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,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 | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...anklin/endpoints/CollectionEndpoint.scala → ...in/api/endpoints/CollectionEndpoint.scala
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
4 changes: 2 additions & 2 deletions
4
...n/endpoints/CollectionItemEndpoints.scala → ...i/endpoints/CollectionItemEndpoints.scala
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...klin/endpoints/LandingPageEndpoints.scala → .../api/endpoints/LandingPageEndpoints.scala
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
2 changes: 1 addition & 1 deletion
2
.../franklin/endpoints/SearchEndpoints.scala → ...nklin/api/endpoints/SearchEndpoints.scala
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
13 changes: 13 additions & 0 deletions
13
api/src/main/scala/com/azavea/franklin/api/implicits/package.scala
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,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)) | ||
} | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...lin/services/CollectionItemsService.scala → ...api/services/CollectionItemsService.scala
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
6 changes: 3 additions & 3 deletions
6
...ranklin/services/CollectionsService.scala → ...lin/api/services/CollectionsService.scala
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
Oops, something went wrong.