Skip to content

Commit

Permalink
made hot_reloading controlled by env and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-ops committed Mar 14, 2024
1 parent 8abe8ec commit cf28763
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 47 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mist = "~> 0.17"
gleam_erlang = "~> 0.24"
gleam_http = "~> 3.5"
nakai = "~> 0.9"
envoy = "~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
8 changes: 5 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# You typically do not need to edit this file

packages = [
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_http", version = "3.5.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "C2FC3322203B16F897C1818D9810F5DEFCE347F0751F3B44421E1261277A7373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "glisten", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "73BC09C8487C2FFC0963BFAB33ED2F0D636FDFA43B966E65C1251CBAB8458099" },
Expand All @@ -21,10 +22,11 @@ packages = [
]

[requirements]
envoy = { version = "~> 1.0" }
gleam_erlang = { version = "~> 0.24" }
gleam_http = { version = "~> 3.5" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
mist = { version = "~> 0.17"}
mist = { version = "~> 0.17" }
nakai = { version = "~> 0.9" }
wisp = { version = "~> 0.12" }
100 changes: 56 additions & 44 deletions src/app/layout.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import envoy
import gleam/string_builder.{type StringBuilder}
import nakai
import nakai/html
import nakai/html/attrs
import wisp

pub fn layout(page: html.Node(a)) {
let hot_reloading_scripts = case envoy.get("DEVELOPMENT") {
Ok("0") | Ok("false") | Ok("FALSE") -> [html.Nothing]
Ok(_) -> hot_reloading()
Error(_) -> [html.Nothing]
}

let layout =
html.Html([], [
html.Doctype("html"),
Expand All @@ -17,50 +24,9 @@ pub fn layout(page: html.Node(a)) {
html.title("HTMX Gleam!"),
]),
html.Body([], [
html.div(
[
attrs.id("reload"),
attrs.Attr("hx-get", "/reload"),
attrs.Attr("hx-trigger", "every 0.5s"),
attrs.Attr("hx-target", "#hot-reload"),
],
[
html.div([attrs.id("hot-reload")], [
html.Text("This will reload every second"),
]),
],
),
html.h1([], [html.Text("Hello, World!")]),
page,
html.Script(
"
let serverStopped = false;
let reload = undefined;
document.body.addEventListener('htmx:sendError', function(evt) {
console.log(evt)
const target = evt.detail.target;
if (target.id === 'hot-reload') {
serverStopped = true;
}
});
document.body.addEventListener('htmx:afterRequest', function(evt) {
const target = evt.detail.target;
if (target.id === 'hot-reload') {
if (evt.detail.failed) {
serverStopped = true;
}
if (evt.detail.successful) {
if (serverStopped) {
console.log('Server restarted!');
serverStopped = false;
window.location.reload();
}
}
}
});
",
),
..hot_reloading_scripts
]),
])

Expand Down Expand Up @@ -89,9 +55,55 @@ pub fn render_partial(partial: html.Node(a)) {
}

fn inject_script_src(html: StringBuilder) -> StringBuilder {
string_builder.replace(
html,
html
|> string_builder.replace(
"</head>",
"<script src='https://unpkg.com/[email protected]'></script></head>",
)
}

fn hot_reloading() -> List(html.Node(n)) {
[
html.div(
[
attrs.id("reload"),
attrs.Attr("hx-get", "/reload"),
attrs.Attr("hx-trigger", "every 0.5s"),
attrs.Attr("hx-target", "#hot-reload"),
],
[
html.div([attrs.id("hot-reload")], [
html.Text("This will reload every second"),
]),
],
),
html.Script(
"
let serverStopped = false;
let reload = undefined;
document.body.addEventListener('htmx:sendError', function(evt) {
console.log(evt)
const target = evt.detail.target;
if (target.id === 'hot-reload') {
serverStopped = true;
}
});
document.body.addEventListener('htmx:afterRequest', function(evt) {
const target = evt.detail.target;
if (target.id === 'hot-reload') {
if (evt.detail.failed) {
serverStopped = true;
}
if (evt.detail.successful) {
if (serverStopped) {
console.log('Server restarted!');
serverStopped = false;
window.location.reload();
}
}
}
});
",
),
]
}

0 comments on commit cf28763

Please sign in to comment.