Elixir的Library与OTP Application的关系

所有的library必须是独立的OTP application.

1
2
3
4
5
6
7
8
9
10
11
12
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:comeonin, "~> 2.5"},
{:guardian, "~> 0.14"},
{:cowboy, "~> 1.0"}]
end

application的依赖写在dpes中,如果该依赖只存在编译的时候,那往往就足够了,如果运行时需要此依赖,则必须在 application 函数中列出。

1
2
3
4
5
def application do
[mod: {Til, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :comeonin]]
end

application 函数中, mod 是一个callback。这个callback需要我们在指定的module中给出实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule Til do
use Application

# See https://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec

# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Til.Repo, []),
# Start the endpoint when the application starts
supervisor(Til.Endpoint, []),
# Start your own worker by calling: Til.Worker.start_link(arg1, arg2, arg3)
# worker(Til.Worker, [arg1, arg2, arg3]),
]

# See https://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Til.Supervisor]
Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
Til.Endpoint.config_change(changed, removed)
:ok
end
end

这里主要就是建立本应用的supervisor tree。

基于Alamofire 和 Carrierwave 的图片上传 && Ecto的insert conflict

基于Alamofire 和 Carrierwave 的图片上传

此文备忘如何使用Alamofire与Rails后台的图片上传API交互。

思路

谈不上思路。注意前端后端传递的内容要一致。

前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
static func upload(image: UIImage, progressHandler:((Double) -> Void)?, responseHandler: (() -> Void)?, errorHandler: (() -> Void)?) {
if !User.isLogin() { return }
let headers: HTTPHeaders = ["Authorization": "Bearer \(User.token)"]

print("POST: \(imagesUploadPath)")
print("HEADER: \(headers)")

Alamofire.upload(multipartFormData: { multipartFormData in
if let data = UIImagePNGRepresentation(image) {
multipartFormData.append(
data,
withName: "images[]",
fileName: "image.png",
mimeType: "image/png"
)

} else if let data = UIImageJPEGRepresentation(image, 0.5) {
multipartFormData.append(
data,
withName: "images[]",
fileName: "image.jpg",
mimeType: "image/jpeg"
)
}

},
to: imagesUploadPath,
method: .post,
headers: headers) { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress { progress in
print("progress: \(progress.fractionCompleted)")
progressHandler?(progress.fractionCompleted)
}.responseJSON { response in
debugPrint(response)
responseHandler?()
}
case .failure(let encodingError):
print(encodingError)
errorHandler?()
}
}
}

注意一般后端会parse文件名之类的meta信息,所以前端传递数据的时候需要指定一个文件名。

后端

按照Carrierwaver的流程建立uploader。
但是由于前端传递的是base64的string,所以后端需要把base64 string还原成文件。
很幸运我们可以直接使用carrierwave-base64这个gem轻松完成。

ecto insert_all on conflict

https://dev.classmethod.jp/server-side/db/postgresql-9-5-new-function-upsert-use/

https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert_all/3