Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
223 commits
Select commit Hold shift + click to select a range
14d04ab
build(deps-dev): bump excoveralls from 0.9.2 to 0.10.0
dependabot-support Aug 27, 2018
64b769c
build(deps): bump phoenix_ecto from 3.3.0 to 3.4.0
dependabot-support Sep 10, 2018
ab0c67b
build(deps): bump dataloader from 1.0.3 to 1.0.4
dependabot-support Sep 17, 2018
1287541
build(deps-dev): bump inch_ex from 1.0.0 to 1.0.1
dependabot-support Sep 17, 2018
0da40c2
Merge pull request #73 from coderplanets/dependabot/hex/dataloader-1.0.4
mydearxym Sep 24, 2018
41e5e54
Merge pull request #74 from coderplanets/dependabot/hex/inch_ex-1.0.1
mydearxym Sep 24, 2018
4c05c6c
Merge pull request #70 from coderplanets/dependabot/hex/phoenix_ecto-…
mydearxym Sep 24, 2018
65d2ac2
build(deps): bump guardian from 1.1.0 to 1.1.1
dependabot-support Sep 24, 2018
ed86377
build(deps-dev): bump mix_test_watch from 0.8.0 to 0.9.0
dependabot-support Sep 24, 2018
e2a22ca
build(deps-dev): bump credo from 0.10.0 to 0.10.1
dependabot-support Sep 24, 2018
12fa5f1
Merge pull request #65 from coderplanets/dependabot/hex/guardian-1.1.1
mydearxym Sep 24, 2018
06fd5b7
Merge pull request #66 from coderplanets/dependabot/hex/excoveralls-0…
mydearxym Sep 24, 2018
eb990fd
Merge pull request #71 from coderplanets/dependabot/hex/mix_test_watc…
mydearxym Sep 24, 2018
9ac99e7
Merge pull request #72 from coderplanets/dependabot/hex/credo-0.10.1
mydearxym Sep 24, 2018
302104b
build: production
mydearxym Sep 23, 2018
b0e7063
ci(fix apollo schema check): schema check broken fix
mydearxym Sep 24, 2018
dc46e65
chore(deps): upgrade
mydearxym Sep 24, 2018
b443237
build(postgresql): update to 10
mydearxym Sep 24, 2018
c0c6d4a
ci(postgresql): fallback to 9.4
mydearxym Sep 24, 2018
214a66a
fix(profile test): fix work backgorund test
mydearxym Sep 24, 2018
f8f86aa
build: production
mydearxym Sep 24, 2018
6d39990
fix(cms video): missing field publish_at
mydearxym Sep 25, 2018
6b59ffa
feat(geoinfo): user info geo & community geo
mydearxym Sep 26, 2018
531bdcc
Merge branch 'geo-service' into dev
mydearxym Sep 26, 2018
584fcab
chore(wip clean up): clean up wip
mydearxym Sep 26, 2018
5b635cd
Update README.md
mydearxym Sep 28, 2018
b5d07cf
fix: add missing cms_thread
mydearxym Sep 28, 2018
a32ffdc
refactor(cms.repo): use new repo fields
mydearxym Sep 28, 2018
76ead2f
refactor(repo thread): change fields to match frontend
mydearxym Sep 29, 2018
3af77fc
Merge branch 'repo-refactor' into dev
mydearxym Sep 29, 2018
c932c26
feat(repo thread): debug with frontend, feature is done
mydearxym Sep 29, 2018
c02018c
Merge branch 'repo-refactor' into dev
mydearxym Sep 29, 2018
35cf03b
refactor(comments): replace body:string -> body:text
mydearxym Sep 30, 2018
d98d512
feat(video comments): basic curd comments
mydearxym Sep 30, 2018
94cbaec
feat(video comments): add like/dilike
mydearxym Sep 30, 2018
ae7c30b
feat(repo comment test): done with test cover
mydearxym Sep 30, 2018
e7e010c
Merge branch 'comments-for-other-threads' into dev
mydearxym Sep 30, 2018
be7ee5e
refactor(comments): add a bug tag for spec case
mydearxym Sep 30, 2018
cb895ca
Merge branch 'comments-for-other-threads' into dev
mydearxym Sep 30, 2018
9b5ad22
feat(wiki thread): add wiki with test
mydearxym Sep 30, 2018
bec08ec
feat(wiki thread): add basic wiki query
mydearxym Sep 30, 2018
8fc9f67
feat(wiki thread): add sync content and add contributor
mydearxym Sep 30, 2018
37a2eca
chore(wiki thread): clean up wiki
mydearxym Sep 30, 2018
b303c10
feat(cheatsheet): add feature just like wiki
mydearxym Sep 30, 2018
8136cce
chore(clean up): warnings and wip tag
mydearxym Sep 30, 2018
05805e0
refactor(factory): auto camelize factory attrs if need
mydearxym Oct 1, 2018
1daab98
Merge branch 'mapkeys' into dev
mydearxym Oct 1, 2018
266aa53
feat(json encode): config Jason in absinthe , ecto
mydearxym Oct 2, 2018
00fb4da
refactor(test): clean up warning
mydearxym Oct 2, 2018
b250f5c
style: fmt
mydearxym Oct 2, 2018
b8ff8df
fix: missing field
mydearxym Oct 2, 2018
e10a33e
Merge branch 'wiki-thread' into dev
mydearxym Oct 2, 2018
04d7ac4
refactor(reactions): extract comment fields and fix job reaction
mydearxym Oct 7, 2018
531e1cf
Merge branch 'cheatsheet-thread' into dev
mydearxym Oct 7, 2018
85d8b67
feat(video reactions): add basic favorite / star
mydearxym Oct 7, 2018
a344032
chore(reactions): add reactions test
mydearxym Oct 7, 2018
fb5847b
fix(video reaction): job_id -> video_id
mydearxym Oct 7, 2018
5177dfe
Merge branch 'reactions' into dev
mydearxym Oct 7, 2018
5850812
test(video comments): add like/dislike tests
mydearxym Oct 8, 2018
8c51ac7
refactor(rename): comments -> pagedComments
mydearxym Oct 8, 2018
5050661
test(repo tests): badic query tests
mydearxym Oct 8, 2018
fef0793
test: add mutation tests for video/repo comments
mydearxym Oct 8, 2018
5f6bee2
feat(job comment): add like / dislike & test
mydearxym Oct 8, 2018
1dfd5d9
chore(clean up): remove @wip tag
mydearxym Oct 8, 2018
f0c4714
Merge branch 'comments' into dev
mydearxym Oct 8, 2018
e4fa80d
refactor(reaction users): more tests on paged reaction users
mydearxym Oct 9, 2018
468b8c1
chore(clean up): remove @wip & add @doc
mydearxym Oct 9, 2018
6cbc9ce
fix: followersCount makes right by rm dataloader
mydearxym Oct 10, 2018
199a447
style: fmt
mydearxym Oct 10, 2018
ef71466
refactor(user favrites cats): enhance support for jobs / videos
mydearxym Oct 11, 2018
b462963
style: fmt
mydearxym Oct 11, 2018
3651fdd
Merge branch 'reaction-users' into dev
mydearxym Oct 11, 2018
81c7497
feat(user achievement): add source contribute info
mydearxym Oct 11, 2018
d93a11b
feat(user signin): init a empty achievement when new signin
mydearxym Oct 12, 2018
c45b72b
feat(favorite cats): add last_updated field
mydearxym Oct 12, 2018
1cee80b
feat(user): add editable-communities query field
mydearxym Oct 12, 2018
f0addbf
refactor(user query): rm unneed filter on some query
mydearxym Oct 12, 2018
1906d2b
feat(user page): user published contents done right
mydearxym Oct 12, 2018
7c2c86d
chore(clean up): rm :wip tag
mydearxym Oct 12, 2018
4e6acca
test(stared contents): add more test on query part
mydearxym Oct 12, 2018
8c2e339
refactor(uer favorites test): extract total_count -> @total_count
mydearxym Oct 12, 2018
029d502
chore(tests): clean warnings & rm :wip tag
mydearxym Oct 12, 2018
113b8c5
Merge branch 'userpage' into dev
mydearxym Oct 12, 2018
1f818cf
test(thread comments): add test for particlepators
mydearxym Oct 14, 2018
40fea00
feat(cms.contents): add favroted_category_id query field
mydearxym Oct 14, 2018
3f49934
test: restore some job tests & clean up
mydearxym Oct 14, 2018
ebc85cd
fix(user favorite): totalcount mismatch when switch cats
mydearxym Oct 14, 2018
08d30e2
Merge branch 'userpage' into dev
mydearxym Oct 14, 2018
bbda464
fix(user reputation): fix reputation calc by refactor
mydearxym Oct 15, 2018
1451208
refactor(contents reaction): block favorite action from react contents
mydearxym Oct 15, 2018
adde568
Merge branch 'userpage' into dev
mydearxym Oct 15, 2018
c885767
test(favrotie reaction tests): removed, use favroted category instead
mydearxym Oct 16, 2018
4e27a43
feat(user published): publisehd comments
mydearxym Oct 16, 2018
68488e7
refactor(comments): extract comments macro
mydearxym Oct 16, 2018
7ec06a9
chore: cleanup warnings
mydearxym Oct 16, 2018
eac987a
refactor(published comments): support parent content info
mydearxym Oct 16, 2018
ade4a93
Merge branch 'published' into dev
mydearxym Oct 16, 2018
49c4590
fix(cms passport): return empty instead of nil
mydearxym Oct 17, 2018
8d62d88
feat(comments): add comemntsCount for job/video/repo
mydearxym Oct 17, 2018
5513c76
fix: missing tags
mydearxym Oct 20, 2018
046255d
Merge branch 'auth' into dev
mydearxym Oct 20, 2018
10ce7d0
feat(reactions): repo favorite with basic test
mydearxym Oct 20, 2018
b97ae50
test(reactions): more test & doc for repo-favorite
mydearxym Oct 20, 2018
c20a171
chore(cleanup): rm wip tags
mydearxym Oct 20, 2018
7a16b4b
Merge branch 'reactions' into dev
mydearxym Oct 20, 2018
cd668d3
test(repo reaction): remove unneed tests on favroites
mydearxym Oct 20, 2018
392b7f5
Merge branch 'reactions' into dev
mydearxym Oct 20, 2018
752af96
feat(comments): comments participators for other
mydearxym Oct 20, 2018
723ff2b
Merge branch 'comments' into dev
mydearxym Oct 20, 2018
ca87c12
feat(cms repo): add missing favorite fields
mydearxym Oct 20, 2018
4c46c88
Merge branch 'reactions' into dev
mydearxym Oct 20, 2018
8529c04
chore(clean up): wip tag
mydearxym Oct 20, 2018
89448da
chore(docs): comments dataloader
mydearxym Oct 20, 2018
e059058
Merge branch 'comments' into dev
mydearxym Oct 20, 2018
8bd0761
refactor(comments): add paged participators
mydearxym Oct 20, 2018
d18e0fe
Merge branch 'comments' into dev
mydearxym Oct 20, 2018
0e319b4
test: missing fields
mydearxym Oct 22, 2018
68020fd
fix(follow choas): follow / undofollow done right
mydearxym Oct 22, 2018
00d7783
chore(clean up): test file warnings
mydearxym Oct 22, 2018
ac8c503
test(favoreted repos): add missing test for f-repos
mydearxym Oct 22, 2018
14f14bc
refactor(cms repos): rename last_fetch_time -> last_sync
mydearxym Oct 22, 2018
29d508a
Merge branch 'repo' into dev
mydearxym Oct 22, 2018
3e8990c
fix(repo favrote): totalCount query
mydearxym Oct 22, 2018
87b9ba5
feat(user): add views field
mydearxym Oct 23, 2018
327c3ef
feat(posts): add copy_right fields
mydearxym Oct 23, 2018
90056bf
fix: missing copy_right field
mydearxym Oct 23, 2018
9e7d122
Merge branch 'userpage' into dev
mydearxym Oct 23, 2018
b64f9f5
feat(viewers): add viewer_has_viewed to posts
mydearxym Oct 23, 2018
d149449
feat(has_viewed): add for jobs
mydearxym Oct 23, 2018
b428bc8
feat(viewer_has_viewed): for videos
mydearxym Oct 23, 2018
9dcbb1f
feat(viewer_has_viewd): for repos
mydearxym Oct 23, 2018
9a12b5f
chore(clean up): wip tags
mydearxym Oct 23, 2018
fefb700
Merge branch 'content-viewer' into dev
mydearxym Oct 23, 2018
8902126
fix(contents viewer): add edge case to test dataloader
mydearxym Oct 24, 2018
a2259ae
Merge branch 'content-viewer' into dev
mydearxym Oct 24, 2018
c209f34
refactor(tests): improve with multi argss
mydearxym Oct 24, 2018
ee761e8
feat(job): simplify job atts, old one is complex
mydearxym Oct 25, 2018
610722e
Merge branch 'jobs' into dev
mydearxym Oct 25, 2018
455edf2
fix(job): missing fields
mydearxym Oct 25, 2018
4f29efa
fix(graphql parse error): fix graphql error crash server
mydearxym Oct 25, 2018
3be43da
fix(job create): clean up & refactor
mydearxym Oct 25, 2018
70cdfb1
Merge branch 'jobs' into dev
mydearxym Oct 25, 2018
8e55f30
feat(topic): basic topic concept
mydearxym Oct 28, 2018
1747494
refactor(topic on posts): impl topic & test only on posts
mydearxym Oct 28, 2018
0024269
feat(tags): add topic support
mydearxym Oct 28, 2018
63ed78a
chore(clean up): wip tag
mydearxym Oct 28, 2018
f5d630c
Merge branch 'topic' into dev
mydearxym Oct 28, 2018
3867185
fix(topic): default args error when topic create
mydearxym Oct 30, 2018
5e2a0b4
refactor(general): make thread & topic downcase in db
mydearxym Oct 31, 2018
e68b08b
fix(passport): community check use raw, not title
mydearxym Oct 31, 2018
9697c41
refactor(topic): change default topic to posts
mydearxym Nov 1, 2018
d6510de
chore(clean up): wip tag
mydearxym Nov 1, 2018
cba7826
refactor(accounts): make inline-subcommunities return paged-version
mydearxym Nov 1, 2018
73159f5
refactor(account): remove account endpoint, use general user
mydearxym Nov 1, 2018
87d00c7
fix(subscribed community): mismatch cur_user
mydearxym Nov 1, 2018
6b938c6
fix: remote ip mismatch
mydearxym Nov 2, 2018
f898726
Merge branch 'topic' into dev
mydearxym Nov 2, 2018
954381f
feat(content counts): add & refactor cms contents count
mydearxym Nov 2, 2018
2858be7
chore(clean up): warnings
mydearxym Nov 2, 2018
43861e6
Merge branch 'totalCount' into dev
mydearxym Nov 2, 2018
eb29d67
refactor(pin contents): use seperate table to pin contens
mydearxym Nov 2, 2018
83926cc
refactor: reduce boil-code
mydearxym Nov 2, 2018
c02d827
refactor(pined contents): remove old pin state from communityFlags
mydearxym Nov 2, 2018
a820979
chore(clean up): wip tags
mydearxym Nov 2, 2018
6756e7c
Merge branch 'flags' into dev
mydearxym Nov 2, 2018
32c6e2f
refactor(customization): basic set/get workflow
mydearxym Nov 3, 2018
3260a71
Merge branch 'customization' into dev
mydearxym Nov 3, 2018
05058d2
fix(c11n): filter nil when user has no c11n & clean up
mydearxym Nov 4, 2018
725d403
Merge branch 'customization' into dev
mydearxym Nov 4, 2018
b1db77e
build: production
mydearxym Nov 6, 2018
9013109
refactor(geo data): mv geo gen out of tests -> Helper
mydearxym Nov 6, 2018
b21d335
build: production
mydearxym Nov 6, 2018
cdbc0c0
build: production
mydearxym Nov 7, 2018
34ce88c
build: production
mydearxym Nov 7, 2018
a4099a4
build: production
mydearxym Nov 7, 2018
ab4c882
build: production
mydearxym Nov 7, 2018
e75b461
build: production
mydearxym Nov 7, 2018
1f629b0
build: production
mydearxym Nov 7, 2018
463d94b
build: production
mydearxym Nov 7, 2018
f7426b2
build: production
mydearxym Nov 7, 2018
b922b49
build: production
mydearxym Nov 7, 2018
4a812b8
build: production
mydearxym Nov 7, 2018
f36af5f
build: production
mydearxym Nov 7, 2018
c3f5698
build: production
mydearxym Nov 7, 2018
1b777de
build: production
mydearxym Nov 7, 2018
29fcec0
build: production
mydearxym Nov 7, 2018
a046a7b
build: production
mydearxym Nov 7, 2018
28d7e73
build: production
mydearxym Nov 7, 2018
f72e1bf
build: production
mydearxym Nov 7, 2018
bed2dcd
build: production
mydearxym Nov 7, 2018
8474142
build: production
mydearxym Nov 7, 2018
120dc3d
build: production
mydearxym Nov 7, 2018
0e02c1a
build: production
mydearxym Nov 7, 2018
ec18d50
build: production
mydearxym Nov 7, 2018
53ce94e
refactor(remote ip): use x-forwarded-for header from SLB
mydearxym Nov 7, 2018
83172bf
build: production
mydearxym Nov 7, 2018
74e1288
build: production
mydearxym Nov 8, 2018
984cf85
build: production
mydearxym Nov 8, 2018
e4dcf35
build: production
mydearxym Nov 8, 2018
7731308
build: production
mydearxym Nov 8, 2018
9cf4f0c
build: production
mydearxym Nov 8, 2018
86193d1
build: production
mydearxym Nov 8, 2018
16a4b0a
build: production
mydearxym Nov 8, 2018
37205cc
build: production
mydearxym Nov 8, 2018
78f62e8
build: production
mydearxym Nov 8, 2018
b3cd151
build: production
mydearxym Nov 8, 2018
a9c6f03
build: production
mydearxym Nov 8, 2018
db71eef
build: production
mydearxym Nov 8, 2018
1dd20dd
build: production
mydearxym Nov 8, 2018
28821d8
build: production
mydearxym Nov 8, 2018
7f25e84
build: production
mydearxym Nov 8, 2018
39f03b7
build: production
mydearxym Nov 8, 2018
4aba7c5
build: production
mydearxym Nov 8, 2018
270597f
chore: clean up
mydearxym Nov 8, 2018
048ebad
build: production
mydearxym Nov 8, 2018
d02b63c
chore(seeds): add basic seeds & fix some edge case
mydearxym Nov 9, 2018
6ed6bb6
style: fmt
mydearxym Nov 9, 2018
2b0295a
feat(seeds): home community & makefile cmd
mydearxym Nov 9, 2018
165a6ba
fix(test error): cause by geo remote_ip
mydearxym Nov 9, 2018
76c283b
refactor(create_tag): update args to follow conventions
mydearxym Nov 9, 2018
107aaa1
refactor(seeds): add tags for default community
mydearxym Nov 9, 2018
84ab32a
build: production
mydearxym Nov 10, 2018
f165fa4
build(deps-dev): bump credo from 0.10.1 to 0.10.2
dependabot-support Nov 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ deploy.prod:
@echo "deploy to docker done!"
@echo "todo: restart docker container"

reset:
$(call reset.help)
@echo "\n"
reset.help:
$(call reset.help)
@echo "\n"
reset.test:
env MIX_ENV=test mix ecto.drop
env MIX_ENV=test mix ecto.create
reset.mock:
env MIX_ENV=mock mix ecto.reset
reset.prod:
env MIX_ENV=prod mix ecto.reset

seeds:
$(call reset.help)
@echo "\n"
seeds.help:
$(call reset.help)
@echo "\n"
seeds.mock:
@echo "------------------------------"
@echo "seeds the mock database"
env MIX_ENV=mock mix cps.seeds

seeds.prod:
@echo "------------------------------"
@echo "seeds the prod database"
env MIX_ENV=prod mix cps.seeds

test.help:
$(call test.help)
@echo "\n"
Expand All @@ -125,9 +155,8 @@ test.watch.wip:
mix test.watch --only wip
test.watch.wip2:
mix test.watch --only wip2
test.db_reset:
env MIX_ENV=test mix ecto.drop
env MIX_ENV=test mix ecto.create
test.watch.bug:
mix test.watch --only bug
test.report:
MIX_ENV=mix test.coverage
$(call browse,"./cover/excoveralls.html")
Expand Down
26 changes: 26 additions & 0 deletions Makefile.include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ define console.help
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
endef

