我现在被困了几个小时,试图弄清楚如何在没有任何其他框架的情况下配置Plug.Static (Phoenix,Sugar,...); 只是牛仔,插头和Elixir.我只是不知道如何把东西放在路由器里.
plug :match
plug Plug.Static, at: "/pub", from: :cerber
plug :dispatch
get "/" do
Logger.info "GET /"
send_resp(conn, 200, "Hello world\n")
end
Run Code Online (Sandbox Code Playgroud)
Plug.Static在正确的地方吗?不应该之后plug :dispatch吗?index.html?index.html应该位于何处我迷失了......
感谢您的反馈
有没有办法在不阻塞事件循环的情况下等待未来完成?
查询 Mongo 的用例示例:
Future<Result> dbFut = Future.future();
mongo.findOne("myusers", myQuery, new JsonObject(), res -> {
if(res.succeeded()) {
...
dbFut.complete(res.result());
}
else {
...
dbFut.fail(res.cause());
}
}
});
// Here I need the result of the DB query
if(dbFut.succeeded()) {
doSomethingWith(dbFut.result());
}
else {
error();
}
Run Code Online (Sandbox Code Playgroud)
我知道doSomethingWith(dbFut.result());可以移动到处理程序,但如果它很长,代码将变得不可读(回调地狱?)这是正确的解决方案吗?这是没有额外库的万能解决方案吗?
我知道 rxJava 简化了代码,但我不知道,学习 Vert.x和rxJava 太多了。
我也想试一试vertx-sync。我把依赖放在pom.xml; 一切都下载正常,但是当我启动我的应用程序时,出现以下错误
maurice@mickey> java \
-javaagent:~/.m2/repository/co/paralleluniverse/quasar-core/0.7.5/quasar-core-0.7.5-jdk8.jar \
-jar target/app-dev-0.1-fat.jar \
-conf conf/config.json
Error opening zip file or JAR manifest missing : …Run Code Online (Sandbox Code Playgroud) 简而言之,为什么return在Elixir和某些(大多数?全部?)功能语言中没有声明?
为了使它更长一点,我遇到了在Java,Groovy,C,Ruby中的情况……我会写一些类似的东西
def func (some, parameters) {
# say some error
if(condition 1) {
return -1
}
# execute some function and return from here the result
if(condition 2) {
return process1(params)
}
# execute 'default' behavior when no condition matches and return result
process2(params)
}
Run Code Online (Sandbox Code Playgroud)
当然,这可以写为nested,if但是如果条件列表很长,则代码将变得不可读。
我在Elixir中阅读了关于Return语句问题的答案:它们清楚地说明了如何实现这种模式,但没有解释没有这种原因的原因return。
我相信拥有return会打破某些FP概念,但我想知道哪个以及为什么。
Elixir中是否有类似于tracelisp中的宏,它显示了函数调用的输入和返回值.摘自Common Lisp HyperSpec 网站:
(defun fact (n) (if (zerop n) 1 (* n (fact (- n 1)))))
=> FACT
(trace fact)
=> (FACT)
;; Of course, the format of traced output is implementation-dependent.
(fact 3)
>> 1 Enter FACT 3
>> | 2 Enter FACT 2
>> | 3 Enter FACT 1
>> | | 4 Enter FACT 0
>> | | 4 Exit FACT 1
>> | 3 Exit FACT 1
>> | 2 Exit FACT …Run Code Online (Sandbox Code Playgroud) 我正在 Elixir/Phoenix 中实现后端 API。出于安全原因,我们决定在 URL 上显示尽可能少的内容,以便将要执行的操作嵌入到 POST 请求的 JSON 负载中。例如,我们将使用以下有效负载https://my.server.com/api/get_user/42发出 POST ,而不是使用类似 REST 的请求:https://my.server.com/api/
{
"action" : "get_user",
"params" : {
"id" : 42
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的路由器看起来像这样:
scope "/api", AlaaarmWeb do
pipe_through :api
post "/", ApiController, :process
match :*, "/*path", ApiController, :error_404
end
Run Code Online (Sandbox Code Playgroud)
我有一堆process在地图上进行模式匹配的函数params:
def process(conn, params = %{"action" => "get_user"}) do
# ... get the user from the DB in resp, and sed back
json(conn, resp)
end
Run Code Online (Sandbox Code Playgroud)
它工作正常,但代码不是很清楚:一个大文件,使用相同的控制器, ApiController而有一个UserApiController用于管理用户,一个 …
我一直在Elixir中尝试一些简单的键盘IO,主要是在IO模块中.IO.puts在最基本的形式中它非常方便,它会打印一个提示并等待输入字符串.
我遇到了一些奇怪的行为; "奇怪",因为我无法弄清楚究竟发生了什么.以下是iex会议以及评论和问题:
iex(85)> IO.gets "Your name: "
Your name: polo
"polo\n"
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好
iex(86)> IO.gets "Your name: " |> String.trim
Your name:polo
"polo\n"
Run Code Online (Sandbox Code Playgroud)
第一个通知,polo不再像以前那样用提示中的空格分隔.此外,该trim函数不适用,因为我得到\n字符串的末尾.
现在,如果我输入:
iex(87)> String.trim(IO.gets "Your name: ")
Your name: polo
"polo"
Run Code Online (Sandbox Code Playgroud)
一切似乎都回到正常行为:提示中的空间在这里,输入按预期修剪.
最后我还记得读一本书(或者是博客?)两种语法之间有一些细微的差别,所以我试过:
iex(88)> IO.gets("Your name: ") |> String.trim
Your name: polo
"polo"
Run Code Online (Sandbox Code Playgroud)
和宾果游戏!
所以,基本上问题是,和之间有什么区别IO.gets "Your name: " |> String.trimIO.gets("Your name: ") |> String.trim?
我有这种让我发疯的奇怪行为!我有以下插件,用于检查 API 客户端是否在x-api-key标头中实际提供了正确的 API 密钥:
defmodule APIServer.APIKeyAuthPlug do
# import Plug.Conn
require Logger
alias APIServer.Util
def init(opts) do
{:ok, options} = opts[:auth]
apikey = options[:apikey]
Logger.debug("API key is #{inspect(apikey)}")
apikey
end
def call(conn, key) do
conn_key = Plug.Conn.get_req_header(conn, "x-api-key") |> hd
Logger.debug("Registered key #{inspect(key)}")
# rest of the call function
end
end
Run Code Online (Sandbox Code Playgroud)
插件插入路由器中的插件链中:
plug Plug.Logger, log: :info
plug APIKeyAuthPlug,
auth: Application.fetch_env(:probe_server, APIServer.APIKeyAuthPlug)
plug(:match)
plug(:dispatch)
Run Code Online (Sandbox Code Playgroud)
正确的密钥本身被定义为环境变量并被读入config.exs:
config :api_server, APIServer.APIKeyAuthPlug,
apikey: System.get_env("API_SERVER_APIKEY") || "XYZ"
Run Code Online (Sandbox Code Playgroud)
现在,当我编译代码,行Logger.debug("API key is …
好吧,标题说明了一切:我想知道 BEAM 不垃圾收集原子的原因是什么。我知道问题How Erlangatoms can be Garbage Collected ,但是,虽然相关,但它没有回答Why。
我试图了解如何正确定义具有多个子句和可选参数的函数。当我写作时
def sum([], total \\ 0), do: total
def sum([h|t], total), do: h + sum(t, total)
Run Code Online (Sandbox Code Playgroud)
我收到警告:
warning: def sum/2 has multiple clauses and also declares default values.
In such cases, the default values should be defined in a header. Instead of:
def foo(:first_clause, b \\ :default) do ... end
def foo(:second_clause, b) do ... end
one should write:
def foo(a, b \\ :default)
def foo(:first_clause, b) do ... end
def foo(:second_clause, b) do ... end …Run Code Online (Sandbox Code Playgroud)