| author | Tomas Zeman <tzeman@volny.cz> |
| Thu, 29 Nov 2018 12:20:20 +0100 | |
| changeset 4 | 1a1347e8c5be |
| parent 3 | jvm/src/sqwl/cms/Server.scala@48479e4b89d4 |
| child 7 | 50a354e5bda4 |
| permissions | -rw-r--r-- |
| 3 | 1 |
package sqwl.cms |
2 |
||
3 |
import akka.actor.ActorSystem |
|
4 |
import akka.http.scaladsl.Http |
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
5 |
import akka.http.scaladsl.model.headers.{ETag, EntityTag}
|
|
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")
|
|
48 |
} ~ pathPrefix(http.prefix / PUBLIC) {
|
|
49 |
getFromResourceDirectory("public")
|
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
Http().bindAndHandle(routes, http.interface, http.port) |
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
54 |
system.log.info("Click `Enter` to close application...")
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
55 |
StdIn.readLine() |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
56 |
system.terminate() |
| 3 | 57 |
|
58 |
} |