blob: 71a4f5bed5e2826b23c07e0b4deda35aa874d771 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package io.crashbox.ci
import java.net.URL
import java.security.MessageDigest
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.marshalling.{Marshaller, ToResponseMarshaller}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._
import akka.stream.OverflowStrategy
import akka.stream.scaladsl.{Source => Src}
import spray.json.DefaultJsonProtocol
trait HttpApi { self: Core with Schedulers with StreamStore =>
val endpoint = "api"
case class Request(url: String) {
def buildId: String = {
val bytes = MessageDigest.getInstance("SHA-256").digest(url.getBytes)
bytes.map { byte =>
Integer.toString((byte & 0xff) + 0x100, 16)
}.mkString
}
}
object Protocol extends DefaultJsonProtocol {
implicit val request = jsonFormat1(Request)
}
import Protocol._
implicit val toResponseMarshaller: ToResponseMarshaller[Src[String, Any]] =
Marshaller.opaque { items =>
val data = items.map(item => ChunkStreamPart(item.toString + "\n"))
HttpResponse(
entity = HttpEntity.Chunked(ContentTypes.`text/plain(UTF-8)`, data))
}
def httpApi: Route = pathPrefix(endpoint) {
path("submit") {
post {
entity(as[Request]) { req =>
val source = Src
.queue[String](100, OverflowStrategy.fail)
.mapMaterializedValue { q =>
q.offer(s"Build ID: ${req.buildId}")
start(
req.buildId,
new URL(req.url),
() => saveStream(req.buildId),
state => q.offer(state.toString)
)
}
complete(source)
}
}
} ~
path(Segment / "cancel") { buildId =>
post {
cancel(buildId)
complete(204 -> s"Cancelled $buildId")
}
}
}
}
|