-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
208 additions
and
38 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
example/src/main/scala/app/wishingtree/HttpAppExample.scala
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
example/src/main/scala/app/wishingtree/SpiderAppExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package app.wishingtree | ||
|
||
import com.sun.net.httpserver.Filter | ||
import dev.wishingtree.branch.friday.JsonEncoder | ||
import dev.wishingtree.branch.friday.http.JsonBodyHandler | ||
import dev.wishingtree.branch.spider.* | ||
import dev.wishingtree.branch.spider.client.ClientRequest.uri | ||
import dev.wishingtree.branch.spider.client.{Client, ClientRequest} | ||
import dev.wishingtree.branch.spider.server.* | ||
import dev.wishingtree.branch.spider.server.OpaqueSegments.* | ||
|
||
import java.net.http.HttpResponse | ||
|
||
object SpiderAppExample extends SpiderApp { | ||
|
||
import RequestHandler.given | ||
|
||
val staticFilesPath = Segments.wd / "site" / "book" | ||
val files = FileContext(staticFilesPath) | ||
|
||
case class Person(name: String) | ||
|
||
given Conversion[Person, Array[Byte]] = { person => | ||
summon[JsonEncoder[Person]].encode(person).toJsonString.getBytes | ||
} | ||
|
||
val personHandler = new ContextHandler("/") { | ||
override val contextRouter: PartialFunction[ | ||
(HttpVerb, Segments), | ||
RequestHandler[Unit, Person] | ||
] = { case HttpVerb.GET -> >> / "person" => | ||
new RequestHandler[Unit, Person] { | ||
override def handle(request: Request[Unit]): Response[Person] = | ||
Response(Person("Mark")) | ||
} | ||
} | ||
} | ||
|
||
val handlers: ContextHandler = files |+| personHandler | ||
ContextHandler.registerHandler(handlers) | ||
|
||
val client = Client.build() | ||
|
||
val request = ClientRequest | ||
.build(uri"http://localhost:9000/person") | ||
|
||
val response = | ||
client.sendAsync(request, JsonBodyHandler.of[Person]) | ||
|
||
response | ||
.thenApply { resp => | ||
println(resp.body()) | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
friday/src/main/scala/dev/wishingtree/branch/friday/http/JsonBodyHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.wishingtree.branch.friday.http | ||
|
||
import dev.wishingtree.branch.friday.JsonDecoder | ||
|
||
import java.net.http.HttpResponse | ||
import java.net.http.HttpResponse.{BodyHandler, BodySubscribers} | ||
import java.nio.charset.Charset | ||
import scala.util.Try | ||
|
||
trait JsonBodyHandler[I] extends BodyHandler[I] {} | ||
|
||
object JsonBodyHandler { | ||
|
||
inline def of[I](using JsonDecoder[I]): JsonBodyHandler[Try[I]] = | ||
summon[JsonBodyHandler[Try[I]]] | ||
|
||
inline given derived[I](using | ||
decoder: JsonDecoder[I] | ||
): JsonBodyHandler[Try[I]] = { | ||
new JsonBodyHandler[Try[I]] { | ||
override def apply( | ||
responseInfo: HttpResponse.ResponseInfo | ||
): HttpResponse.BodySubscriber[Try[I]] = { | ||
BodySubscribers.mapping( | ||
BodySubscribers.ofString(Charset.defaultCharset()), | ||
str => decoder.decode(str) | ||
) | ||
} | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
friday/src/main/scala/dev/wishingtree/branch/friday/http/JsonBodyPublisher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.wishingtree.branch.friday.http | ||
|
||
import dev.wishingtree.branch.friday.JsonEncoder | ||
|
||
import java.net.http.HttpRequest | ||
import java.net.http.HttpRequest.BodyPublishers | ||
|
||
object JsonBodyPublisher { | ||
|
||
inline def of[I]( | ||
i: I | ||
)(using jsonEncoder: JsonEncoder[I]): HttpRequest.BodyPublisher = { | ||
BodyPublishers.ofString(jsonEncoder.encode(i).toJsonString) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Blammo | ||
|
||
This module contains some helpers around `java.util.logging`. So far, the main thing it has going for it is a | ||
`JsonFormatter` for some structured logging. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Veil | ||
|
||
Veil is a small layer to help with configs and environment variables. | ||
|
||
Veil can load a `.env`, `.env.test`, or `.env.prod` file based on the environment variable `SCALA_ENV` being set to | ||
`DEV`, `TEST`, or `PROD`. Values in this file are loaded into an in-memory map, and you can look up an env variable with | ||
`Veil.get(key: String): Option[String]`. If it's not present in the in-memory map, it will then search Java's | ||
`System.getenv()`. | ||
|
||
There is also a `Config` type-class that helps with loading json from files/resources, and mapping them to a case | ||
class (which presumably is used for configuration). |
15 changes: 15 additions & 0 deletions
15
spider/src/main/scala/dev/wishingtree/branch/spider/client/Client.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.wishingtree.branch.spider.client | ||
|
||
import java.net.http.HttpClient | ||
|
||
object Client { | ||
|
||
def builder: HttpClient.Builder = | ||
HttpClient.newBuilder() | ||
|
||
def build(settings: HttpClient.Builder => HttpClient.Builder*): HttpClient = | ||
settings | ||
.foldLeft(builder)((b, s) => s(b)) | ||
.build() | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
spider/src/main/scala/dev/wishingtree/branch/spider/client/ClientRequest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.wishingtree.branch.spider.client | ||
|
||
import dev.wishingtree.branch.spider.ContentType | ||
import java.net.URI | ||
import java.net.http.HttpRequest | ||
|
||
object ClientRequest { | ||
|
||
extension (sc: StringContext) { | ||
def uri(args: Any*): URI = URI.create(sc.s(args*)) | ||
} | ||
|
||
extension (rb: HttpRequest.Builder) { | ||
def withContentType(contentType: ContentType): HttpRequest.Builder = | ||
rb.setHeader("Content-Type", contentType.content) | ||
} | ||
|
||
def builder(uri: URI): HttpRequest.Builder = | ||
HttpRequest.newBuilder(uri) | ||
|
||
def build( | ||
uri: URI, | ||
settings: HttpRequest.Builder => HttpRequest.Builder* | ||
): HttpRequest = { | ||
settings | ||
.foldLeft( | ||
builder(uri) | ||
)((b, s) => s(b)) | ||
.build() | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
...ngtree/branch/spider/ContextHandler.scala → ...branch/spider/server/ContextHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
...shingtree/branch/spider/FileHandler.scala → ...ee/branch/spider/server/FileHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ngtree/branch/spider/OpaqueSegments.scala → ...branch/spider/server/OpaqueSegments.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...v/wishingtree/branch/spider/Request.scala → ...ngtree/branch/spider/server/Request.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ngtree/branch/spider/RequestHandler.scala → ...branch/spider/server/RequestHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
.../wishingtree/branch/spider/Response.scala → ...gtree/branch/spider/server/Response.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...v/wishingtree/branch/spider/HttpApp.scala → ...tree/branch/spider/server/SpiderApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spider/src/test/scala/dev/wishingtree/branch/spider/PathsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
spider/src/test/scala/dev/wishingtree/branch/spider/RequestHandlerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters