Skip to content

Commit

Permalink
feat(sbt): 🔨 refactor project structure
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Bonezi <[email protected]>
  • Loading branch information
felipebonezi committed Oct 22, 2022
1 parent e25041f commit d5e925e
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 108 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continouos-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ jobs:
matrix:
distribution: [ 'corretto' ]
jdk: [ '8', '11' ]
scala: [ '2.12.16', '2.13.8' ]
scala: [ '2.12.17', '2.13.10' ]
experimental: [ false ]
include:
- jdk: '17'
distribution: 'corretto'
scala: '2.13.8'
scala: '2.13.10'
experimental: true

steps:
Expand Down
32 changes: 31 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ import Dependencies.scala213

lazy val root = project
.in(file("."))
.aggregate(core)
.aggregate(actuator)
.settings(
name := "play-actuator-root",
crossScalaVersions := Nil,
publish / skip := true,
)

lazy val core = project
.in(file("play-actuator-core"))
.settings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala212, scala213),
libraryDependencies ++= Dependencies.core,
publish / skip := true
)

lazy val actuator = project
.in(file("play-actuator"))
.dependsOn(core)
.settings(
name := s"$repoName",
organization := "io.github.felipebonezi",
Expand All @@ -51,6 +61,26 @@ lazy val core = project
)
.enablePlugins(Common)

lazy val database = project
.in(file("play-actuator-indicators/database"))
.dependsOn(core)
.settings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala212, scala213),
Dependencies.db,
publish / skip := true
)

lazy val redis = project
.in(file("play-actuator-indicators/redis"))
.dependsOn(core)
.settings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala212, scala213),
Dependencies.redis,
publish / skip := true
)

