Skip to content

Commit 002cb4b

Browse files
authored
Release v2.0.0 (#1)
* Update libraries * Add docker-compose * Add components * Update tests * Add scalafmt * Refactor
1 parent e3018a6 commit 002cb4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+599
-639
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ out/
33
.idea
44
*.iml
55
gen
6+
.bsp/
67

78

89

.scalafmt.conf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version = 3.0.7
2+
runner.dialect = scala212
3+
maxColumn = 145
4+
5+
assumeStandardLibraryStripMargin = true
6+
7+
align.preset = none
8+
align.stripMargin = true
9+
align.openParenCallSite = false
10+
align.openParenDefnSite = false
11+
align.tokens = [
12+
{
13+
code = "%"
14+
owners = [{
15+
regex = "Term.ApplyInfix"
16+
}]
17+
}, {
18+
code = "%%"
19+
owners = [{
20+
regex = "Term.ApplyInfix"
21+
}]
22+
}
23+
]
24+
25+
binPack.literalArgumentLists = true
26+
27+
newlines.topLevelStatementBlankLines = [
28+
{
29+
blanks {before = 1}
30+
}
31+
]
32+
newlines.alwaysBeforeElseAfterCurlyIf = false
33+
newlines.alwaysBeforeMultilineDef = false
34+
newlines.alwaysBeforeCurlyBraceLambdaParams = false
35+
newlines.penalizeSingleSelectMultiArgList = false
36+
37+
verticalMultiline.newlineAfterOpenParen = false
38+
39+
optIn.annotationNewlines = true
40+
optIn.breakChainOnFirstMethodDot = false
41+
optIn.breaksInsideChains = true
42+
optIn.configStyleArguments = false
43+
44+
rewrite.redundantBraces.stringInterpolation = true
45+
rewrite.redundantBraces.includeUnitMethods = true
46+
rewrite.redundantBraces.generalExpressions = false
47+
rewrite.redundantBraces.methodBodies = true
48+
rewrite.rules = [RedundantBraces, SortModifiers, PreferCurlyFors, AvoidInfix, Imports]
49+
rewrite.imports.sort = original

README.md

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ The goal of the project is to show how you can use `slick` library as *Functiona
44

55
## Features
66

7-
- automatically creates table named `books` in the database
7+
- docker-compose for automatic database creation
88
- CRUD operations
99
- REST interface build on Akka HTTP
10-
- *HikaryCP* as connection pool
10+
- *HikaryCP* as a connection pool
11+
- Unit tests
1112

1213
## Getting Started
1314

14-
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
15+
These instructions will get you a copy of the project up and running on your local machine for development and testing
16+
purposes.
1517

16-
### Prerequisites
18+
### Database
1719

18-
This example uses `postgresql`. Firstly make sure to have one installed on your machine.
20+
Run the database (it will automatically create all necessary databases, schemas and tables)
1921

20-
21-
1. After installing create database named `example`. Replace `user` and `password` values in `application.conf` file on your own.
22-
2. In database `example` create schema named `slick`. If you want another schema name then you should change it in `kz.example.database.table.BooksTable` in definition of class `Books`
22+
```shell
23+
docker-compose up -d
24+
```
2325

2426
### Dependencies
2527

@@ -45,50 +47,49 @@ sbt run -Dconfig.resource=application.conf
4547

4648
You can play with slick through REST interface.
4749

48-
> add book
49-
50-
```shell
51-
curl -X POST \
52-
http://localhost:8080/books \
53-
-H 'Content-Type: application/json' \
54-
-H 'Postman-Token: b2650af6-4348-4681-9ad6-6f760c2124b3' \
55-
-H 'cache-control: no-cache' \
56-
-d '{
57-
"id": 1,
58-
"name": "test name",
59-
"author": "test author"
60-
}'
61-
```
62-
63-
> get book
64-
65-
```shell
66-
curl -X GET \
67-
http://localhost:8080/books/1 \
68-
-H 'Postman-Token: cfea87e8-9f4a-4ab3-914c-3926f0cf5fad' \
69-
-H 'cache-control: no-cache'
70-
```
71-
72-
> update book
73-
74-
```shell
75-
curl -X PUT \
76-
http://localhost:8080/books \
77-
-H 'Content-Type: application/json' \
78-
-H 'Postman-Token: 952b1e75-907f-4ff8-a8a3-4b16bb2f6ecd' \
79-
-H 'cache-control: no-cache' \
80-
-d '{
81-
"id": 1,
82-
"name": "test update name",
83-
"author": "test update author"
84-
}'
85-
```
86-
87-
> delete book
88-
89-
```shell
90-
curl -X DELETE \
91-
http://localhost:8080/books/1 \
92-
-H 'Postman-Token: 826f5900-865e-46fc-bb02-ab4023863d3e' \
93-
-H 'cache-control: no-cache'
94-
```
50+
- add book
51+
52+
```shell
53+
curl -X POST \
54+
http://localhost:8080/books \
55+
-H 'Content-Type: application/json' \
56+
-d '{
57+
"id": 1,
58+
"name": "test name",
59+
"author": "test author"
60+
}'
61+
```
62+
63+
- get book
64+
65+
```shell
66+
curl -X GET \
67+
http://localhost:8080/books/1
68+
```
69+
70+
- get books
71+
72+
```shell
73+
curl -X GET \
74+
http://localhost:8080/books
75+
```
76+
77+
- update book
78+
79+
```shell
80+
curl -X PUT \
81+
http://localhost:8080/books \
82+
-H 'Content-Type: application/json' \
83+
-d '{
84+
"id": 1,
85+
"name": "updated name",
86+
"author": "updated author"
87+
}'
88+
```
89+
90+
- delete book
91+
92+
```shell
93+
curl -X DELETE \
94+
http://localhost:8080/books/1
95+
```

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name := "slick-example"
22

33
version := "1.0.0"
44

5-
scalaVersion := "2.12.8"
5+
scalaVersion := "2.12.15"
66

77
libraryDependencies ++= Dependencies.depends
88

database/init.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
5+
CREATE DATABASE slick_example_test;
6+
CREATE SCHEMA book_store;
7+
8+
CREATE TABLE IF NOT EXISTS book_store.books
9+
(
10+
id INTEGER NOT NULL CONSTRAINT books_pkey PRIMARY KEY,
11+
name VARCHAR NOT NULL,
12+
author VARCHAR NOT NULL
13+
);
14+
EOSQL

docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3.1'
2+
services:
3+
postgresql:
4+
image: mdillon/postgis:9.6-alpine
5+
ports:
6+
- 5432:5432
7+
environment:
8+
POSTGRES_USER: postgres
9+
POSTGRES_PASSWORD: postgres
10+
POSTGRES_DB: slick_example
11+
volumes:
12+
- ./database:/docker-entrypoint-initdb.d/

project/Dependencies.scala

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
import sbt._
22

3-
43
object Version {
5-
val akka = "2.5.20"
6-
val slick = "3.3.0"
7-
val postgres = "42.2.5"
8-
val akkaHttp = "10.1.7"
9-
val json4s = "3.6.4"
10-
val akkaHttpJson4s = "1.25.2"
11-
val logbackClassic = "1.2.3"
12-
val scalaTest = "3.0.8-RC4"
4+
val akka = "2.6.17"
5+
val slick = "3.3.3"
6+
val postgres = "42.3.1"
7+
val akkaHttp = "10.2.7"
8+
val json4s = "4.0.3"
9+
val akkaHttpJson4s = "1.38.2"
10+
val logbackClassic = "1.2.7"
11+
val scalaTest = "3.2.10"
1312
}
1413

15-
1614
object Library {
17-
val akkaActor = "com.typesafe.akka" %% "akka-actor" % Version.akka
18-
val slick = "com.typesafe.slick" %% "slick" % Version.slick
19-
val slickHikariCP = "com.typesafe.slick" %% "slick-hikaricp" % Version.slick
20-
val postgresql = "org.postgresql" % "postgresql" % Version.postgres
21-
val akkaHttp = "com.typesafe.akka" %% "akka-http" % Version.akkaHttp
22-
val json4sNative = "org.json4s" %% "json4s-native" % Version.json4s
23-
val json4sJackson = "org.json4s" %% "json4s-jackson" % Version.json4s
15+
val akkaActor = "com.typesafe.akka" %% "akka-actor" % Version.akka
16+
val akkaStream = "com.typesafe.akka" %% "akka-stream" % Version.akka
17+
val slick = "com.typesafe.slick" %% "slick" % Version.slick
18+
val slickHikariCP = "com.typesafe.slick" %% "slick-hikaricp" % Version.slick
19+
val postgresql = "org.postgresql" % "postgresql" % Version.postgres
20+
val akkaHttp = "com.typesafe.akka" %% "akka-http" % Version.akkaHttp
21+
val json4sNative = "org.json4s" %% "json4s-native" % Version.json4s
22+
val json4sJackson = "org.json4s" %% "json4s-jackson" % Version.json4s
2423
val akkaHttpJson4s = "de.heikoseeberger" %% "akka-http-json4s" % Version.akkaHttpJson4s
25-
val logbackClassic = "ch.qos.logback" % "logback-classic" % Version.logbackClassic
26-
val scalaTest = "org.scalatest" %% "scalatest" % Version.scalaTest % Test
24+
val logbackClassic = "ch.qos.logback" % "logback-classic" % Version.logbackClassic
25+
val scalaTest = "org.scalatest" %% "scalatest" % Version.scalaTest % Test
2726
}
2827

29-
3028
object Dependencies {
3129

3230
import Library._
3331

3432
val depends = Seq(
3533
akkaActor,
34+
akkaStream,
3635
slick,
3736
slickHikariCP,
3837
postgresql,

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.2.8
1+
sbt.version = 1.5.5

src/main/resources/application.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ database {
1313
properties = {
1414
serverName = "localhost"
1515
portNumber = "5432"
16-
databaseName = "example"
16+
databaseName = "slick_example"
1717
user = "postgres"
1818
password = "postgres"
1919
}

src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</encoder>
88
</appender>
99

10-
<root level="DEBUG">
10+
<root level="INFO">
1111
<appender-ref ref="STDOUT"/>
1212
</root>
1313

src/main/scala/kz/example/Boot.scala

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,17 @@
11
package kz.example
22

3-
import akka.actor.ActorSystem
4-
import akka.http.scaladsl.Http
5-
import akka.stream.ActorMaterializer
6-
import com.typesafe.config.{Config, ConfigFactory}
7-
import kz.example.repository.{BooksPostgreRepository, BooksRepository}
8-
import kz.example.routing.RestRoutes
9-
import org.slf4j.LoggerFactory
10-
import slick.jdbc.PostgresProfile.api._
11-
12-
import scala.concurrent.ExecutionContext
13-
import scala.util.{Failure, Success}
14-
15-
16-
object Boot extends App {
17-
18-
val config: Config = ConfigFactory.load()
19-
val actorSystemName = config.getString("akka.system.name")
20-
21-
implicit val system: ActorSystem = ActorSystem(actorSystemName)
22-
implicit val materializer: ActorMaterializer = ActorMaterializer()
23-
implicit val executionContext: ExecutionContext = system.dispatcher
24-
25-
val log = LoggerFactory.getLogger(Boot.getClass)
26-
27-
val db = Database.forConfig("database.postgre")
28-
val booksRepository: BooksRepository = new BooksPostgreRepository(db)
29-
30-
booksRepository.prepareRepository().onComplete {
31-
case Success(_) => log.info("Books repository was successfully prepared")
32-
33-
case Failure(exception) =>
34-
log.error("Failed to prepare books repository with exception = {}", exception.toString)
35-
throw exception
36-
}
37-
38-
val restRoutes = new RestRoutes(booksRepository)
39-
40-
val host = config.getString("application.host")
41-
val port = config.getInt("application.port")
42-
Http().bindAndHandle(restRoutes.route, host, port)
43-
44-
log.info("{} ActorSystem started", actorSystemName)
45-
46-
system.registerOnTermination {
47-
log.info("Terminating {} ActorSystem", actorSystemName)
48-
}
49-
50-
}
3+
import kz.example.component.impl._
4+
import kz.example.http.{HttpRoute, HttpRoutingService}
5+
import kz.example.utils.{Logging, Serializers}
6+
7+
object Boot
8+
extends App
9+
with Logging
10+
with ConfigComponentImpl
11+
with ActorSystemComponentImpl
12+
with DatabaseComponentImpl
13+
with RepositoriesImpl
14+
with Serializers
15+
with HttpRoute
16+
with HttpRoutingService
17+
with RoutingComponentImpl

src/main/scala/kz/example/actor/PerRequestActor.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)