I don't know if i like this.

pull/363/head
connor rigby 2017-09-12 13:11:01 -07:00
parent a1287a485a
commit 82afc009c7
9 changed files with 147 additions and 8 deletions

2
.gitignore vendored
View File

@ -33,6 +33,8 @@ Makefile.bac
/images
/_nerves-tmp
nerves_system_*
NERVES_SYSTEM_*
Makefile
release-*
dump.rdb

View File

@ -41,6 +41,7 @@ defmodule Farmbot.Bootstrap.Configurator do
_ ->
Logger.info "Building new config."
import Supervisor.Spec
:ets.new(:session, [:named_table, :public, read_concurrency: true])
children = [
Plug.Adapters.Cowboy.child_spec(:http, Farmbot.Bootstrap.Configurator.Router, [], [port: 4001])
]

View File

@ -3,17 +3,59 @@ defmodule Farmbot.Bootstrap.Configurator.Router do
use Plug.Router
plug Plug.Static, from: {:farmbot, "priv/static"}, at: "/static"
plug Plug.Static, from: {:farmbot, "priv/static"}, at: "/"
plug Plug.Session, store: :ets, key: "session", table: :session
plug :match
plug :dispatch
import Farmbot.Bootstrap.Configurator.HTML
get "/" do
conn |> send_resp(200, render("index"))
conn = conn |> fetch_session()
# session = conn |> get_session("session")
conn
|> put_session("session", 0)
|> send_resp(200, render("page0"))
end
match _ do
send_resp(conn, 404, "oops")
get "/previous" do
conn = conn |> fetch_session()
session = conn |> get_session("session")
cond do
is_nil(session) ->
conn
|> put_session("session", 0)
|> send_resp(200, render("page0"))
is_number(session) ->
conn |> handle_step(session - 1)
end
end
get "/next" do
conn = conn |> fetch_session()
session = conn |> get_session("session")
cond do
is_nil(session) ->
conn
|> put_session("session", 0)
|> send_resp(200, render("page0"))
is_number(session) ->
conn |> handle_step(session + 1)
end
end
defp handle_step(conn, num) do
try do
conn
|> put_session("session", num)
|> send_resp(200, render("page#{num}"))
rescue
_ ->
conn
|> put_session("session", 0)
|> send_resp(200, render("page0"))
end
end
match _, do: send_resp(conn, 404, "Page not found.")
end

View File

@ -0,0 +1,78 @@
defmodule Plug.Session.ETS do
@moduledoc """
Stores the session in an in-memory ETS table.
This store does not create the ETS table; it expects that an
existing named table with public properties is passed as an
argument.
We don't recommend using this store in production as every
session will be stored in ETS and never cleaned until you
create a task responsible for cleaning up old entries.
Also, since the store is in-memory, it means sessions are
not shared between servers. If you deploy to more than one
machine, using this store is again not recommended.
This store, however, can be used as an example for creating
custom storages, based on Redis, Memcached, or a database
itself.
## Options
* `:table` - ETS table name (required)
For more information on ETS tables, visit the Erlang documentation at
http://www.erlang.org/doc/man/ets.html.
## Storage
The data is stored in ETS in the following format:
{sid :: String.t, data :: map, timestamp :: :erlang.timestamp}
The timestamp is updated whenever there is a read or write to the
table and it may be used to detect if a session is still active.
## Examples
# Create an ETS table when the application starts
:ets.new(:session, [:named_table, :public, read_concurrency: true])
# Use the session plug with the table name
plug Plug.Session, store: :ets, key: "sid", table: :session
"""
@behaviour Plug.Session.Store
@max_tries 100
def init(opts) do
Keyword.fetch!(opts, :table)
end
def get(_conn, sid, table) do
case :ets.lookup(table, sid) do
[{^sid, data, _timestamp}] ->
:ets.update_element(table, sid, {3, now()})
{sid, data}
[] ->
{nil, %{}}
end
end
def put(_conn, nil, data, table) do
put_new(data, table)
end
def put(_conn, sid, data, table) do
:ets.insert(table, {sid, data, now()})
sid
end
def delete(_conn, sid, table) do
:ets.delete(table, sid)
:ok
end
defp put_new(data, table, counter \\ 0)
when counter < @max_tries do
sid = Base.encode64(:crypto.strong_rand_bytes(96))
if :ets.insert_new(table, {sid, data, now()}) do
sid
else
put_new(data, table, counter + 1)
end
end
defp now() do
:os.timestamp()
end
end

View File

@ -4,7 +4,13 @@
Configure your Farmbot! - <%= page %>
</title>
</head>
<div id=<%= page %>>
<%= render.() %>
</div>
<body>
<div id="content">
<div id=<%= page %>>
<%= render.() %>
</div>
</div>
</body>
</html>

View File

@ -1 +0,0 @@
whoops

View File

@ -0,0 +1,5 @@
Get ready to configure your Farmbot!
<form action="next">
<button> Go! </button>
</form>

View File

@ -0,0 +1,5 @@
Lets configure Network!
<form action="network" method="post">
<input> SSID </input>
<input> PSK </input>
</form>

View File

@ -0,0 +1 @@
2