Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hayesgm/plug_protobufs
A Plug Parser for Protobufs
https://github.com/hayesgm/plug_protobufs
Last synced: about 1 month ago
JSON representation
A Plug Parser for Protobufs
- Host: GitHub
- URL: https://github.com/hayesgm/plug_protobufs
- Owner: hayesgm
- License: mit
- Created: 2017-01-18T09:11:16.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-10T23:38:26.000Z (over 5 years ago)
- Last Synced: 2024-09-19T11:19:54.369Z (about 2 months ago)
- Language: Elixir
- Size: 13.7 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Plug Protobufs
A plug parser for Protobufs input. This creates a simple and easy way to accept Protobufs as the input to your plug projects.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:
1. Add `plug_protobufs` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:plug_protobufs, "~> 0.1.4"}]
end
```2. Add `Plug.Parsers.Protobuf` to your Plug parsers:
```elixir
config :plug, :parsers, [Plug.Parsers.Protobuf]
```or add the plug yourself:
```elixir
plug Plug.Parsers,
parsers: [Plug.Parsers.Protobuf]
```3. Define your Protobufs:
```elixir
defmodule Requests do
use Protobufs, """
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 1;
}
"""
end
```3. In your route handler, add:
```elixir
defmodule AppRouter do
use Plug.Routerget "/hello", private: %{protobuf: Requests.HelloRequest} do
send_resp(conn, 200, "hello #{conn.params["_protobuf"].name}")
end
end
```or, if you want to response in protobufs:
```elixir
defmodule AppRouter do
use Plug.Routerget "/hello", private: %{protobuf: Requests.HelloRequest} do
name = conn.params["_protobuf"].nameconn
|> put_resp_content_type("application/x-protobuf")
|> send_resp(200, Requests.HelloResponse.new(greeting: "Hello #{name}"))
end
end
```