elixir

How to configure Elixir, NGINX, Websockets on server

我与影子孤独终老i 提交于 2019-12-10 10:23:30
问题 I'm setting up a server with a Phoenix app that will use websockets. Locally websocket work but I have problems with setting it up on my staging server. Can someone help me with setting up websockets on my server. I have nginx configured like this: map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream my_app { server 0.0.0.0:6443; } server { listen 80; listen 443; server_name example.com; ssl on; ssl_certificate /path/to/wildcard.crt; ssl_certificate_key /path/to

HMAC, Elixir, Plug.Conn (trying to call read_body more than once)

六月ゝ 毕业季﹏ 提交于 2019-12-10 09:44:46
问题 I'm struggling with an issue where something is reading the body of an http request before Plug.Parsers.JSON gets it in the pipeline. Because of this, read_body in the plug for json times out--you can't read the body twice. We have an HMAC implementation in an earlier plug in our pipeline and it reads the body in some cases. Is there a pattern for how use of the body is to behave in Plug? I mean, if we can only read it once, and it has to be decoded in Plug.Parsers.JSON, well...it's not going

How to process events in batches with elixir flow

馋奶兔 提交于 2019-12-10 09:21:16
问题 I have a csv_file in which a.) first, each rows need to converted to xml and b.) second, converted xml will be send to rails side for some database write operation. Below is my Flow code for the same. flow = csv_rows |> Flow.from_enumerable() |> Flow.partition |> Flow.map(&(CSV.generate_xml(&1))) |> Flow.map(&(CSV.save_to_rails_databse(&1))) |> Flow.run Everyting is working fine for the small csv file, but when the csv_file is very large(suppose 20,000) records, then performing the second

Default datetime with Ecto & Elixir

最后都变了- 提交于 2019-12-10 03:54:31
问题 I've just started working Elixir & Phoenix today, i am trying to add Ecto as a mapper, but i'm having some trouble using time. This is my model. schema "users" do field :name, :string field :email, :string field :created_at, :datetime, default: Ecto.DateTime.local field :updated_at, :datetime, default: Ecto.DateTime.local end I'm trying to set the created_at and updated_at per default, but when i try to compile this, i get the following error. == Compilation error on file web/models/user.ex =

cannot invoke remote function inside match : Foreach loop

社会主义新天地 提交于 2019-12-10 03:31:18
问题 I'm trying to set some property of User model inside a for-each loop, But I keep getting following error cannot invoke remote function x.token/0 inside match (elixir) src/elixir_fn.erl:9: anonymous fn/3 in :elixir_fn.translate/3 (stdlib) lists.erl:1353: :lists.mapfoldl/3 (elixir) src/elixir_fn.erl:14: :elixir_fn.translate/3 Method: Enum.each(users, fn(user) -> user.token = Comeonin.Bcrypt.hashpwsalt(to_string(user.id)) end) 回答1: There are a few issues here. The = operator is the match

Chunking list based on struct type changing

♀尐吖头ヾ 提交于 2019-12-10 03:22:43
问题 I have a list I want to chunk up based on a transition from struct type B to A. So for example, I have the following: iex(1)> defmodule A, do: defstruct [] {:module, A ... iex(2)> defmodule B, do: defstruct [] {:module, B ... iex(3)> values = [ %A{}, %A{}, %B{}, %B{}, %B{}, %A{}, %A{}, %B{} ] [%A{}, %A{}, %B{}, %B{}, %B{}, %A{}, %A{}, %B{}] I want to have that data chunked up into a 2-element list containing: [ [ %A{}, %A{}, %B{}, %B{}, %B{} ], [ %A{}, %A{}, %B{} ] ] If the input were to be

Elixir - Convert float to string

谁都会走 提交于 2019-12-10 02:38:09
问题 I am trying to figure out how to convert a float to a string/binary but seems like its not as easy as it looks iex(1)> to_string(1200.00) "1.2e3" iex(2)> Float.to_string(1200.00) "1.2e3" We need "1200.00" to come out...just not in the exponent notation 回答1: Without further details about your usecase, this will give you your desired result: iex(1)> Float.to_string(1200.00, decimals: 2) "1200.00" It is using erlang's float_to_binary/2 and will be deprecated in elixir 1.4 (https://github.com

Using Erlang library with Elixir

我是研究僧i 提交于 2019-12-10 02:17:17
问题 I'm having a small issue trying to use a Erlang library within an Elixir project. The library in question is the erl8583 for ISO-8583 message packing and unpacking. I found a github repository for erl8583 , and adjusted my mix.exs to the following: defmodule Iso.Mixfile do use Mix.Project def project do [app: :iso, version: "0.0.1", elixir: "~> 1.0", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] end def application do [applications: [:logger]] end defp deps

How do you embed double-quotes an Elixir string?

风流意气都作罢 提交于 2019-12-10 01:48:04
问题 I have a sting that has embedded " : tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture"> how can i present such a string as a value in Elixir? for example: iex> s= "tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">" Using ~s and ~S did not help iex(20)> s=~S("tx <iq id="wUcdTMYuYoo41" to="2348138248411@" type="set" xmlns="w:profile:picture">") ** (SyntaxError) iex:20: keyword argument must be followed by space after: w: iex(20)

(整理)用Elixir做一个多人扑克游戏 4

情到浓时终转凉″ 提交于 2019-12-09 22:37:46
sockets 和 channels 是Phoenix中用来实现实时效果的两大工具。 Sockets socket是用来连接客户端与服务器的,它使用endpoint来声明: defmodule GenPoker.Endpoint do use Phoenix.Endpoint, otp_app: :gen_poker socket "/socket", GenPoker.PlayerSocket end Channels 客户端只有加入了channel之后才能发送消息。 defmodule GenPoker.PlayerSocket do use Phoenix.Socket channel "tables:*", GenPoker.TableChannel end 创建socket defmodule GenPoker.PlayerSocket do use Phoenix.Socket transport :websocket, Phoenix.Transports.WebSocket def connect(%{"playerId" => player_id}, socket) do {:ok, assign(socket, :player_id, player_id)} end def id(socket) do "players_socket:#{socket