Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Cleanup repo #42

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build and test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:
name: Build and test (Elixir ${{matrix.versions.elixir}})
strategy:
matrix:
versions: [{os: 'ubuntu-20.04', otp: '23.3.4.9', elixir: '1.13.4-otp-23'}, {os: 'ubuntu-22.04', otp: '25.3.2.9', elixir: '1.15.7-otp-25'}]
runs-on: ${{matrix.versions.os}}

steps:
- uses: actions/checkout@v4

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: ${{matrix.versions.elixir}}
otp-version: ${{matrix.versions.otp}}

# Define how to cache deps. Restores existing cache if present.
- name: Cache deps
id: cache-deps
uses: actions/cache@v3
env:
cache-name: cache-elixir-deps
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.cache-name }}-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-${{env.cache-name}}

# Define how to cache the `_build` directory. After the first run,
# this speeds up tests runs a lot. This includes not re-compiling our
# project's downloaded deps every run.
- name: Cache compiled build
id: cache-build
uses: actions/cache@v3
env:
cache-name: cache-compiled-build
with:
path: _build
key: ${{ runner.os }}-mix-${{ env.cache-name }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{ env.cache-name }}-
${{ runner.os }}-mix-

- name: Install dependencies
run: mix deps.get

- name: Run tests
run: mix test
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Ewebmachine [![Build Status](https://github.com/kbrw/ewebmachine/actions/workflows/.github/workflows/build-and-test.yml/badge.svg)](https://github.com/kbrw/ewebmachine/actions/workflows/build-and-test.yml) [![Hex.pm](https://img.shields.io/hexpm/v/ewebmachine.svg)](https://hex.pm/packages/ewebmachine) [![Documentation](https://img.shields.io/badge/documentation-gray)](https://hexdocs.pm/ewebmachine) ![Hex.pm License](https://img.shields.io/hexpm/l/ewebmachine)

Ewebmachine is a full rewrite with clean DSL and plug integration
based on Webmachine from basho. This version is not backward compatible with
the previous one that was only a thin wrapper around webmachine, use the branch
1.0-legacy to use the old one.

[![Build Status](https://travis-ci.org/kbrw/ewebmachine.svg?branch=master)](https://travis-ci.org/kbrw/ewebmachine)

See the [generated documentation](http://hexdocs.pm/ewebmachine) for more detailed explanations.

The principle is to go through the [HTTP decision tree](https://raw.githubusercontent.com/kbrw/ewebmachine/master/doc/http_diagram.png)
and make decisions according to response of some callbacks called "handlers".

Expand All @@ -28,7 +26,7 @@ To do that, the library gives you 5 plugs and 2 plug pipeline builders :
define at the same time an `Ewebmachine.Builder.Handlers` and the
matching spec to use it, and a plug `:resource_match` to do the
match and execute the associated plug. The macro `resources_plugs` helps you
to define commong plug pipeling
to define common plug pipeline

## Example usage

Expand Down Expand Up @@ -64,14 +62,18 @@ defmodule FullApi do

resource "/hello/:name" do %{name: name} after
content_types_provided do: ['application/xml': :to_xml]
defh to_xml, do: "<Person><name>#{state.name}</name>"
defh to_xml, do: "<Person><name>#{state.name}</name></Person>"
end

resource "/hello/json/:name" do %{name: name} after
plug MyJSONApi #this is also a plug pipeline
allowed_methods do: ["GET","DELETE"]
resource_exists do: pass((user=DB.get(state.name)) !== nil, json_obj: user)
delete_resource do: DB.delete(state.name)

defh resource_exists do
user = DB.get(state.name)
pass(user !== nil, json_obj: user)
end
end

resource "/static/*path" do %{path: Enum.join(path,"/")} after
Expand Down Expand Up @@ -105,7 +107,7 @@ defmodule MyApp do
use Application
def start(_type, _args), do:
Supervisor.start_link([
Plug.Adapters.Cowboy.child_spec(:http,FullApi,[], port: 4000)
Plug.Cowboy.child_spec(:http,FullApi,[], port: 4000)
], strategy: :one_for_one)
end
```
Expand All @@ -117,7 +119,7 @@ def application do
[applications: [:logger,:ewebmachine,:cowboy], mod: {MyApp,[]}]
end
defp deps, do:
[{:ewebmachine, "2.0.0"}, {:cowboy, "~> 1.0"}]
[{:ewebmachine, "2.3.2"}, {:cowboy, "~> 1.0"}]
```

# CONTRIBUTING
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
Expand Down
17 changes: 12 additions & 5 deletions examples/hello/lib/hello.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ defmodule Hello do

def start(_type, _args) do
Supervisor.start_link([
Plug.Adapters.Cowboy2.child_spec(scheme: :http, plug: Hello.Api, options: [port: 4000]),
Supervisor.Spec.worker(Hello.Db, [])
Plug.Cowboy.child_spec(scheme: :http, plug: Hello.Api, options: [port: 4000]),
Hello.Db
], strategy: :one_for_one)
end
end

defmodule Db do
def child_spec(_), do: %{
id: __MODULE__,
start: {__MODULE__, :start_link, []},
type: :worker,
restart: :permanent,
shutdown: 500
}
def start_link, do: Agent.start_link(&Map.new/0, name: __MODULE__)
def get(id), do: Agent.get(__MODULE__, &(Map.get(&1, id, nil)))
def put(id, val), do: Agent.update(__MODULE__, &(Map.put(&1, id, val)))
Expand Down Expand Up @@ -38,7 +45,7 @@ defmodule Hello do

resource "/hello/:name" do %{name: name} after
content_types_provided do: ['application/xml': :to_xml]
defh to_xml(conn, state), do: {"<Person><name>#{state.name}</name>", conn, state}
defh to_xml(conn, state), do: {"<Person><name>#{state.name}</name></Person>", conn, state}
end

resource "/hello/json/:name" do %{name: name} after
Expand Down Expand Up @@ -100,8 +107,8 @@ defmodule Hello do

resource "/static/*path" do %{path: Enum.join(path, "/")} after
resource_exists do: File.regular?(path(state.path))
content_types_provided do: [ {state.path |> Plug.MIME.path() |> default_plain, :to_content} ]
content_types_provided do: [ {state.path |> MIME.from_path() |> default_plain, :to_content} ]

defh to_content(conn, state) do
body = File.stream!( path(state.path), [], 300_000_000)
{body, conn, state}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Hello.Mixfile do

def application do
[
applications: [:logger, :ewebmachine, :cowboy],
applications: [:logger, :ewebmachine, :cowboy, :poison],
mod: {Hello.App, []}
]
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Ewebmachine.Mixfile do
[
app: :ewebmachine,
version: version(),
elixir: ">= 1.10.0",
elixir: ">= 1.13.4",
docs: docs(),
deps: deps(),
description: @description,
Expand Down Expand Up @@ -46,7 +46,7 @@ defmodule Ewebmachine.Mixfile do
defp deps do
[
{:plug, "~> 1.10"},
{:plug_cowboy, "~> 2.4", optional: true},
{:plug_cowboy, "~> 2.4"},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:dialyxir, "~> 0.5", only: [:dev], runtime: false}
]
Expand Down
Loading