|
| 1 | +package com.example.helloworld |
| 2 | + |
| 3 | +//#import |
| 4 | + |
| 5 | + |
| 6 | +import java.security.KeyStore |
| 7 | +import java.security.SecureRandom |
| 8 | +import java.security.cert.Certificate |
| 9 | +import java.security.cert.CertificateFactory |
| 10 | + |
| 11 | +import scala.io.Source |
| 12 | + |
| 13 | +import akka.actor.typed.ActorSystem |
| 14 | +import akka.actor.typed.scaladsl.Behaviors |
| 15 | +import akka.http.scaladsl.ConnectionContext |
| 16 | +import akka.http.scaladsl.Http |
| 17 | +import akka.http.scaladsl.HttpsConnectionContext |
| 18 | +import akka.http.scaladsl.model.HttpRequest |
| 19 | +import akka.http.scaladsl.model.HttpResponse |
| 20 | +import akka.pki.pem.DERPrivateKeyLoader |
| 21 | +import akka.pki.pem.PEMDecoder |
| 22 | +import com.typesafe.config.ConfigFactory |
| 23 | +import javax.net.ssl.KeyManagerFactory |
| 24 | +import javax.net.ssl.SSLContext |
| 25 | + |
| 26 | +import scala.concurrent.ExecutionContext |
| 27 | +import scala.concurrent.Future |
| 28 | +import scala.util.Failure |
| 29 | +import scala.util.Success |
| 30 | +import scala.concurrent.duration._ |
| 31 | +//#import |
| 32 | + |
| 33 | + |
| 34 | +//#server |
| 35 | +object GreeterServer { |
| 36 | + |
| 37 | + def main(args: Array[String]): Unit = { |
| 38 | + // important to enable HTTP/2 in ActorSystem's config |
| 39 | + val conf = ConfigFactory.parseString("akka.http.server.enable-http2 = on") |
| 40 | + .withFallback(ConfigFactory.defaultApplication()) |
| 41 | + val system = ActorSystem[Nothing](Behaviors.empty[Nothing], "GreeterServer", conf) |
| 42 | + new GreeterServer(system).run() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class GreeterServer(system: ActorSystem[_]) { |
| 47 | + |
| 48 | + def run(): Future[Http.ServerBinding] = { |
| 49 | + implicit val sys = system |
| 50 | + implicit val ec: ExecutionContext = system.executionContext |
| 51 | + |
| 52 | + val service: HttpRequest => Future[HttpResponse] = |
| 53 | + GreeterServiceHandler(new GreeterServiceImpl(system)) |
| 54 | + |
| 55 | + val bound: Future[Http.ServerBinding] = Http()(system) |
| 56 | + .newServerAt(interface = "127.0.0.1", port = 8080) |
| 57 | + .enableHttps(serverHttpContext) |
| 58 | + .bind(service) |
| 59 | + .map(_.addToCoordinatedShutdown(hardTerminationDeadline = 10.seconds)) |
| 60 | + |
| 61 | + bound.onComplete { |
| 62 | + case Success(binding) => |
| 63 | + val address = binding.localAddress |
| 64 | + println(s"gRPC server bound to ${address.getHostString}:${address.getPort}") |
| 65 | + case Failure(ex) => |
| 66 | + println("Failed to bind gRPC endpoint, terminating system") |
| 67 | + ex.printStackTrace() |
| 68 | + system.terminate() |
| 69 | + } |
| 70 | + |
| 71 | + bound |
| 72 | + } |
| 73 | + //#server |
| 74 | + |
| 75 | + |
| 76 | + private def serverHttpContext: HttpsConnectionContext = { |
| 77 | + val privateKey = |
| 78 | + DERPrivateKeyLoader.load(PEMDecoder.decode(readPrivateKeyPem())) |
| 79 | + val fact = CertificateFactory.getInstance("X.509") |
| 80 | + val cer = fact.generateCertificate( |
| 81 | + classOf[GreeterServer].getResourceAsStream("/certs/server1.pem") |
| 82 | + ) |
| 83 | + val ks = KeyStore.getInstance("PKCS12") |
| 84 | + ks.load(null) |
| 85 | + ks.setKeyEntry( |
| 86 | + "private", |
| 87 | + privateKey, |
| 88 | + new Array[Char](0), |
| 89 | + Array[Certificate](cer) |
| 90 | + ) |
| 91 | + val keyManagerFactory = KeyManagerFactory.getInstance("SunX509") |
| 92 | + keyManagerFactory.init(ks, null) |
| 93 | + val context = SSLContext.getInstance("TLS") |
| 94 | + context.init(keyManagerFactory.getKeyManagers, null, new SecureRandom) |
| 95 | + ConnectionContext.httpsServer(context) |
| 96 | + } |
| 97 | + |
| 98 | + private def readPrivateKeyPem(): String = |
| 99 | + Source.fromResource("certs/server1.key").mkString |
| 100 | + //#server |
| 101 | + |
| 102 | +} |
| 103 | +//#server |
0 commit comments