Skip to content

Commit 77e5d30

Browse files
committed
Initial Commit
0 parents  commit 77e5d30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1942
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# App artifacts
2+
/_build
3+
/db
4+
/deps
5+
/*.ez
6+
7+
# Generate on crash by the VM
8+
erl_crash.dump
9+
10+
# Static artifacts
11+
/node_modules
12+
13+
# Since we are building assets from web/static,
14+
# we ignore priv/static. You may want to comment
15+
# this depending on your deployment strategy.
16+
/priv/static/
17+
18+
# The config/prod.secret.exs file by default contains sensitive
19+
# data and you should not commit it into version control.
20+
#
21+
# Alternatively, you may comment the line below and commit the
22+
# secrets file as long as you replace its contents by environment
23+
# variables.
24+
/config/prod.secret.exs

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Todo
2+
3+
To start your Phoenix app:
4+
5+
* Install dependencies with `mix deps.get`
6+
* Create and migrate your database with `mix ecto.create && mix ecto.migrate`
7+
* Install Node.js dependencies with `npm install`
8+
* Start Phoenix endpoint with `mix phoenix.server`
9+
10+
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
11+
12+
Ready to run in production? Please [check our deployment guides](http://www.phoenixframework.org/docs/deployment).
13+
14+
## Learn more
15+
16+
* Official website: http://www.phoenixframework.org/
17+
* Guides: http://phoenixframework.org/docs/overview
18+
* Docs: http://hexdocs.pm/phoenix
19+
* Mailing list: http://groups.google.com/group/phoenix-talk
20+
* Source: https://github.com/phoenixframework/phoenix

brunch-config.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
exports.config = {
2+
// See http://brunch.io/#documentation for docs.
3+
files: {
4+
javascripts: {
5+
joinTo: "js/app.js"
6+
7+
// To use a separate vendor.js bundle, specify two files path
8+
// https://github.com/brunch/brunch/blob/stable/docs/config.md#files
9+
// joinTo: {
10+
// "js/app.js": /^(web\/static\/js)/,
11+
// "js/vendor.js": /^(web\/static\/vendor)|(deps)/
12+
// }
13+
//
14+
// To change the order of concatenation of files, explicitly mention here
15+
// https://github.com/brunch/brunch/tree/master/docs#concatenation
16+
// order: {
17+
// before: [
18+
// "web/static/vendor/js/jquery-2.1.1.js",
19+
// "web/static/vendor/js/bootstrap.min.js"
20+
// ]
21+
// }
22+
},
23+
stylesheets: {
24+
joinTo: "css/app.css"
25+
},
26+
templates: {
27+
joinTo: "js/app.js"
28+
}
29+
},
30+
31+
conventions: {
32+
// This option sets where we should place non-css and non-js assets in.
33+
// By default, we set this to "/web/static/assets". Files in this directory
34+
// will be copied to `paths.public`, which is "priv/static" by default.
35+
assets: /^(web\/static\/assets)/
36+
},
37+
38+
// Phoenix paths configuration
39+
paths: {
40+
// Dependencies and current project directories to watch
41+
watched: [
42+
"web/static",
43+
"test/static"
44+
],
45+
46+
// Where to compile files to
47+
public: "priv/static"
48+
},
49+
50+
// Configure your plugins
51+
plugins: {
52+
babel: {
53+
// Do not use ES6 compiler in vendor code
54+
ignore: [/web\/static\/vendor/]
55+
},
56+
elixirscript: {
57+
inputFolder: "web/static/exjs",
58+
outputFolder: "web/static/js",
59+
mainModule: "Todo"
60+
}
61+
},
62+
63+
modules: {
64+
autoRequire: {
65+
"js/app.js": ["web/static/js/main"]
66+
}
67+
},
68+
69+
npm: {
70+
enabled: true,
71+
// Whitelist the npm deps to be pulled in as front-end assets.
72+
// All other deps in package.json will be excluded from the bundle.
73+
whitelist: ["phoenix", "phoenix_html"]
74+
}
75+
};

config/config.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
#
4+
# This configuration file is loaded before any dependency and
5+
# is restricted to this project.
6+
use Mix.Config
7+
8+
# Configures the endpoint
9+
config :todo, Todo.Endpoint,
10+
url: [host: "localhost"],
11+
root: Path.dirname(__DIR__),
12+
secret_key_base: "iyanJp73OeHaopWgaSRc3dazQENLfmMw6/+gy7QGjAoLqE17fKBSJskQPFPs8Fbr",
13+
render_errors: [accepts: ~w(html json)],
14+
pubsub: [name: Todo.PubSub,
15+
adapter: Phoenix.PubSub.PG2]
16+
17+
# Configures Elixir's Logger
18+
config :logger, :console,
19+
format: "$time $metadata[$level] $message\n",
20+
metadata: [:request_id]
21+
22+
# Import environment specific config. This must remain at the bottom
23+
# of this file so it overrides the configuration defined above.
24+
import_config "#{Mix.env}.exs"
25+
26+
# Configure phoenix generators
27+
config :phoenix, :generators,
28+
migration: true,
29+
binary_id: false

config/dev.exs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use Mix.Config
2+
3+
# For development, we disable any cache and enable
4+
# debugging and code reloading.
5+
#
6+
# The watchers configuration can be used to run external
7+
# watchers to your application. For example, we use it
8+
# with brunch.io to recompile .js and .css sources.
9+
config :todo, Todo.Endpoint,
10+
http: [port: 4000],
11+
debug_errors: true,
12+
code_reloader: true,
13+
check_origin: false,
14+
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin"]]
15+
16+
# Watch static and templates for browser reloading.
17+
config :todo, Todo.Endpoint,
18+
live_reload: [
19+
patterns: [
20+
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
21+
~r{priv/gettext/.*(po)$},
22+
~r{web/views/.*(ex)$},
23+
~r{web/templates/.*(eex)$}
24+
]
25+
]
26+
27+
# Do not include metadata nor timestamps in development logs
28+
config :logger, :console, format: "[$level] $message\n"
29+
30+
# Set a higher stacktrace during development.
31+
# Do not configure such in production as keeping
32+
# and calculating stacktraces is usually expensive.
33+
config :phoenix, :stacktrace_depth, 20
34+
35+
# Configure your database
36+
config :todo, Todo.Repo,
37+
adapter: Ecto.Adapters.Postgres,
38+
username: "postgres",
39+
password: "postgres",
40+
database: "todo_dev",
41+
hostname: "localhost",
42+
pool_size: 10

config/prod.exs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use Mix.Config
2+
3+
# For production, we configure the host to read the PORT
4+
# from the system environment. Therefore, you will need
5+
# to set PORT=80 before running your server.
6+
#
7+
# You should also configure the url host to something
8+
# meaningful, we use this information when generating URLs.
9+
#
10+
# Finally, we also include the path to a manifest
11+
# containing the digested version of static files. This
12+
# manifest is generated by the mix phoenix.digest task
13+
# which you typically run after static files are built.
14+
config :todo, Todo.Endpoint,
15+
http: [port: {:system, "PORT"}],
16+
url: [host: "example.com", port: 80],
17+
cache_static_manifest: "priv/static/manifest.json"
18+
19+
# Do not print debug messages in production
20+
config :logger, level: :info
21+
22+
# ## SSL Support
23+
#
24+
# To get SSL working, you will need to add the `https` key
25+
# to the previous section and set your `:url` port to 443:
26+
#
27+
# config :todo, Todo.Endpoint,
28+
# ...
29+
# url: [host: "example.com", port: 443],
30+
# https: [port: 443,
31+
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
32+
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
33+
#
34+
# Where those two env variables return an absolute path to
35+
# the key and cert in disk or a relative path inside priv,
36+
# for example "priv/ssl/server.key".
37+
#
38+
# We also recommend setting `force_ssl`, ensuring no data is
39+
# ever sent via http, always redirecting to https:
40+
#
41+
# config :todo, Todo.Endpoint,
42+
# force_ssl: [hsts: true]
43+
#
44+
# Check `Plug.SSL` for all available options in `force_ssl`.
45+
46+
# ## Using releases
47+
#
48+
# If you are doing OTP releases, you need to instruct Phoenix
49+
# to start the server for all endpoints:
50+
#
51+
# config :phoenix, :serve_endpoints, true
52+
#
53+
# Alternatively, you can configure exactly which server to
54+
# start per endpoint:
55+
#
56+
# config :todo, Todo.Endpoint, server: true
57+
#
58+
# You will also need to set the application root to `.` in order
59+
# for the new static assets to be served after a hot upgrade:
60+
#
61+
# config :todo, Todo.Endpoint, root: "."
62+
63+
# Finally import the config/prod.secret.exs
64+
# which should be versioned separately.
65+
import_config "prod.secret.exs"

config/test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use Mix.Config
2+
3+
# We don't run a server during test. If one is required,
4+
# you can enable the server option below.
5+
config :todo, Todo.Endpoint,
6+
http: [port: 4001],
7+
server: false
8+
9+
# Print only warnings and errors during test
10+
config :logger, level: :warn
11+
12+
# Configure your database
13+
config :todo, Todo.Repo,
14+
adapter: Ecto.Adapters.Postgres,
15+
username: "postgres",
16+
password: "postgres",
17+
database: "todo_test",
18+
hostname: "localhost",
19+
pool: Ecto.Adapters.SQL.Sandbox

lib/todo.ex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule Todo do
2+
use Application
3+
4+
# See http://elixir-lang.org/docs/stable/elixir/Application.html
5+
# for more information on OTP Applications
6+
def start(_type, _args) do
7+
import Supervisor.Spec, warn: false
8+
9+
children = [
10+
# Start the endpoint when the application starts
11+
supervisor(Todo.Endpoint, []),
12+
# Start the Ecto repository
13+
supervisor(Todo.Repo, []),
14+
worker(Todo.Store, []),
15+
# Here you could define other workers and supervisors as children
16+
# worker(Todo.Worker, [arg1, arg2, arg3]),
17+
]
18+
19+
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
20+
# for other strategies and supported options
21+
opts = [strategy: :one_for_one, name: Todo.Supervisor]
22+
Supervisor.start_link(children, opts)
23+
end
24+
25+
# Tell Phoenix to update the endpoint configuration
26+
# whenever the application is updated.
27+
def config_change(changed, _new, removed) do
28+
Todo.Endpoint.config_change(changed, removed)
29+
:ok
30+
end
31+
end

lib/todo/endpoint.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule Todo.Endpoint do
2+
use Phoenix.Endpoint, otp_app: :todo
3+
4+
socket "/socket", Todo.UserSocket
5+
6+
# Serve at "/" the static files from "priv/static" directory.
7+
#
8+
# You should set gzip to true if you are running phoenix.digest
9+
# when deploying your static files in production.
10+
plug Plug.Static,
11+
at: "/", from: :todo, gzip: false,
12+
only: ~w(css fonts images js favicon.ico robots.txt)
13+
14+
# Code reloading can be explicitly enabled under the
15+
# :code_reloader configuration of your endpoint.
16+
if code_reloading? do
17+
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
18+
plug Phoenix.LiveReloader
19+
plug Phoenix.CodeReloader
20+
end
21+
22+
plug Plug.RequestId
23+
plug Plug.Logger
24+
25+
plug Plug.Parsers,
26+
parsers: [:urlencoded, :multipart, :json],
27+
pass: ["*/*"],
28+
json_decoder: Poison
29+
30+
plug Plug.MethodOverride
31+
plug Plug.Head
32+
33+
plug Plug.Session,
34+
store: :cookie,
35+
key: "_todo_key",
36+
signing_salt: "CAuLmP0F"
37+
38+
plug Todo.Router
39+
end

