Skip to content

Instantly share code, notes, and snippets.

View duanebester's full-sized avatar

Duane Bester duanebester

View GitHub Profile
@duanebester
duanebester / AkkaWSExample.scala
Created December 9, 2019 00:40
Akka WebSocket Example
def greeter: Flow[Message, Message, Any] =
Flow[Message].mapConcat {
case tm: TextMessage =>
TextMessage(Source.single("Hello ") ++ tm.textStream ++ Source.single("!")) :: Nil
case bm: BinaryMessage =>
// ignore binary messages but drain content to avoid the stream being clogged
bm.dataStream.runWith(Sink.ignore)
Nil
}
val websocketRoute =
@duanebester
duanebester / wsStatusFlow.scala
Created December 9, 2019 00:37
WebSocket Status Flow
val (wsActor, wsSource) = Source
.actorRef[Message](32, OverflowStrategy.dropNew)
.preMaterialize()
def wsStatusFlow(uuid: String): Flow[Message, Message, Any] =
Flow.fromSinkAndSource(Sink.ignore, wsSource)
@duanebester
duanebester / uploadRoute.scala
Created December 9, 2019 00:15
Upload Route
val uploadRoute =
pathPrefix("image") {
path("upload") {
post {
uploadedFile("fileUpload") {
case (_, file) =>
val image = ImageIO.read(file)
processImage(image)
complete(StatusCodes.OK)
}
@duanebester
duanebester / processImage.scala
Last active December 9, 2019 00:11
Process Image
def processImage(bi: BufferedImage) = Source.single(bi).via(processImageFlow).runWith(Sink.ignore)
@duanebester
duanebester / processImageFlow.scala
Created December 9, 2019 00:07
Process Image Flow
def processImageFlow(): Flow[BufferedImage, ImageProcessed, NotUsed] =
processStage(1)
.via(processStage(2))
.via(processStage(3))
.via(processStage(4))
.map(_ => ImageProcessed("Complete!"))
@duanebester
duanebester / processStage.scala
Created December 8, 2019 20:37
Process Stage
def processStage(stageNum: Int) = Flow[BufferedImage]
.async
.delay(1 seconds)
.map(bi => {
println(s"Processing Stage: ${stageNum}")
bi
})
@duanebester
duanebester / gql-elastic.App-3.js
Created December 2, 2019 19:45
App 3 Map listeners
const moveEnd = map => {
const bbox = mapBoundsToBbox(map.getBounds());
setBBox(bbox);
};
const onLoad = () => {
setMapState({ zoom: [10], loaded: true });
};
@duanebester
duanebester / gql-elastic.mapBoundsToBbox.js
Created December 2, 2019 19:43
Map Bounds To Bbox Utility
const mapBoundsToBbox = bounds => {
const northWest = bounds.getNorthWest();
const southEast = bounds.getSouthEast();
const topLeft = {
lat: `${northWest.lat}`,
lon: `${northWest.lng}`
};
const bottomRight = {
lat: `${southEast.lat}`,
lon: `${southEast.lng}`
@duanebester
duanebester / gql-elastic.App-2.js
Created December 2, 2019 19:41
App 2 - bbox state
const [bbox, setBBox] = useState({
topLeft: {
lat: null,
lon: null
},
bottomRight: {
lat: null,
lon: null
}
});
const client = new ApolloClient({
uri: "http://localhost:8080/graphql"
});
// inside function App()
return (
<div className="App">
<ApolloProvider client={client}>
<Map
{...mapConfig}