Skip to content

Commit 2f7e5a8

Browse files
Initial setup
0 parents  commit 2f7e5a8

15 files changed

+398
-0
lines changed

.gitignore

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# sbt
2+
lib_managed
3+
project/project
4+
target
5+
6+
# Worksheets (Eclipse or IntelliJ)
7+
*.sc
8+
9+
# Eclipse
10+
.cache*
11+
.classpath
12+
.project
13+
.scala_dependencies
14+
.settings
15+
.target
16+
.worksheet
17+
18+
# IntelliJ
19+
.idea
20+
21+
# ENSIME
22+
.ensime
23+
.ensime_lucene
24+
.ensime_cache
25+
26+
# Mac
27+
.DS_Store
28+
29+
# Akka
30+
ddata*
31+
journal
32+
snapshots
33+
34+
# Log files
35+
*.log
36+
37+
# jenv
38+
.java-version
39+
40+
# Metals
41+
.metals
42+
.bloop

.scalafmt.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version = "2.6.4"
2+
3+
preset = "defaultWithAlign"
4+
5+
danglingParentheses.preset = true
6+
indentOperator.preset = "spray"
7+
maxColumn = 100
8+
newlines.alwaysBeforeMultilineDef = true
9+
rewrite.rules = ["AsciiSortImports", "RedundantBraces", "RedundantParens"]
10+
spaces.inImportCurlyBraces = true
11+
unindentTopLevelOperators = true

LICENSE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MIT License (MIT)
2+
Copyright (c) <YEAR> <OWNER>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 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.

NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright 2020 Branislav Lazic

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# finch-zio-slick #
2+
3+
Test ground for integration between finch, ZIO and Slick
4+
5+
6+
## Contribution policy ##
7+
8+
Contributions via GitHub pull requests are gladly accepted from their original author. Along with
9+
any pull requests, please state that the contribution is your original work and that you license
10+
the work to the project under the project's open source license. Whether or not you state this
11+
explicitly, by submitting any copyrighted material via pull request, email, or other means you
12+
agree to license the material under the project's open source license and warrant that you have the
13+
legal authority to do so.
14+
15+
## License ##
16+
17+
This code is open source software licensed under the
18+
[MIT](https://opensource.org/licenses/MIT) license.

build.sbt

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// *****************************************************************************
2+
// Projects
3+
// *****************************************************************************
4+
5+
lazy val `finch-zio-slick` =
6+
project
7+
.in(file("."))
8+
.enablePlugins(AutomateHeaderPlugin)
9+
.settings(commonSettings)
10+
.settings(
11+
libraryDependencies ++= Seq(
12+
library.circeGeneric,
13+
library.dupinCore,
14+
library.finchCore,
15+
library.finchCirce,
16+
library.postgresql,
17+
library.slick,
18+
library.slickHikari,
19+
library.zio,
20+
library.zioInteropCats,
21+
library.scalaCheck % Test,
22+
library.scalaTest % Test
23+
)
24+
)
25+
26+
// *****************************************************************************
27+
// Library dependencies
28+
// *****************************************************************************
29+
30+
lazy val library =
31+
new {
32+
object Version {
33+
val circe = "0.13.0"
34+
val dupin = "0.1.1"
35+
val finch = "0.32.1"
36+
val postgresql = "42.2.16.jre7"
37+
val slick = "3.3.3"
38+
val zio = "1.0.1"
39+
val zioInteropCats = "2.1.4.0"
40+
val scalaCheck = "1.14.3"
41+
val scalaTest = "3.1.0"
42+
}
43+
val circeGeneric = "io.circe" %% "circe-generic" % Version.circe
44+
val dupinCore = "com.github.yakivy" %% "dupin-core" % Version.dupin
45+
val finchCore = "com.github.finagle" %% "finchx-core" % Version.finch
46+
val finchCirce = "com.github.finagle" %% "finchx-circe" % Version.finch
47+
val postgresql = "org.postgresql" % "postgresql" % Version.postgresql
48+
val slick = "com.typesafe.slick" %% "slick" % Version.slick
49+
val slickHikari = "com.typesafe.slick" %% "slick-hikaricp" % Version.slick
50+
val zio = "dev.zio" %% "zio" % Version.zio
51+
val zioInteropCats = "dev.zio" %% "zio-interop-cats" % Version.zioInteropCats
52+
val scalaCheck = "org.scalacheck" %% "scalacheck" % Version.scalaCheck
53+
val scalaTest = "org.scalatest" %% "scalatest" % Version.scalaTest
54+
}
55+
56+
// *****************************************************************************
57+
// Settings
58+
// *****************************************************************************
59+
60+
lazy val commonSettings =
61+
Seq(
62+
scalaVersion := "2.13.3",
63+
organization := "codeeng",
64+
organizationName := "Branislav Lazic",
65+
startYear := Some(2020),
66+
licenses += ("MIT", url("https://opensource.org/licenses/MIT")),
67+
scalacOptions ++= Seq(
68+
"-unchecked",
69+
"-deprecation",
70+
"-language:_",
71+
"-target:jvm-11",
72+
"-encoding",
73+
"UTF-8",
74+
"-Ywarn-unused:imports"
75+
),
76+
testFrameworks += new TestFramework("munit.Framework"),
77+
scalafmtOnCompile := true
78+
)

docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3.1'
2+
3+
services:
4+
db:
5+
image: postgres:12.3
6+
restart: always
7+
environment:
8+
POSTGRES_PASSWORD: postgres
9+
POSTGRES_USER: postgres
10+
POSTGRES_DB: todo_db
11+
ports:
12+
- 5432:5432

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.3.13

project/plugins.sbt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1")
2+
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")
3+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0")
4+
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")

src/main/resources/application.conf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
db = {
2+
connectionPool = "HikariCP"
3+
dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
4+
properties = {
5+
serverName = "localhost"
6+
portNumber = "5432"
7+
databaseName = "todo_db"
8+
user = "postgres"
9+
password = "postgres"
10+
}
11+
poolName = "todo-hikari"
12+
numThreads = 32
13+
}

src/main/scala/io/codeeng/DB.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020 Branislav Lazic
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package io.codeeng
23+
24+
import slick.jdbc.H2Profile.api._
25+
import scala.concurrent.ExecutionContext
26+
27+
object DB {
28+
val db = Database.forConfig("db")
29+
implicit val dbExecutionContext: ExecutionContext = db.executor.executionContext
30+
}

src/main/scala/io/codeeng/Main.scala

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2020 Branislav Lazic
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package io.codeeng
23+
import com.twitter.finagle.http.{ Request, Response }
24+
import com.twitter.finagle.{ Http, ListeningServer, Service }
25+
import com.twitter.util.Future
26+
import io.finch.ToAsync
27+
import zio._
28+
import zio.interop.catz._
29+
30+
object Main extends CatsApp {
31+
32+
private def closeLater(listeningServer: ListeningServer): ZIO[Any, Nothing, Any] =
33+
Task
34+
.effectSuspend(implicitly[ToAsync[Future, Task]].apply(listeningServer.close()))
35+
.catchAll(t => UIO("Failed to close the server", t))
36+
37+
private def serve(
38+
address: String,
39+
service: Service[Request, Response]
40+
): ZManaged[Any, Throwable, ListeningServer] =
41+
Managed.makeEffect(Http.server.serve(address, service))(closeLater)
42+
43+
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
44+
for {
45+
exitCode <- serve(":8000", TodoEndpoints(new TodoRepository)(implicitly[Runtime[Any]]))
46+
.use(_ => ZIO.never)
47+
.catchAll(t => UIO("Failed to start the server", t))
48+
.map(_ => zio.ExitCode.success)
49+
50+
} yield exitCode
51+
}

src/main/scala/io/codeeng/Todo.scala

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 Branislav Lazic
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package io.codeeng
23+
24+
case class Todo(id: Option[Int] = None, title: String, description: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2020 Branislav Lazic
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package io.codeeng
23+
24+
import com.twitter.finagle.Service
25+
import com.twitter.finagle.http.{ Request, Response, Status }
26+
import io.finch._
27+
import zio.Task
28+
import zio._
29+
import zio.interop.catz._
30+
import io.finch.circe._
31+
import io.circe.generic.auto._
32+
import shapeless.{ :+:, CNil }
33+
34+
class TodoEndpoints[R](todoRepository: TodoRepository)(implicit runtime: Runtime[R])
35+
extends EndpointModule[Task] {
36+
private def getTodo: Endpoint[Task, Todo] =
37+
get("api" :: path[Int]) { id: Int =>
38+
todoRepository.findOne(id).map {
39+
case Some(todo) => Ok(todo)
40+
case None => Output.empty(Status.NotFound)
41+
}
42+
}
43+
44+
private def getAllTodos: Endpoint[Task, Seq[Todo]] =
45+
get("api") {
46+
todoRepository.findAll().map { todos =>
47+
Ok(todos)
48+
}
49+
}
50+
51+
def endpoints: Endpoint[Task, Todo :+: Seq[Todo] :+: CNil] = getTodo :+: getAllTodos
52+
}
53+
54+
object TodoEndpoints {
55+
def apply[R](
56+
todoRepository: TodoRepository
57+
)(implicit runtime: Runtime[R]): Service[Request, Response] =
58+
new TodoEndpoints(todoRepository).endpoints.toService
59+
}

0 commit comments

Comments
 (0)