Skip to content

Commit

Permalink
Merge pull request #25 from ospaarmann/downgrade_poison
Browse files Browse the repository at this point in the history
Downgrade poison and test against Dgraph v1.0.13
  • Loading branch information
ospaarmann committed Mar 28, 2019
2 parents 53cf4a7 + dc5f249 commit 1e14a3b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 134 deletions.
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.2.0-beta.2] - 2019-03-28

- Downgrade poison to `v3.1.0`.
- Test against Dgraph `v1.0.13`
- Set the GRPC deadline and `DBConnection` checkout timeout to use `:timeout` (contribution by @emhagman).
- Update dependency `uuid` to `elixir_uuid` (contribution by @emhagman)
- Some smaller tweaks (contribution by @optikfluffel).
- Update the docs.

## [0.2.0-beta.1] - 2019-01-22

- Handle `:gun_up` and `:gun_down` messages as debug level for now.
Expand Down
71 changes: 9 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**ExDgraph is functional but I would be careful using it in production. If you want to help, please drop me a message. Any help is greatly appreciated!**

ExDgraph is a gRPC based client for the [Dgraph](https://github.com/dgraph-io/dgraph) database. It uses the [DBConnection](https://hexdocs.pm/db_connection/DBConnection.html) behaviour to support transactions and connection pooling via [Poolboy](https://github.com/devinus/poolboy). Works with Dgraph v1.0.11 (latest).
ExDgraph is a gRPC based client for the [Dgraph](https://github.com/dgraph-io/dgraph) database. It uses the [DBConnection](https://hexdocs.pm/db_connection/DBConnection.html) behaviour to support transactions and connection pooling via [Poolboy](https://github.com/devinus/poolboy). Works with Dgraph v1.0.13 (latest).

> Dgraph is an open source, horizontally scalable and distributed graph database, providing ACID transactions, consistent replication and linearizable reads. [...] Dgraph's goal is to provide Google production level scale and throughput, with low enough latency to be serving real time user queries, over terabytes of structured data. ([Source](https://github.com/dgraph-io/dgraph))
Expand All @@ -27,7 +27,7 @@ Add the package `ex_dgraph` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ex_dgraph, "~> 0.2.0-beta.1"}
{:ex_dgraph, "~> 0.2.0-beta.2"}
]
end
```
Expand Down Expand Up @@ -82,13 +82,13 @@ def start(_type, _args) do
end
```

**Important:** Please also note the instructions further down on how to run ExDgraph with Phoenix 1.3. It requires Cowboy 2 which means you have to change some things.

## Usage

