server.jl 822 B

12345678910111213141516171819202122232425262728
  1. using Pkg
  2. Pkg.activate(@__DIR__)
  3. using HTTP
  4. import JSON3
  5. using Dates
  6. @info "starting listener"
  7. HTTP.listen("0.0.0.0", 8080, reuseaddr=true) do http
  8. HTTP.setheader(http, "Server" => "Julia-HTTP")
  9. HTTP.setheader(http, "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT")
  10. if endswith(http.message.target, "/plaintext")
  11. HTTP.setheader(http, "Content-Type" => "text/plain")
  12. HTTP.setstatus(http, 200)
  13. startwrite(http)
  14. write(http, "Hello, World!")
  15. elseif endswith(http.message.target, "/json")
  16. HTTP.setheader(http, "Content-Type" => "application/json")
  17. HTTP.setstatus(http, 200)
  18. startwrite(http)
  19. JSON3.write(http, (;message = "Hello, World!"))
  20. else
  21. HTTP.setstatus(http, 404)
  22. startwrite(http)
  23. write(http, "Not Found")
  24. end
  25. end