项目作者: CrowdHailer

项目描述 :
HTTP web server and client, supports http1 and http2
高级语言: Elixir
项目地址: git://github.com/CrowdHailer/Ace.git
创建时间: 2015-08-03T06:49:28Z
项目社区:https://github.com/CrowdHailer/Ace

开源协议:MIT License

下载


Ace

HTTP web server and client, supports http1 and http2

Hex pm
Build Status
License

See Raxx.Kit for a project generator that helps you set up
a web project based on Raxx/Ace.

Get started

Hello, World!

  1. defmodule MyApp do
  2. use Ace.HTTP.Service, port: 8080, cleartext: true
  3. use Raxx.SimpleServer
  4. @impl Raxx.SimpleServer
  5. def handle_request(%{method: :GET, path: []}, %{greeting: greeting}) do
  6. response(:ok)
  7. |> set_header("content-type", "text/plain")
  8. |> set_body("#{greeting}, World!")
  9. end
  10. end

The arguments given to use Ace.HTTP.Service are default values when starting the service.

Start the service

  1. config = %{greeting: "Hello"}
  2. MyApp.start_link(config, port: 1234)

Here the default port value has been overridden at startup

Raxx

Ace implements the Raxx HTTP interface.
This allows applications to be built with any components from the Raxx ecosystem.

Raxx has tooling for streaming, server-push, routing, api documentation and more. See documentation for details.

The correct version of raxx is included with ace, raxx does not need to be added as a dependency.

TLS/SSL

If a service is started without the cleartext it will start using TLS. This requires a certificate and key.

  1. config = %{greeting: "Hello"}
  2. options = [port: 8443, certfile: "path/to/certificate", keyfile: "path/to/key"]
  3. MyApp.start_link(application, options)

TLS is required to serve content via HTTP/2.

Supervising services

The normal way to run services is as part of a projects supervision tree. When starting a new project use the --sup flag.

  1. mix new my_app --sup

Add the services to be supervised in the application file lib/my_app/application.ex.

  1. defmodule MyApp.Application do
  2. @moduledoc false
  3. use Application
  4. def start(_type, _args) do
  5. import Supervisor.Spec, warn: false
  6. children = [
  7. {MyApp, [%{greeting: "Hello"}]}
  8. ]
  9. opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  10. Supervisor.start_link(children, opts)
  11. end
  12. end

Start project using iex -S mix and visit http://localhost:8080.

Testing

  1. mix test --include ci:true

Will run h2spec as one of the tests.

If the h2spec is not specified in the environment variable $H2SPEC_PATH or on the
$PATH, it will be downloaded into test/support/h2spec/ and executed.

Alternative HTTP servers

Other servers you can use for Elixir/erlang applications are Cowboy and Elli.

Of the three Cowboy is the most widely used.
Both Elli and Cowboy are written in erlang, Ace is written in Elixir.

Elixir HTTP Benchmark - Ace vs Cowboy