-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
suazi
committed
Jun 5, 2019
1 parent
5ed8efb
commit 72ed792
Showing
62 changed files
with
2,009 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,74 @@ | ||
# Barcamp | ||
|
||
To start your Phoenix server: | ||
Requirements: | ||
|
||
* Install dependencies with `mix deps.get` | ||
* Create and migrate your database with `mix ecto.setup` | ||
* Install Node.js dependencies with `cd assets && npm install` | ||
* Start Phoenix endpoint with `mix phx.server` | ||
* `elixir` | ||
* `nodejs` | ||
* `yarn` (install `node` first, then install `yarn` with `npm i -g yarn`, do NOT install it with `brew` or `apt`) | ||
* `postgresql` | ||
|
||
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. | ||
|
||
Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). | ||
### Building Releases | ||
|
||
## Learn more | ||
Add a `config/prod.secret.exs` file and put your very secret configs in there: | ||
``` | ||
use Mix.Config | ||
* Official website: http://www.phoenixframework.org/ | ||
* Guides: https://hexdocs.pm/phoenix/overview.html | ||
* Docs: https://hexdocs.pm/phoenix | ||
* Mailing list: http://groups.google.com/group/phoenix-talk | ||
* Source: https://github.com/phoenixframework/phoenix | ||
config :barcamp, BarcampWeb.Endpoint, | ||
secret_key_base: "__SECRET_KEY__" | ||
config :barcamp, Barcamp.Repo, | ||
username: "__DB_USER__", | ||
password: "__DB_PASSWORD__", | ||
database: "barcamp_prod", | ||
hostname: "localhost", | ||
pool_size: 10 | ||
``` | ||
|
||
You may use `mix phx.gen.secret` to generate a secret key. | ||
|
||
Replace the CA in `priv/cert/barcamp_key.pem` and `priv/cert/barcamp.pem` with your public CA. | ||
|
||
Build everything: | ||
``` | ||
mix deps.get --only prod | ||
MIX_ENV=prod mix compile | ||
yarn --cwd assets install && yarn --cwd assets deploy | ||
mix phx.digest | ||
MIX_ENV=prod mix do ecto.create, release | ||
``` | ||
|
||
See it in action: | ||
``` | ||
mkdir deploy_here | ||
cp _build/prod/rel/barcamp/releases/0.1.0/barcamp.tar.gz deploy_here/ | ||
cd deploy_here | ||
tar xvf barcamp.tar.gz | ||
PORT=4001 ./bin/barcamp start | ||
./bin/barcamp migrate | ||
./bin/barcamp create_user login password0! | ||
``` | ||
|
||
Then navigate to `localhost:4001`. | ||
|
||
_Note:_ You do not need to have erlang or elixir where you are deploying a release. | ||
|
||
### Updates / Hot-Code-Swap | ||
|
||
Bump the version: | ||
``` | ||
def project do | ||
[ | ||
... | ||
version: "0.1.1", # Bump the version here | ||
... | ||
] | ||
end | ||
``` | ||
|
||
Then: | ||
``` | ||
MIX_ENV=prod mix release --upgrade | ||
cp _build/prod/rel/barcamp/releases/0.1.1/barcamp.tar.gz deploy_here/releases/0.1.1/ | ||
./deploy_here/bin/barcamp upgrade 0.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
defmodule Barcamp.Auth do | ||
@moduledoc """ | ||
The Auth context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Barcamp.Repo | ||
|
||
alias Barcamp.Auth.User | ||
|
||
@doc """ | ||
Returns the list of users. | ||
## Examples | ||
iex> list_users() | ||
[%User{}, ...] | ||
""" | ||
def list_users do | ||
Repo.all(User) | ||
end | ||
|
||
@doc """ | ||
Gets a single user. | ||
Raises `Ecto.NoResultsError` if the User does not exist. | ||
## Examples | ||
iex> get_user!(123) | ||
%User{} | ||
iex> get_user!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_user!(id), do: Repo.get!(User, id) | ||
|
||
@doc """ | ||
Creates a user. | ||
## Examples | ||
iex> create_user(%{field: value}) | ||
{:ok, %User{}} | ||
iex> create_user(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_user(attrs \\ %{}) do | ||
result = | ||
%User{} | ||
|> User.registration_changeset(attrs) | ||
|> Repo.insert() | ||
|
||
case result do | ||
{:ok, user} -> {:ok, %User{user | password: nil}} | ||
_ -> result | ||
end | ||
end | ||
|
||
@doc """ | ||
Updates a user. | ||
## Examples | ||
iex> update_user(user, %{field: new_value}) | ||
{:ok, %User{}} | ||
iex> update_user(user, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_user(%User{} = user, attrs) do | ||
user | ||
|> User.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a User. | ||
## Examples | ||
iex> delete_user(user) | ||
{:ok, %User{}} | ||
iex> delete_user(user) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_user(%User{} = user) do | ||
Repo.delete(user) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking user changes. | ||
## Examples | ||
iex> change_user(user) | ||
%Ecto.Changeset{source: %User{}} | ||
""" | ||
def change_user(%User{} = user) do | ||
User.changeset(user, %{}) | ||
end | ||
|
||
def authenticate(login, password) do | ||
user = Repo.get_by(User, login: login) | ||
|
||
case check_password(user, password) do | ||
true -> {:ok, user} | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp check_password(user, password) do | ||
case user do | ||
nil -> Comeonin.Argon2.dummy_checkpw() | ||
_ -> Comeonin.Argon2.checkpw(password, user.password_hash) | ||
end | ||
end | ||
end |
Oops, something went wrong.