addCommandAlias(
"validateCode",
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
*/
package play.actuator.health
import play.actuator.ActuatorEnum.Status
import play.api.libs.json.Format
import play.api.libs.json.JsNull
import play.api.libs.json.JsNumber
import play.api.libs.json.JsString
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.libs.json.Writes
import play.api.libs.json.{JsNull, JsNumber, JsString, Json, Writes}

case class Health(name: String, status: Status, details: Map[String, Any]) {
override def toString: String = s"Health(status=$status, details=$details)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health
import play.actuator.ActuatorEnum.Status
import play.actuator.ActuatorEnum.Up
import play.actuator.ActuatorEnum.Status

import scala.collection.mutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health.indicator
package actuator.health.indicator
import play.actuator.health.indicator.BaseHealthIndicator

trait DatabaseIndicator extends BaseHealthIndicator {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health.indicator
package actuator.health.indicator

import actuator.health.indicator.DatabaseJdbcIndicator.DB_TIMEOUT_SECS
import play.actuator.ActuatorEnum
import play.actuator.health.HealthBuilder
import play.actuator.health.indicator.DatabaseJdbcIndicator.DB_TIMEOUT_SECS
import play.api.db.Database

import java.sql.Connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health.indicator
package actuator.health.indicator

import actuator.health.indicator.DatabaseSlickIndicator.DB_TIMEOUT_SECS
import com.typesafe.config.Config
import play.actuator.ActuatorEnum.Down
import play.actuator.ActuatorEnum.Up
import play.actuator.ActuatorEnum
import play.actuator.health.HealthBuilder
import play.actuator.health.indicator.DatabaseSlickIndicator.DB_TIMEOUT_SECS
import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfigProvider
import slick.jdbc.JdbcProfile
Expand All @@ -34,7 +33,7 @@ import javax.inject.Inject

class DatabaseSlickIndicator @Inject() (
val config: Config,
val dbConfigProvider: DatabaseConfigProvider
protected val dbConfigProvider: DatabaseConfigProvider
) extends DatabaseIndicator
with HasDatabaseConfigProvider[JdbcProfile] {

Expand Down Expand Up @@ -65,14 +64,14 @@ class DatabaseSlickIndicator @Inject() (
try {
val metaData = this.getConnection.getMetaData
builder
.withStatus(Up)
.withStatus(ActuatorEnum.Up)
.withDetail("name", this.dbConfigProvider.get.profileName)
.withDetail("url", this.config.getString("slick.dbs.default.db.url"))
.withDetail("driver", metaData.getDriverName)
} catch {
case e: Exception =>
builder
.withStatus(Down)
.withStatus(ActuatorEnum.Down)
.withDetail("name", this.dbConfigProvider.get.profileName)
.withDetail("url", this.config.getString("slick.dbs.default.db.url"))
.withDetail("exception", e.getMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health.indicator
package actuator.health.indicator

import com.typesafe.config.Config
import play.actuator.ActuatorEnum.Down
import play.actuator.ActuatorEnum.Up
import play.actuator.ActuatorEnum
import play.actuator.health.HealthBuilder
import play.api.cache.redis.RedisConnector

Expand All @@ -42,12 +42,12 @@ class PlayRedisIndicator @Inject() (
try {
Await.result(this.connector.ping(), 3.seconds)
builder
.withStatus(Up)
.withStatus(ActuatorEnum.Up)
.withDetail("source", this.config.getString("play.cache.redis.source"))
} catch {
case e: Exception =>
builder
.withStatus(Down)
.withStatus(ActuatorEnum.Down)
.withDetail("message", "Redis connection failed!")
.withDetail("exception", e.getMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.health.indicator
package actuator.health.indicator
import play.actuator.health.indicator.BaseHealthIndicator

trait RedisIndicator extends BaseHealthIndicator {}
1 change: 1 addition & 0 deletions play-actuator-sample/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.18")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.18")
80 changes: 36 additions & 44 deletions play-actuator/src/main/scala/play/actuator/ActuatorModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,47 @@
*/
package play.actuator
import com.google.inject.AbstractModule
import com.google.inject.name.Names
import play.actuator.ActuatorEnum.Up
import play.actuator.health.HealthBuilder
import play.actuator.health.indicator.DatabaseIndicator
import play.actuator.health.indicator.DatabaseJdbcIndicator
import play.actuator.health.indicator.DatabaseSlickIndicator
import play.actuator.health.indicator.PlayRedisIndicator
import play.actuator.health.indicator.RedisIndicator
import play.api.Configuration
import play.api.Environment
import play.api.Configuration

class ActuatorModule(environment: Environment, config: Configuration) extends AbstractModule {

override def configure(): Unit = {
if (config.has("db")) {
bind(classOf[DatabaseIndicator])
.annotatedWith(Names.named("jdbcIndicator"))
.to(classOf[DatabaseJdbcIndicator])
} else {
bind(classOf[DatabaseIndicator])
.annotatedWith(Names.named("jdbcIndicator"))
.toInstance((builder: HealthBuilder) => {
builder
.withStatus(Up)
.withDetail("message", "Application without database connection.")
})
}
if (config.has("slick")) {
bind(classOf[DatabaseIndicator])
.annotatedWith(Names.named("slickIndicator"))
.to(classOf[DatabaseSlickIndicator])
} else {
bind(classOf[DatabaseIndicator])
.annotatedWith(Names.named("slickIndicator"))
.toInstance((builder: HealthBuilder) => {
builder
.withStatus(Up)
.withDetail("message", "Application without Slick connection.")
})
}
if (config.has("play.cache.redis")) {
bind(classOf[RedisIndicator]).to(classOf[PlayRedisIndicator])
} else {
bind(classOf[RedisIndicator]).toInstance((builder: HealthBuilder) => {
builder
.withStatus(Up)
.withDetail("message", "Application without Redis connection.")
})
}
// if (config.has("db")) {
// bind(classOf[DatabaseIndicator])
// .annotatedWith(Names.named("jdbcIndicator"))
// .to(classOf[DatabaseJdbcIndicator])
// } else {
// bind(classOf[DatabaseIndicator])
// .annotatedWith(Names.named("jdbcIndicator"))
// .toInstance((builder: HealthBuilder) => {
// builder
// .withStatus(Up)
// .withDetail("message", "Application without database connection.")
// })
// }
// if (config.has("slick")) {
// bind(classOf[DatabaseIndicator])
// .annotatedWith(Names.named("slickIndicator"))
// .to(classOf[DatabaseSlickIndicator])
// } else {
// bind(classOf[DatabaseIndicator])
// .annotatedWith(Names.named("slickIndicator"))
// .toInstance((builder: HealthBuilder) => {
// builder
// .withStatus(Up)
// .withDetail("message", "Application without Slick connection.")
// })
// }
// if (config.has("play.cache.redis")) {
// bind(classOf[RedisIndicator]).to(classOf[PlayRedisIndicator])
// } else {
// bind(classOf[RedisIndicator]).toInstance((builder: HealthBuilder) => {
// builder
// .withStatus(Up)
// .withDetail("message", "Application without Redis connection.")
// })
// }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,24 @@
*/
package play.actuator.health

import play.actuator.ActuatorEnum.Down
import play.actuator.ActuatorEnum.Status
import play.actuator.ActuatorEnum.Up
import play.actuator.health.indicator.DatabaseIndicator
import play.actuator.ActuatorEnum.Down
import play.actuator.health.indicator.DiskSpaceIndicator
import play.actuator.health.indicator.RedisIndicator
import play.api.Configuration

import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
import javax.inject.Named
import javax.inject.Inject
import scala.collection.mutable

@Singleton
class HealthService @Inject() (
config: Configuration,
diskSpaceIndicator: DiskSpaceIndicator,
@Named("jdbcIndicator") jdbcIndicator: DatabaseIndicator,
@Named("slickIndicator") slickIndicator: DatabaseIndicator,
redisIndicator: RedisIndicator
diskSpaceIndicator: DiskSpaceIndicator
// @Named("jdbcIndicator") jdbcIndicator: DatabaseIndicator,
// @Named("slickIndicator") slickIndicator: DatabaseIndicator,
// redisIndicator: RedisIndicator
) {

def globalStatus: Status =
Expand All @@ -57,23 +55,23 @@ class HealthService @Inject() (
indicators.append(builder.build)
}

if (isIndicatorActive("jdbc")) {
val builder = new HealthBuilder("jdbc")
this.jdbcIndicator.info(builder)
indicators.append(builder.build)
}

if (isIndicatorActive("slick")) {
val builder = new HealthBuilder("slick")
this.slickIndicator.info(builder)
indicators.append(builder.build)
}

if (isIndicatorActive("redis")) {
val builder = new HealthBuilder("redis")
this.redisIndicator.info(builder)
indicators.append(builder.build)
}
// if (isIndicatorActive("jdbc")) {
// val builder = new HealthBuilder("jdbc")
// this.jdbcIndicator.info(builder)
// indicators.append(builder.build)
// }
//
// if (isIndicatorActive("slick")) {
// val builder = new HealthBuilder("slick")
// this.slickIndicator.info(builder)
// indicators.append(builder.build)
// }
//
// if (isIndicatorActive("redis")) {
// val builder = new HealthBuilder("redis")
// this.redisIndicator.info(builder)
// indicators.append(builder.build)
// }

indicators.toSeq
}
Expand Down
Loading

0 comments on commit d5e925e

Please sign in to comment.