-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use a one-line log format - load templates only once - fixed typos - fix reset timezone in ui settings - hide bookmarks headline if there are none
- Loading branch information
Showing
7 changed files
with
84 additions
and
74 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
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
57 changes: 57 additions & 0 deletions
57
modules/restserver/src/main/scala/docspell/restserver/webapp/Templates.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,57 @@ | ||
package docspell.restserver.webapp | ||
|
||
import cats.effect._ | ||
import cats.implicits._ | ||
import yamusca.imports._ | ||
import fs2.text | ||
|
||
import java.net.URL | ||
import cats.effect.unsafe.implicits._ | ||
|
||
trait Templates[F[_]] { | ||
def index: F[Template] | ||
def doc: F[Template] | ||
def serviceWorker: F[Template] | ||
} | ||
|
||
object Templates { | ||
|
||
def apply[F[_]: Sync]: Templates[F] = | ||
new Templates[F] { | ||
def index = Templates.indexTemplate.pure[F] | ||
def doc = Templates.docTemplate.pure[F] | ||
def serviceWorker = Templates.swTemplate.pure[F] | ||
} | ||
|
||
private lazy val indexTemplate = fromResource[IO]("/index.html").unsafeRunSync() | ||
private lazy val docTemplate = fromResource[IO]("/doc.html").unsafeRunSync() | ||
private lazy val swTemplate = fromResource[IO]("/sw.js").unsafeRunSync() | ||
|
||
def fromResource[F[_]: Sync](path: String): F[Template] = | ||
loadResource[F](path).flatMap(loadTemplate[F](_)) | ||
|
||
private def loadResource[F[_]: Sync](name: String): F[URL] = | ||
Option(getClass.getResource(name)) match { | ||
case None => | ||
Sync[F].raiseError(new Exception("Unknown resource: " + name)) | ||
case Some(r) => | ||
r.pure[F] | ||
} | ||
|
||
private def loadUrl[F[_]: Sync](url: URL): F[String] = | ||
fs2.io | ||
.readInputStream(Sync[F].delay(url.openStream()), 64 * 1024) | ||
.through(text.utf8.decode) | ||
.compile | ||
.string | ||
|
||
private def parseTemplate[F[_]: Sync](str: String): F[Template] = | ||
Sync[F].pure(mustache.parse(str).leftMap(err => new Exception(err._2))).rethrow | ||
|
||
private def loadTemplate[F[_]: Sync](url: URL): F[Template] = { | ||
val logger = docspell.logging.getLogger[F] | ||
loadUrl[F](url).flatMap(parseTemplate[F]).flatMap { t => | ||
logger.info(s"Compiled template $url") *> t.pure[F] | ||
} | ||
} | ||
} |
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