-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Moved to monorepo project structure with separate frontend/backend folders. * Created Matrix build on Travis CI and using specific badges, currently code coverage is only collected for the frontend * Initial dockerfiles and kubernetes config for frontend, mongo, and backend. * Server currently is just a minimal Scala/Finch server from the tutorials, which is enough to prove the build process * Basic put/get Mongo service created with scala & http4s
- Loading branch information
Showing
56 changed files
with
524 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,2 @@ | ||
# See https://help.github.com/ignore-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# bucklescript | ||
/lib | ||
/types | ||
.merlin | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.vscode | ||
.idea |
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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
cache: yarn | ||
script: ["yarn add coveralls", "yarn bsb -clean-world -make-world", "yarn ci"] | ||
matrix: | ||
include: | ||
- language: node_js | ||
node_js: | ||
- "8" | ||
cache: yarn | ||
before_script: | ||
- cd dokusho | ||
script: ["yarn add coveralls", "yarn bsb -clean-world -make-world", "yarn ci"] | ||
|
||
- language: scala | ||
jdk: oraclejdk8 | ||
scala: | ||
- 2.12.4 | ||
before_script: | ||
- cd dokusho-server | ||
script: | ||
- sbt test |
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 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,7 @@ | ||
#!/bin/bash | ||
|
||
eval $(minikube docker-env) | ||
|
||
docker build ../dokusho -t dokusho:web | ||
|
||
docker build ../dokusho-server -t dokusho:server |
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,7 @@ | ||
#!/bin/bash | ||
|
||
kubectl create -f ../kube/mongo-service.yaml | ||
kubectl create -f ../kube/web-service.yaml | ||
|
||
kubectl create -f ../kube/mongo-controller.yaml | ||
kubectl create -f ../kube/web-controller.yaml |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
*.class | ||
*.log | ||
|
||
# sbt specific | ||
.cache | ||
.history | ||
.lib/ | ||
dist/* | ||
target/ | ||
lib_managed/ | ||
src_managed/ | ||
project/boot/ | ||
project/plugins/project/ | ||
|
||
# Idea | ||
.idea/ |
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,24 @@ | ||
# Dokusho-Server | ||
|
||
[![Build Status](https://travis-matrix-badges.herokuapp.com/repos/RawToast/dokusho/branches/master/2)](https://travis-ci.org/RawToast/dokusho) | ||
|
||
Backend server for Dokusho. This is a work in progress and is not currently being used by the frontend. | ||
|
||
## Running the backend | ||
|
||
### Using sbt | ||
|
||
`sbt run` will compile and start a server listening on `8080` | ||
|
||
### Docker | ||
|
||
Currently, no docker configuration has been craeted for this module. | ||
|
||
## Additional Information | ||
|
||
This project is built using the following: | ||
|
||
* [Scala](http://scala-lang.org) | ||
* [Cats](https://typelevel.org/cats) | ||
* [Http4s](http://http4s.org) | ||
* [Circe](https://circe.github.io/circe/) |
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,3 @@ | ||
#!/bin/bash | ||
|
||
docker build ../dokusho-server -t dokusho:server |
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,30 @@ | ||
name := "dokusho-server" | ||
|
||
mainClass in(Compile, run) := Some("Main") | ||
|
||
val SCALA_VERSION = "2.12.4" | ||
val CIRCE_VERSION = "0.9.1" | ||
val HTTP4S_VERSION = "0.18.1" | ||
val MONGO_VERSION = "2.2.1" | ||
|
||
resolvers ++= Seq( | ||
Resolver.sonatypeRepo("releases"), | ||
Resolver.sonatypeRepo("snapshots"), | ||
"Bintary JCenter" at "http://jcenter.bintray.com" | ||
) | ||
|
||
libraryDependencies ++= Seq( | ||
"org.http4s" %% "http4s-dsl" % HTTP4S_VERSION, | ||
"org.http4s" %% "http4s-blaze-server" % HTTP4S_VERSION, | ||
"org.http4s" %% "http4s-circe" % HTTP4S_VERSION, | ||
|
||
"io.circe" %% "circe-generic" % CIRCE_VERSION, | ||
"io.circe" %% "circe-generic-extras" % CIRCE_VERSION, | ||
"io.circe" %% "circe-parser" % CIRCE_VERSION, | ||
|
||
"org.mongodb.scala" %% "mongo-scala-driver" % MONGO_VERSION, | ||
|
||
"ch.qos.logback" % "logback-classic" % "1.2.3" | ||
) | ||
|
||
scalacOptions ++= Seq("-Ypartial-unification") |
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,7 @@ | ||
FROM hseeberger/scala-sbt:8u151-2.12.4-1.1.1 | ||
|
||
COPY . . | ||
|
||
RUN sbt compile | ||
|
||
CMD sbt run |
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 @@ | ||
sbt.version = 1.1.1 |
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,14 @@ | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<!-- encoders are assigned the type | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
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,28 @@ | ||
import cats.effect._ | ||
import dokusho.{MongoRepository, MongoService} | ||
import org.http4s.dsl.io._ | ||
import org.http4s.server.blaze.BlazeBuilder | ||
import fs2.{Stream, StreamApp} | ||
import fs2.StreamApp.ExitCode | ||
import org.http4s.server.ServerBuilder | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
object Main extends StreamApp[IO] { | ||
|
||
//TODO move to .env | ||
lazy val mongo = new MongoRepository( | ||
"mongodb://mongo:27017", | ||
"test", | ||
"dokusho") | ||
|
||
val mongoService = new MongoService(mongo) | ||
|
||
|
||
override def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, ExitCode] = | ||
BlazeBuilder[IO] | ||
.bindHttp(8080, "localhost") | ||
.mountService(mongoService.routes, "/") | ||
.withBanner(ServerBuilder.DefaultBanner) | ||
.serve | ||
} |
59 changes: 59 additions & 0 deletions
59
dokusho-server/src/main/scala/dokusho/MongoRepository.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,59 @@ | ||
package dokusho | ||
|
||
import cats.effect._ | ||
import io.circe.generic.auto._ | ||
import io.circe.parser._ | ||
import io.circe.syntax._ | ||
import io.circe.{DecodingFailure, Json, ParsingFailure} | ||
import org.bson.Document | ||
import org.mongodb.scala.model.Filters._ | ||
import org.mongodb.scala.{MongoClient, MongoCollection, Observable, model} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class MongoRepository(connectionString: String, databaseName: String, collectionName: String) { | ||
|
||
implicit val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
lazy val collection: MongoCollection[Document] = | ||
MongoClient(connectionString) | ||
.getDatabase(databaseName) | ||
.getCollection(collectionName) | ||
|
||
def get(userId: String) = for { | ||
document <- getDocument(userId) | ||
hs <- toUserReadingHistory(document) | ||
} yield hs | ||
|
||
def put(g: UserReadingHistory): IO[UserReadingHistory] = | ||
collection.replaceOne(equal("uuid", g.userId.toString), Document.parse(g.asJson.spaces2), | ||
model.UpdateOptions().upsert(true)).asIO | ||
.map(_ => g) | ||
|
||
|
||
private def getDocument(id: String): IO[Document] = collection.find(equal("userId", id)).asIO | ||
|
||
private def toUserReadingHistory(task: Document): IO[UserReadingHistory] = { | ||
for { | ||
json <- parseJson(task.toJson) | ||
readingHistory <- parseReadingHistory(json) | ||
} yield readingHistory | ||
} | ||
|
||
private def parseJson(s: String): IO[Json] = parse(s) match { | ||
case Left(err: ParsingFailure) => IO.raiseError(err.underlying) | ||
case Right(json) => IO.pure(json) | ||
} | ||
|
||
private def parseReadingHistory(j: Json): IO[UserReadingHistory] = j.as[UserReadingHistory] match { | ||
case Left(err: DecodingFailure) => IO.raiseError(err.getCause) | ||
case Right(json: UserReadingHistory) => IO.pure(json) | ||
} | ||
|
||
private implicit class IOSyntax[T](obs: Observable[T]) { | ||
def asIO: IO[T] = IO.fromFuture(IO(obs.head())) | ||
|
||
def asSeqIO: IO[Seq[T]] = IO.fromFuture(IO(obs.toFuture())) | ||
} | ||
|
||
} |
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,31 @@ | ||
package dokusho | ||
|
||
import cats.effect.IO | ||
import io.circe.Json | ||
import io.circe.generic.auto._ | ||
import io.circe.syntax._ | ||
import org.http4s._ | ||
import org.http4s.circe._ | ||
import org.http4s.dsl.io.{->, /, GET, Ok, Root, _} | ||
|
||
class MongoService(mongoRepository: MongoRepository) { | ||
|
||
case class SuccessfulPut(userId: String) | ||
|
||
val routes: HttpService[IO] = HttpService[IO] { | ||
case GET -> Root / "mongo" / name => | ||
for { | ||
userReadingHistory <- mongoRepository.get(name) | ||
json: Json = userReadingHistory.asJson | ||
response <- Ok(json) | ||
} yield response | ||
case req@PUT -> Root / "mongo" => | ||
implicit val userDecoder: EntityDecoder[IO, UserReadingHistory] = jsonOf[IO, UserReadingHistory] | ||
for { | ||
userReadingHistory <- req.as[UserReadingHistory] | ||
storedHistory <- mongoRepository.put(userReadingHistory) | ||
json: Json = SuccessfulPut(storedHistory.userId).asJson | ||
response <- Ok(json) | ||
} yield response | ||
} | ||
} |
Oops, something went wrong.