define reset.help
@echo "\n"
@echo " [WARNING: reset db commands]"
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
@echo " reset.test : reset test db"
@echo " ..................................."
@echo " reset.mock : reset mock db"
@echo " ..................................."
@echo " reset.prod : reset prod db"
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
endef

define seeds.help
@echo "\n"
@echo " [seeds db commands]"
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
@echo " seeds.mock : insert default communities etc .. to mock db"
@echo " .........................................................."
@echo " seeds.prod : insert default communities etc .. to prod db"
@echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
endef

define test.help
@echo "\n"
@echo " [valid test commands]"
Expand All @@ -130,6 +152,10 @@ define test.help
@echo " ....................................................."
@echo " test.watch.wip : run @wip test in watch mode"
@echo " ....................................................."
@echo " test.watch.wip2 : shortcut for lots of @wip around"
@echo " ....................................................."
@echo " test.watch.bug : sometimes fails for unkown reason"
@echo " ....................................................."
@echo " test.db_reset : reset test database"
@echo " | needed when add new migration"
@echo " ....................................................."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

![CPS Brand](https://github.com/mydearxym/mastani_server/blob/dev/docs/snapshots/cps_logo_md.png)
![CPS Brand](https://github.com/mydearxym/mastani_server/blob/dev/docs/snapshots/cps_logo_md.png?raw=true)
=========
[![Build Status](https://travis-ci.org/coderplanets/coderplanets_server.svg?branch=dev)](https://travis-ci.org/coderplanets/coderplanets_server)
[![codecov](https://codecov.io/gh/coderplanets/coderplanets_server/branch/dev/graph/badge.svg)](https://codecov.io/gh/coderplanets/coderplanets_server)
Expand Down
11 changes: 11 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ config :logger, :console,
metadata: [:request_id]

config :phoenix, :format_encoders, json: Jason
config :ecto, json_library: Jason

# TODO move this config to secret later
config :mastani_server, Helper.Guardian,
Expand Down Expand Up @@ -50,6 +51,16 @@ config :mastani_server, :general,
user_achieve_favorite_weight: 2,
user_achieve_follow_weight: 3

config :mastani_server, :customization,
theme: "cyan",
community_chart: false,
brainwash_free: false,
banner_layout: "digest",
contents_layout: "digest",
content_divider: false,
mark_viewed: true,
display_density: "20"

config :mastani_server, MastaniServerWeb.Gettext, default_locale: "zh_CN", locales: ~w(en zh_CN)

import_config "#{Mix.env()}.exs"
Expand Down
2 changes: 2 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ config :mastani_server, :github_oauth,
client_id: System.get_env("OAUTH_GITHUB_CLIENT_ID"),
client_secret: System.get_env("OAUTH_GITHUB_CLIENT_SECRET")

config :mastani_server, :radar_search, ip_service: System.get_env("IP_LOCATE_KEY")

config :sentry,
dsn: System.get_env("SENTRY_DSN"),
environment_name: :prod,
Expand Down
Binary file modified deploy/dev/api_server.tar.gz
Binary file not shown.
Binary file modified deploy/production/api_server.tar.gz
Binary file not shown.
85 changes: 85 additions & 0 deletions lib/helper/PublicIpPlug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# https://www.cogini.com/blog/getting-the-client-public-ip-address-in-phoenix/
defmodule Helper.PublicIpPlug do
@moduledoc "Get public IP address of request from x-forwarded-for header"
@behaviour Plug
@app :mastani_server

def init(opts), do: opts

def call(%{assigns: %{ip: _}} = conn, _opts), do: conn

def call(conn, _opts) do
process(conn, Plug.Conn.get_req_header(conn, "X-Forwarded-For"))
end

def process(conn, []) do
Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(get_peer_ip(conn))))
end

def process(conn, vals) do
if Application.get_env(@app, :trust_x_forwarded_for, false) do
ip_address = get_ip_address(conn, vals)

# Rewrite standard remote_ip field with value from header
# See https://hexdocs.pm/plug/Plug.Conn.html
conn = %{conn | remote_ip: ip_address}

Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(ip_address)))
else
Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(get_peer_ip(conn))))
end
end

