I don't know if i like this.
parent
a1287a485a
commit
82afc009c7
|
@ -33,6 +33,8 @@ Makefile.bac
|
||||||
|
|
||||||
/images
|
/images
|
||||||
/_nerves-tmp
|
/_nerves-tmp
|
||||||
|
nerves_system_*
|
||||||
|
NERVES_SYSTEM_*
|
||||||
Makefile
|
Makefile
|
||||||
release-*
|
release-*
|
||||||
dump.rdb
|
dump.rdb
|
||||||
|
|
|
@ -41,6 +41,7 @@ defmodule Farmbot.Bootstrap.Configurator do
|
||||||
_ ->
|
_ ->
|
||||||
Logger.info "Building new config."
|
Logger.info "Building new config."
|
||||||
import Supervisor.Spec
|
import Supervisor.Spec
|
||||||
|
:ets.new(:session, [:named_table, :public, read_concurrency: true])
|
||||||
children = [
|
children = [
|
||||||
Plug.Adapters.Cowboy.child_spec(:http, Farmbot.Bootstrap.Configurator.Router, [], [port: 4001])
|
Plug.Adapters.Cowboy.child_spec(:http, Farmbot.Bootstrap.Configurator.Router, [], [port: 4001])
|
||||||
]
|
]
|
||||||
|
|
|
@ -3,17 +3,59 @@ defmodule Farmbot.Bootstrap.Configurator.Router do
|
||||||
|
|
||||||
use Plug.Router
|
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 :match
|
||||||
plug :dispatch
|
plug :dispatch
|
||||||
|
|
||||||
import Farmbot.Bootstrap.Configurator.HTML
|
import Farmbot.Bootstrap.Configurator.HTML
|
||||||
|
|
||||||
get "/" do
|
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
|
end
|
||||||
|
|
||||||
match _ do
|
get "/previous" do
|
||||||
send_resp(conn, 404, "oops")
|
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
|
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
|
end
|
||||||
|
|
|
@ -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
|
|
@ -4,7 +4,13 @@
|
||||||
Configure your Farmbot! - <%= page %>
|
Configure your Farmbot! - <%= page %>
|
||||||
</title>
|
</title>
|
||||||
</head>
|
</head>
|
||||||
<div id=<%= page %>>
|
|
||||||
<%= render.() %>
|
<body>
|
||||||
</div>
|
<div id="content">
|
||||||
|
<div id=<%= page %>>
|
||||||
|
<%= render.() %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
whoops
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
Get ready to configure your Farmbot!
|
||||||
|
|
||||||
|
<form action="next">
|
||||||
|
<button> Go! </button>
|
||||||
|
</form>
|
|
@ -0,0 +1,5 @@
|
||||||
|
Lets configure Network!
|
||||||
|
<form action="network" method="post">
|
||||||
|
<input> SSID </input>
|
||||||
|
<input> PSK </input>
|
||||||
|
</form>
|
|
@ -0,0 +1 @@
|
||||||
|
2
|
Loading…
Reference in New Issue