WebServer.scala 814 B

1234567891011121314151617181920212223242526272829
  1. import org.http4s._
  2. import org.http4s.server._
  3. import org.http4s.dsl._
  4. import org.http4s.argonaut._
  5. import org.http4s.server.blaze.BlazeBuilder
  6. import headers._
  7. import _root_.argonaut._, Argonaut._
  8. object WebServer extends App {
  9. val service = HttpService {
  10. case GET -> Root / "json" =>
  11. val dateHeader = Date(DateTime(4))
  12. Ok(Json("message" -> jString("Hello, World!")).asJson)
  13. .withHeaders(dateHeader)
  14. .withContentType(Some(`Content-Type`(MediaType.`application/json`)))
  15. case GET -> Root / "plaintext" =>
  16. val dateHeader = Date(DateTime(4))
  17. Ok("Hello, World!")
  18. .withHeaders(dateHeader)
  19. .withContentType(Some(`Content-Type`(MediaType.`text/plain`)))
  20. }
  21. BlazeBuilder.bindHttp(8080)
  22. .mountService(service, "/")
  23. .run
  24. .awaitShutdown()
  25. }