| author | Tomas Zeman <tomas@functionals.cz> |
| Tue, 24 Nov 2020 11:17:26 +0100 | |
| changeset 31 | 5c4364d6e726 |
| parent 14 | 7c4b05467630 |
| child 32 | 2d14f02ba3bd |
| permissions | -rw-r--r-- |
| 3 | 1 |
package sqwl.cms |
2 |
||
3 |
import akka.actor.ActorSystem |
|
4 |
import akka.http.scaladsl.Http |
|
| 13 | 5 |
import akka.http.scaladsl.model.headers.ETag |
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
6 |
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpResponse}
|
| 3 | 7 |
import akka.http.scaladsl.server.Directives._ |
8 |
import akka.stream.{ActorMaterializer, Materializer}
|
|
9 |
||
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
10 |
import scala.collection.immutable |
| 3 | 11 |
import scala.concurrent.ExecutionContextExecutor |
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
12 |
import scala.io.StdIn |
| 3 | 13 |
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
14 |
trait Server extends App with Service with config with UrlScheme {
|
| 3 | 15 |
override implicit val system: ActorSystem = ActorSystem() |
16 |
override implicit val executor: ExecutionContextExecutor = system.dispatcher |
|
17 |
override implicit val materializer: Materializer = ActorMaterializer() |
|
18 |
||
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
19 |
protected def content: iContent |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
20 |
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
21 |
val asArticle = Segment.flatMap(content.articleByPath(_)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
22 |
val asCategory = Segment.flatMap(content.categoryByPath(_)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
23 |
val asTag = Segment.flatMap(content.tagByPath(_)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
24 |
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
25 |
private def asHtml(st: ViewState): HttpResponse = {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
26 |
val s = Layout(content, st).render |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
27 |
HttpResponse( |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
28 |
headers = immutable.Seq[HttpHeader](ETag(MD5(s))), |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
29 |
entity = HttpEntity(ContentTypes.`text/html(UTF-8)`, |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
30 |
"<!DOCTYPE html>" + s)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
31 |
} |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
32 |
|
| 3 | 33 |
private val routes = get {
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
34 |
pathPrefix(http.prefix) {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
35 |
pathEnd {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
36 |
complete(asHtml(Dashboard)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
37 |
} ~ pathPrefix(asArticle) { article =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
38 |
pathEnd {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
39 |
complete(asHtml(ViewArticle(article))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
40 |
} ~ getFromDirectory(article.assets.toString) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
41 |
} ~ path(asCategory) { category =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
42 |
complete(asHtml(ViewCategory(category))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
43 |
} ~ path(TAG / asTag) { tag =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
44 |
complete(asHtml(ViewTag(tag))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
45 |
} |
| 3 | 46 |
} ~ pathPrefix(http.prefix / ASSETS) {
|
47 |
getFromResourceDirectory("META-INF/resources/webjars")
|
|
| 7 | 48 |
} ~ pathPrefix(http.prefix / PUBLIC / content.icon._1) {
|
49 |
getFromFile(content.icon._2.toFile) |
|
50 |
} ~ pathPrefix(http.prefix / PUBLIC / content.styleSheet._1) {
|
|
51 |
getFromFile(content.styleSheet._2.toFile) |
|
| 3 | 52 |
} ~ pathPrefix(http.prefix / PUBLIC) {
|
| 7 | 53 |
getFromDirectory(content.publicAssets.toString) |
| 3 | 54 |
} |
55 |
} |
|
56 |
||
57 |
Http().bindAndHandle(routes, http.interface, http.port) |
|
| 14 | 58 |
if (run.mode == "devel") {
|
59 |
system.log.info("Click `Enter` to close application...")
|
|
60 |
StdIn.readLine() |
|
61 |
system.terminate() |
|
62 |
} |
|
| 3 | 63 |
|
64 |
} |