| author | Tomas Zeman <tomas@functionals.cz> |
| Tue, 24 Nov 2020 13:15:49 +0100 | |
| changeset 32 | 2d14f02ba3bd |
| parent 14 | 7c4b05467630 |
| child 33 | fa0f19a74283 |
| permissions | -rw-r--r-- |
|
32
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
1 |
/* |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
2 |
* Copyright 2018-2020 Tomas Zeman <tomas@functionals.cz> |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
3 |
* |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
5 |
* you may not use this file except in compliance with the License. |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
6 |
* You may obtain a copy of the License at |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
7 |
* |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
9 |
* |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
13 |
* See the License for the specific language governing permissions and |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
14 |
* limitations under the License. |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
15 |
*/ |
| 3 | 16 |
package sqwl.cms |
17 |
||
|
32
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
18 |
import akka.actor.typed.ActorSystem |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
19 |
import akka.actor.typed.scaladsl.Behaviors |
| 3 | 20 |
import akka.http.scaladsl.Http |
| 13 | 21 |
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
|
22 |
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpResponse}
|
| 3 | 23 |
import akka.http.scaladsl.server.Directives._ |
24 |
||
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
25 |
import scala.collection.immutable |
| 3 | 26 |
import scala.concurrent.ExecutionContextExecutor |
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
27 |
import scala.io.StdIn |
| 3 | 28 |
|
|
32
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
29 |
trait Server extends App with Service[Nothing] with config with UrlScheme {
|
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
30 |
|
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
31 |
override implicit val system: ActorSystem[Nothing] = |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
32 |
ActorSystem(Behaviors.empty, "Server") |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
33 |
override implicit val executor: ExecutionContextExecutor = |
|
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
34 |
system.executionContext |
| 3 | 35 |
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
36 |
protected def content: iContent |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
37 |
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
HttpResponse( |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
45 |
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
|
46 |
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
|
47 |
"<!DOCTYPE html>" + s)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
48 |
} |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
49 |
|
| 3 | 50 |
private val routes = get {
|
|
4
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
51 |
pathPrefix(http.prefix) {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
52 |
pathEnd {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
53 |
complete(asHtml(Dashboard)) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
54 |
} ~ pathPrefix(asArticle) { article =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
55 |
pathEnd {
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
56 |
complete(asHtml(ViewArticle(article))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
57 |
} ~ getFromDirectory(article.assets.toString) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
58 |
} ~ path(asCategory) { category =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
59 |
complete(asHtml(ViewCategory(category))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
60 |
} ~ path(TAG / asTag) { tag =>
|
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
61 |
complete(asHtml(ViewTag(tag))) |
|
1a1347e8c5be
Build modules: base, content, app; example content, minimal layout
Tomas Zeman <tzeman@volny.cz>
parents:
3
diff
changeset
|
62 |
} |
| 3 | 63 |
} ~ pathPrefix(http.prefix / ASSETS) {
|
64 |
getFromResourceDirectory("META-INF/resources/webjars")
|
|
| 7 | 65 |
} ~ pathPrefix(http.prefix / PUBLIC / content.icon._1) {
|
66 |
getFromFile(content.icon._2.toFile) |
|
67 |
} ~ pathPrefix(http.prefix / PUBLIC / content.styleSheet._1) {
|
|
68 |
getFromFile(content.styleSheet._2.toFile) |
|
| 3 | 69 |
} ~ pathPrefix(http.prefix / PUBLIC) {
|
| 7 | 70 |
getFromDirectory(content.publicAssets.toString) |
| 3 | 71 |
} |
72 |
} |
|
73 |
||
|
32
2d14f02ba3bd
SQWL#2014 Server platform (component) upgrade
Tomas Zeman <tomas@functionals.cz>
parents:
14
diff
changeset
|
74 |
Http().newServerAt(http.interface, http.port).bind(routes) |
| 14 | 75 |
if (run.mode == "devel") {
|
76 |
system.log.info("Click `Enter` to close application...")
|
|
77 |
StdIn.readLine() |
|
78 |
system.terminate() |
|
79 |
} |
|
| 3 | 80 |
|
81 |
} |