defp get_ip_address(conn, vals)
defp get_ip_address(conn, []), do: get_peer_ip(conn)

defp get_ip_address(conn, [val | _]) do
# Split into multiple values
comps =
val
|> String.split(~r{\s*,\s*}, trim: true)
# Get rid of "unknown" values
|> Enum.filter(&(&1 != "unknown"))
# Split IP from port, if any
|> Enum.map(&hd(String.split(&1, ":")))
# Filter out blanks
|> Enum.filter(&(&1 != ""))
# Parse address into :inet.ip_address tuple
|> Enum.map(&parse_address(&1))
# Elminate internal IP addreses, e.g. 192.168.1.1
|> Enum.filter(&is_public_ip(&1))

case comps do
[] -> get_peer_ip(conn)
[comp | _] -> comp
end
end

@spec get_peer_ip(Plug.Conn.t()) :: :inet.ip_address()
defp get_peer_ip(conn) do
{ip, _port} = conn.peer
ip
end

@spec parse_address(String.t()) :: :inet.ip_address()
defp parse_address(ip) do
case :inet.parse_ipv4strict_address(to_charlist(ip)) do
{:ok, ip_address} -> ip_address
{:error, :einval} -> :einval
end
end

# Whether the input is a valid, public IP address
# http://en.wikipedia.org/wiki/Private_network
@spec is_public_ip(:inet.ip_address() | atom) :: boolean
defp is_public_ip(ip_address) do
case ip_address do
{10, _, _, _} -> false
{192, 168, _, _} -> false
{172, second, _, _} when second >= 16 and second <= 31 -> false
{127, 0, 0, _} -> false
{_, _, _, _} -> true
:einval -> false
end
end
end
Loading