-
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.
Basic hardcoded integration and kube deployment. * Fetch client for fetching history, reseting history, and adding new entries * Created JSON encoders and decoders * Add entry adjustments, reduced cluster size * Updated readmes, to reflect the multi-module nature of the project * Additional .gitignore entries for erroneous target directories * Created deployment configuration for Google Cloud
- Loading branch information
Showing
28 changed files
with
492 additions
and
159 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,3 +1,5 @@ | ||
.vscode | ||
.idea | ||
*.iml | ||
target/ | ||
project/target/ |
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,9 @@ | ||
#!/bin/bash | ||
|
||
docker build ../dokusho -t gcr.io/dokusho-199010/dokusho-web | ||
|
||
docker build ../dokusho-server -t gcr.io/dokusho-199010/dokusho-server | ||
|
||
gcloud docker -- push gcr.io/dokusho-199010/dokusho-web:latest | ||
|
||
gcloud docker -- push gcr.io/dokusho-199010/dokusho-server:latest |
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
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.
43 changes: 43 additions & 0 deletions
43
dokusho-server/src/main/scala/dokusho/ReadingHistoryRouter.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,43 @@ | ||
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 ReadingHistoryRouter(readingHistoryService: ReadingHistoryService) { | ||
|
||
case class SuccessfulPut(userId: String) | ||
|
||
val routes: HttpService[IO] = HttpService[IO] { | ||
case GET -> Root / "history" / userId => | ||
for { | ||
userReadingHistory <- readingHistoryService.getReadingHistory(userId) | ||
json: Option[Json] = userReadingHistory.map(_.asJson) | ||
resp <- json.fold(NotFound())(j => Ok(j)) | ||
} yield resp | ||
case req@PUT -> Root / "history" / userId => | ||
implicit val userDecoder: EntityDecoder[IO, ReadingHistory] = jsonOf[IO, ReadingHistory] | ||
for { | ||
readingHistory <- req.as[ReadingHistory] | ||
storedHistory <- readingHistoryService.upsert(UserReadingHistory(userId, readingHistory)) | ||
json: Json = SuccessfulPut(storedHistory.userId).asJson | ||
response <- Ok(json) | ||
} yield response | ||
case req@POST -> Root / "history" / userId / "add" => | ||
implicit val entryDecoder: EntityDecoder[IO, NewEntry] = jsonOf[IO, NewEntry] | ||
for { | ||
entry <- req.as[NewEntry] | ||
storedHistory <- readingHistoryService.addNewEntry(userId, entry) | ||
json = storedHistory.map(_.asJson) | ||
result <- json.fold(NotFound())(j => Ok(j)) | ||
} yield result | ||
case PUT -> Root / "history" / userId / "reset" => | ||
readingHistoryService.reset(userId) | ||
.map(_.asJson) | ||
.flatMap(j => Ok(j)) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
dokusho-server/src/main/scala/dokusho/ReadingHistoryService.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,52 @@ | ||
package dokusho | ||
|
||
import java.time.LocalDate | ||
|
||
import cats.data.OptionT | ||
import cats.effect.IO | ||
import monocle.macros.GenLens | ||
import org.bson.Document | ||
import org.mongodb.scala.model | ||
import org.mongodb.scala.model.Filters.equal | ||
|
||
class ReadingHistoryService(mongoRepository: MongoRepository) { | ||
|
||
private lazy val daysLens = GenLens[UserReadingHistory](_.readingHistory.days) | ||
private lazy val entriesLens = GenLens[Day](_.entries) | ||
|
||
def getReadingHistory(userId: String): IO[Option[UserReadingHistory]] = | ||
mongoRepository.get(userId) | ||
|
||
def addNewEntry(userId: String, entry: NewEntry): IO[Option[UserReadingHistory]] = { | ||
lazy val update = daysLens.modify(updateDay(entry)) | ||
OptionT(getReadingHistory(userId)) | ||
.map(update) | ||
.semiflatMap(upsert) | ||
.value | ||
} | ||
|
||
def upsert(userReadingHistory: UserReadingHistory): IO[UserReadingHistory] = | ||
mongoRepository.put(userReadingHistory) | ||
|
||
def reset(userId: String): IO[UserReadingHistory] = { | ||
val emptyHistory = UserReadingHistory(userId, ReadingHistory(Seq.empty)) | ||
upsert(emptyHistory) | ||
} | ||
|
||
private def updateDay(entry: NewEntry)(days: Seq[Day]) = { | ||
val currentDay = Day(LocalDate.now().atStartOfDay().toString, Seq.empty) | ||
|
||
val daysWithUpdatedDay = | ||
if (days.exists(_.date == currentDay.date)) days | ||
else currentDay +: days | ||
|
||
daysWithUpdatedDay.withFilter(_.date == currentDay.date) | ||
.map(addEntry(entry)) | ||
} | ||
|
||
private def addEntry(entry: NewEntry) = entriesLens | ||
.modify { es => Entry(getNextId(es), entry.kind, entry.value) +: es} | ||
|
||
private def getNextId(entries: Seq[Entry]) = 1l + | ||
entries.foldLeft(-1l)((currMax, entry) => Math.max(currMax, entry.id)) | ||
} |
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
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,51 @@ | ||
[%raw "require('isomorphic-fetch')"]; | ||
|
||
open Types; | ||
|
||
module Client = { | ||
type serverResponse = { | ||
userId: string, | ||
readingHistory: readingHistory | ||
}; | ||
let parseResponse = (json: Js.Json.t) : serverResponse => | ||
Json.Decode.{ | ||
userId: json |> field("userId", string), | ||
readingHistory: json |> field("readingHistory", Decoders.parseHistory) | ||
}; | ||
let backendURI = "http://35.189.70.144:8080"; | ||
/* let backendURI = "http://localhost:8080"; */ | ||
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"}); | ||
|
||
/* Fetches the given user's reading history */ | ||
let userHistory = (userId:string) => | ||
Js.Promise.( | ||
Fetch.fetch(backendURI ++ "/history/" ++ userId) | ||
|> then_(Fetch.Response.json) | ||
|> then_(resp => resp |> parseResponse |> resolve) | ||
); | ||
|
||
/* Adds a new reading entry for today to a user's reading history */ | ||
let newEntry = (userId:string, kind: pageType, value: int) => { | ||
Js.Promise.( | ||
Fetch.fetchWithInit(backendURI ++ "/history/" ++ userId ++ "/add", | ||
Fetch.RequestInit.make(~method_=Post, | ||
~body=Fetch.BodyInit.make(Encoders.endcodeInput(kind, value) |> Js.Json.stringify), | ||
~headers=jsonHeader, | ||
())) | ||
|> then_(Fetch.Response.json) | ||
|> then_(resp => resp |> parseResponse |> resolve) | ||
); | ||
}; | ||
|
||
/* Resets a user's reading history */ | ||
let resetUser = (userId:string) => { | ||
Js.Promise.( | ||
Fetch.fetchWithInit(backendURI ++ "/history/" ++ userId ++ "/reset", | ||
Fetch.RequestInit.make(~method_=Put, | ||
~headers=jsonHeader, | ||
())) | ||
|> then_(Fetch.Response.json) | ||
|> then_(resp => resp |> parseResponse |> resolve) | ||
); | ||
}; | ||
}; |
Oops, something went wrong.