Again, this is work in progress. I'll add more examples on how to use this on the go. So far you can connect to a server and run a simple query. I recommend installing and running Dgraph locally with Docker. You find information on how to do that [here](https://docs.dgraph.io/get-started/#from-docker-image). To use this simple example you first have to [import the example data](https://docs.dgraph.io/get-started/#step-3-run-queries). You can just open [http://localhost:8000](http://localhost:8000) in your browser when Dgraph is running to execute and visualize queries using Ratel.
I recommend installing and running Dgraph locally with Docker. You find information on how to do that [here](https://docs.dgraph.io/get-started/#from-docker-image). To use this simple example you first have to [import the example data](https://docs.dgraph.io/get-started/#step-3-run-queries). You can just open [http://localhost:8000](http://localhost:8000) in your browser when Dgraph is running to execute and visualize queries using Ratel.

At the moment simple queries, mutations and operations are supported. And to make things easier ExDgraph returns an Elixir map and you can also just insert a map. This allows you to insert a complex dataset with vertices and edges in one call. To make things even better: If there is a `uid` present anywhere in the map the record isn't inserted but updated. This way you can update and add records in one go.

At the moment simple queries, mutations and operations are supported via the DBConnection behaviour. Everything else is done directly via the Protobuf API. This will change. Check the tests for examples.
Also check the tests for more examples.

**Example for a query**

Expand Down Expand Up @@ -266,6 +266,8 @@ ExDgraph.operation(conn, %{schema: @testing_schema})

**Example for a raw query**

This is an example on how to use the Protobuf API with GRPC directly and how to extend the library.

```elixir
# Connect to Server
{:ok, channel} = GRPC.Stub.connect("#localhost:9080")
Expand Down Expand Up @@ -296,63 +298,8 @@ request = ExDgraph.Api.Request.new(query: query)
json = Poison.decode!(msg.json)
```

## Using with Phoenix 1.3
Since grpc-elixir needs Cowboy 2 you need to upgrade your Phoenix app to work with Cowboy 2.

**Update dependencies in your mix.exs**

Open up the mix.exs file and replace the dependencies with this:

```elixir
defp deps do
[
{:phoenix, git: "https://github.com/phoenixframework/phoenix", branch: "master", override: true},
{:plug, git: "https://github.com/elixir-plug/plug", branch: "master", override: true},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 2.1", override: true},
{:ex_dgraph, github: "ospaarmann/exdgraph", tag: "v0.1.0"}
]
end
```

Run `mix deps.get` to retrieve the new dependencies.

**Create a self-signed certificate for https**

We will need to do this as, although http2 does not specifically require it, browser do expect http2 connections to be secured over TLS.

In your project folder run:

```
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
```

Add the two generated files to .gitignore.

Now we will need to adjust the Endpoint configuration to use a secure connection and use a Cowboy 2 handler

Replace the configuration with the following.

```elixir
config :my_app, MyAppWeb.Endpoint,
debug_errors: true,
handler: Phoenix.Endpoint.Cowboy2Handler,
code_reloader: true,
check_origin: false,
watchers: [],
https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"]
```

This tells phoenix to listen on port 4000 with the just generated certificate. Furthermore, the handler tells Phoenix to use Cowboy2.

If you now start the application with `mix phx.server` and go to https://localhost:4000 the browser will tell that the connection is not secure with for example NET::ERR_CERT_AUTHORITY_INVALID. This is because the certificate is self signed, and not by a certificate authority. You can open the certificate in for example Keychain on Mac OS X and tell your OS to trust the certificate.

*[Source](https://maartenvanvliet.nl/2017/12/15/upgrading_phoenix_to_http2/)*

## Using SSL

If you want to connect to Dgraph using SSL you have to set the `:ssl` config to `true` and provide a certificate:

```elixir
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.2"
services:
zero:
image: dgraph/dgraph:v1.0.11
image: dgraph/dgraph:v1.0.13
volumes:
- type: bind
source: ./dgraph_data
Expand All @@ -14,7 +14,7 @@ services:
restart: on-failure
command: dgraph zero --my=zero:5080
server:
image: dgraph/dgraph:v1.0.11
image: dgraph/dgraph:v1.0.13
volumes:
- type: bind
source: ./dgraph_data
Expand All @@ -27,7 +27,7 @@ services:
restart: on-failure
command: dgraph alpha --my=server:7080 --lru_mb=2048 --zero=zero:5080
ratel:
image: dgraph/dgraph:v1.0.11
image: dgraph/dgraph:v1.0.13
volumes:
- type: bind
source: ./dgraph_data
Expand Down
75 changes: 10 additions & 65 deletions lib/exdgraph.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExDgraph do
@moduledoc """
ExDgraph is a gRPC based client for the Dgraph database. It uses the DBConnection behaviour to support transactions and connection pooling via Poolboy. Works with Dgraph v1.0.11 (latest).
ExDgraph is a gRPC based client for the Dgraph database. It uses the DBConnection behaviour to support transactions and connection pooling via Poolboy. Works with Dgraph v1.0.13 (latest).
## Installation
Expand All @@ -9,7 +9,7 @@ defmodule ExDgraph do
```elixir
def deps do
[
{:ex_dgraph, "~> 0.2.0-beta.1"}
{:ex_dgraph, "~> 0.2.0-beta.2"}
]
end
```
Expand Down Expand Up @@ -84,13 +84,13 @@ defmodule ExDgraph do
end
```
**Important:** Please also note the instructions further down on how to run ExDgraph with Phoenix 1.3. It requires Cowboy 2 which means you have to change some things.
## Usage
Again, this is work in progress. I'll add more examples on how to use this on the go. So far you can connect to a server and run a simple query. I recommend installing and running Dgraph locally with Docker. You find information on how to do that [here](https://docs.dgraph.io/get-started/#from-docker-image). To use this simple example you first have to [import the example data](https://docs.dgraph.io/get-started/#step-3-run-queries). You can just open [http://localhost:8000](http://localhost:8000) in your browser when Dgraph is running to execute and visualize queries using Ratel.
I recommend installing and running Dgraph locally with Docker. You find information on how to do that [here](https://docs.dgraph.io/get-started/#from-docker-image). To use this simple example you first have to [import the example data](https://docs.dgraph.io/get-started/#step-3-run-queries). You can just open [http://localhost:8000](http://localhost:8000) in your browser when Dgraph is running to execute and visualize queries using Ratel.
At the moment simple queries, mutations and operations are supported. And to make things easier ExDgraph returns an Elixir map and you can also just insert a map. This allows you to insert a complex dataset with vertices and edges in one call. To make things even better: If there is a `uid` present anywhere in the map the record isn't inserted but updated. This way you can update and add records in one go.
At the moment simple queries, mutations and operations are supported via the DBConnection behaviour. Everything else is done directly via the Protobuf API. This will change. Check the tests for examples.
Also check the tests for more examples.
### Example for a query
Expand Down Expand Up @@ -268,6 +268,8 @@ defmodule ExDgraph do
### Example for a raw query
This is an example on how to use the Protobuf API with GRPC directly and how to extend the library.
```elixir
# Connect to Server
{:ok, channel} = GRPC.Stub.connect("#localhost:9080")
Expand Down Expand Up @@ -298,63 +300,8 @@ defmodule ExDgraph do
json = Poison.decode!(msg.json)
```
## Using with Phoenix 1.3
Since grpc-elixir needs Cowboy 2 you need to upgrade your Phoenix app to work with Cowboy 2.
### Update dependencies in your mix.exs
Open up the mix.exs file and replace the dependencies with this:
```elixir
defp deps do
[
{:phoenix, git: "https://github.com/phoenixframework/phoenix", branch: "master", override: true},
{:plug, git: "https://github.com/elixir-plug/plug", branch: "master", override: true},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 2.1", override: true},
{:ex_dgraph, "~> 0.1.0", github: "ospaarmann/exdgraph", branch: "master"}
]
end
```
Run `mix deps.get` to retrieve the new dependencies.
### Create a self-signed certificate for https
We will need to do this as, although http2 does not specifically require it, browser do expect http2 connections to be secured over TLS.
In your project folder run:
```
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
```
Add the two generated files to .gitignore.
Now we will need to adjust the Endpoint configuration to use a secure connection and use a Cowboy 2 handler
Replace the configuration with the following.
```elixir
config :my_app, MyAppWeb.Endpoint,
debug_errors: true,
handler: Phoenix.Endpoint.Cowboy2Handler,
code_reloader: true,
check_origin: false,
watchers: [],
https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"]
```
This tells phoenix to listen on port 4000 with the just generated certificate. Furthermore, the handler tells Phoenix to use Cowboy2.
If you now start the application with `mix phx.server` and go to https://localhost:4000 the browser will tell that the connection is not secure with for example NET::ERR_CERT_AUTHORITY_INVALID. This is because the certificate is self signed, and not by a certificate authority. You can open the certificate in for example Keychain on Mac OS X and tell your OS to trust the certificate.
*[Source](https://maartenvanvliet.nl/2017/12/15/upgrading_phoenix_to_http2/)*
## Using SSL
If you want to connect to Dgraph using SSL you have to set the `:ssl` config to `true` and provide a certificate:
```elixir
Expand Down Expand Up @@ -420,9 +367,7 @@ defmodule ExDgraph do
- `:password` - User password;
- `:pool_size` - maximum pool size;
- `:max_overflow` - maximum number of workers created if pool is empty
- `:timeout` - Connect timeout in milliseconds (default: `#{@timeout}`)
Poolboy will block the current process and wait for an available worker,
failing after a timeout, when the pool is full;
- `:timeout` - Connect timeout in milliseconds (default: `#{@timeout}`) for DBConnection and the GRPC client deadline.
- `:pool` - The connection pool. Defaults to `DbConnection.Poolboy`.
- `:ssl` - If to use ssl for the connection (please see configuration example).
If you set this option, you also have to set `cacertfile` to the correct path.
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExDgraph.MixProject do
def project do
[
app: :ex_dgraph,
version: "0.2.0-beta.1",
version: "0.2.0-beta.2",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -46,7 +46,7 @@ defmodule ExDgraph.MixProject do
{:grpc, "~> 0.3.1"},
{:gun, "1.3.0"},
{:protobuf, "~> 0.5"},
{:poison, "~> 4.0.1"},
{:poison, "~> 3.1"},
{:poolboy, "~> 1.5.2"},
{:db_connection, "~> 1.1"},
{:retry, "~> 0.11.2"},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"credo": {:hex, :credo, "1.0.2", "88bc918f215168bf6ce7070610a6173c45c82f32baa08bdfc80bf58df2d103b6", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.18.4", "4406b8891cecf1352f49975c6d554e62e4341ceb41b9338949077b0d4a97b949", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.10.5", "7c912c4ec0715a6013647d835c87cde8154855b9b84e256bc7a63858d5f284e3", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.6", "fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3", [:mix], [], "hexpm"},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], [], "hexpm"},
Expand All @@ -29,7 +29,7 @@
"morphix": {:hex, :morphix, "0.6.0", "78902c71672a5de64759fb5847f9bed708eb82bc0eb017f1e5cd95f93f1a167b", [:mix], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm"},
"protobuf": {:hex, :protobuf, "0.5.4", "2e1b8eec211aff034ad8a14e3674220b0158bfb9a3c7128ac9d2a1ed1b3724d3", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.6.2", "6db93c78f411ee033dbb18ba8234c5574883acb9a75af0fb90a9b82ea46afa00", [:rebar3], [], "hexpm"},
Expand Down

0 comments on commit 1e14a3b

Please sign in to comment.