defmodule MyProject.WWW do
use Raxx.Server
def handle_headers(%Raxx.Request{method: :GET, path: []}, greeting) do
Raxx.response(:ok)
|> Raxx.set_header("content-type", "text/plain")
|> Raxx.set_body(greeting)
end
end
Added is the Ace.HTTP.Service. This is the general purpose way start a server, currently supports HTTP/1 but will serve the same content over HTTP/1 and HTTP/2 in next releases
This functionality obsoletes the ace_http previously for serving content over HTTP/1
Application layer protocol notification(ALPN) support now means that a single endpoint will serve content over HTTP/1 or HTTP/2 dependant on the client. This was the final step to make Ace useful to most web applications. Test it out by using Ace.HTTP.Service.
Ace 0.15.4 Uses latest version of server interface with better support for streaming.
defmodule Upload do
use Raxx.Server
@impl Raxx.Server
def handle_head(%{method: :PUT, path: ["upload"] body: true}, _state) do
{:ok, io_device} = File.open("my/path")
{[], {:file, device}}
end
@impl Raxx.Server
def handle_data(data, state = {:file, device}) do
IO.write(device, data)
{[], state}
end
@impl Raxx.Server
def handle_tail(_trailers, state) do
response(:see_other)
|> set_header("location", "/")
end
end
Access to underlying connection information added 0.16.1
For both HTTP/1 and HTTP/2 it is now possible to access connection information from a worker Process.get(Ace.HTTP.Connection). This allows inspection and verification of peer certificate. For example allowing a pure Elixir implementation of GRPC.
Better errors and child_specs added in 0.16.4 to help get started.
1. Callback errors
Raxx.ReturnError added to help debug callbacks. Now if a callback returns an invalid value the following error will be raise.
** (ReturnError) Invalid reaction from server module. Response must be complete or include update server state
e.g.
# Complete
Raxx.response(:ok)
|> Raxx.set_body("Hello, World!")
# New server state
response = Raxx.response(:ok)
|> Raxx.set_body(true)
{[response], new_state}
Actual value returned was
:ok
2. Server child_specs
If a module using Ace.HTTP.Service defines all the required server options it can be added to a supervision tree using a simpler child spec.
# lib/my_app/www.ex
defmodule MyApp.WWW do
use Ace.HTTP.Service, [port: 8080, cleartext: true]
end
# lib/my_app/application.ex
children = [
{MyApp.WWW, [%{config: foo}]}
# or
MyApp.WWW
]
0.18.0 release to separate streaming from simple interface.
Ace HTTP service must now explicitly implement Raxx.SimpleServer or Raxx.Server.
e.g.
defmodule MyApp.WWW do
use Ace.HTTP.Service, port: 8080, cleartext: true
use Raxx.SimpleServer
@impl Raxx.SimpleServer
def handle_request(_,_) do
response(:ok)
end
end
This changes are to facilitate adding middleware to Raxx.