base/src/sqwl/cms/Server.scala
changeset 32 2d14f02ba3bd
parent 14 7c4b05467630
child 33 fa0f19a74283
equal deleted inserted replaced
31:5c4364d6e726 32:2d14f02ba3bd
       
     1 /*
       
     2  * Copyright 2018-2020 Tomas Zeman <tomas@functionals.cz>
       
     3  *
       
     4  * Licensed under the Apache License, Version 2.0 (the "License");
       
     5  * you may not use this file except in compliance with the License.
       
     6  * You may obtain a copy of the License at
       
     7  *
       
     8  *     http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  * Unless required by applicable law or agreed to in writing, software
       
    11  * distributed under the License is distributed on an "AS IS" BASIS,
       
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  * See the License for the specific language governing permissions and
       
    14  * limitations under the License.
       
    15  */
     1 package sqwl.cms
    16 package sqwl.cms
     2 
    17 
     3 import akka.actor.ActorSystem
    18 import akka.actor.typed.ActorSystem
       
    19 import akka.actor.typed.scaladsl.Behaviors
     4 import akka.http.scaladsl.Http
    20 import akka.http.scaladsl.Http
     5 import akka.http.scaladsl.model.headers.ETag
    21 import akka.http.scaladsl.model.headers.ETag
     6 import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpResponse}
    22 import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpResponse}
     7 import akka.http.scaladsl.server.Directives._
    23 import akka.http.scaladsl.server.Directives._
     8 import akka.stream.{ActorMaterializer, Materializer}
       
     9 
    24 
    10 import scala.collection.immutable
    25 import scala.collection.immutable
    11 import scala.concurrent.ExecutionContextExecutor
    26 import scala.concurrent.ExecutionContextExecutor
    12 import scala.io.StdIn
    27 import scala.io.StdIn
    13 
    28 
    14 trait Server extends App with Service with config with UrlScheme {
    29 trait Server extends App with Service[Nothing] with config with UrlScheme {
    15   override implicit val system: ActorSystem = ActorSystem()
    30 
    16   override implicit val executor: ExecutionContextExecutor = system.dispatcher
    31   override implicit val system: ActorSystem[Nothing] =
    17   override implicit val materializer: Materializer = ActorMaterializer()
    32     ActorSystem(Behaviors.empty, "Server")
       
    33   override implicit val executor: ExecutionContextExecutor =
       
    34     system.executionContext
    18 
    35 
    19   protected def content: iContent
    36   protected def content: iContent
    20 
    37 
    21   val asArticle = Segment.flatMap(content.articleByPath(_))
    38   val asArticle = Segment.flatMap(content.articleByPath(_))
    22   val asCategory = Segment.flatMap(content.categoryByPath(_))
    39   val asCategory = Segment.flatMap(content.categoryByPath(_))
    52     } ~ pathPrefix(http.prefix / PUBLIC) {
    69     } ~ pathPrefix(http.prefix / PUBLIC) {
    53       getFromDirectory(content.publicAssets.toString)
    70       getFromDirectory(content.publicAssets.toString)
    54     }
    71     }
    55   }
    72   }
    56 
    73 
    57   Http().bindAndHandle(routes, http.interface, http.port)
    74   Http().newServerAt(http.interface, http.port).bind(routes)
    58   if (run.mode == "devel") {
    75   if (run.mode == "devel") {
    59     system.log.info("Click `Enter` to close application...")
    76     system.log.info("Click `Enter` to close application...")
    60     StdIn.readLine()
    77     StdIn.readLine()
    61     system.terminate()
    78     system.terminate()
    62   }
    79   }