lib/todo/repo.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule Todo.Repo do
2+
use Ecto.Repo, otp_app: :todo
3+
end

lib/todo/store.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule Todo.Store do
2+
3+
def start_link() do
4+
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
5+
end
6+
7+
def list() do
8+
Agent.get(__MODULE__, fn(state) ->
9+
Enum.map(Dict.to_list(state), fn({key, value}) ->
10+
value
11+
end)
12+
end)
13+
end
14+
15+
def add(title) do
16+
Agent.update(__MODULE__, fn(state) ->
17+
Dict.put(state, Set.size(state), %Todo.Models.Todo{ title: title, completed: false, id: Set.size(state) })
18+
end)
19+
end
20+
21+
def update(id, completed) do
22+
Agent.update(__MODULE__, fn(state) ->
23+
if Dict.has_key?(state, id) do
24+
updated_todo = %{ Dict.get(state, id) | completed: completed }
25+
Dict.put(state, id, updated_todo)
26+
else
27+
state
28+
end
29+
end)
30+
end
31+
32+
def remove(id) do
33+
Agent.update(__MODULE__, fn(state) ->
34+
if Dict.has_key?(state, id) do
35+
Dict.delete(state, id)
36+
else
37+
state
38+
end
39+
end)
40+
end
41+
42+
end

0 commit comments

Comments
 (0)