Oh look it's a working server component

This commit is contained in:
Mikko Ahlroth 2024-07-07 21:39:33 +03:00
parent a52629ceaa
commit c08f162db2
36 changed files with 218 additions and 276 deletions

4
.env.dist Normal file
View file

@ -0,0 +1,4 @@
export USERNAME="username"
export PASSWORD="password"
export PORT=48100
export SECRET_KEY_BASE="some very long value that would be a good base for a secret key 1234"

View file

View file

@ -1,9 +0,0 @@
import wisp.{type Request}
pub fn handle_request(req: Request) {
use <- wisp.log_request(req)
use <- wisp.rescue_crashes()
use _ <- wisp.handle_head(req)
wisp.ok() |> wisp.string_body("Hello.")
}

View file

@ -1,89 +0,0 @@
import aurinko/updater
import aurinko/updater/messaging
import aurinko/updater/pubsub
import aurinko/updater/registry
import aurinko/web/ws/encoder
import gleam/erlang/process
import gleam/json
import gleam/option
import gleam/otp/actor
import glubsub
import mist
pub type State {
State(
updater_pubsub: pubsub.UpdaterPubSub,
updater_registry: registry.UpdaterRegistry,
pubsub_subject: process.Subject(pubsub.OutMessage),
)
}
pub fn on_init(
conn: mist.WebsocketConnection,
updater_pubsub: pubsub.UpdaterPubSub,
updater_registry: registry.UpdaterRegistry,
) {
let pubsub_subject = process.new_subject()
let assert Ok(_) = glubsub.subscribe(updater_pubsub, pubsub_subject)
send_initial_dataset(conn, updater_registry)
let self_selector =
process.new_selector() |> process.selecting(pubsub_subject, fn(m) { m })
let state = State(updater_pubsub, updater_registry, pubsub_subject)
#(state, option.Some(self_selector))
}
pub fn on_close(state: State) {
let _ = glubsub.unsubscribe(state.updater_pubsub, state.pubsub_subject)
Nil
}
pub fn handle_message(
state: State,
conn: mist.WebsocketConnection,
message: mist.WebsocketMessage(pubsub.OutMessage),
) {
case message {
mist.Custom(pubsub.NewData(dataset)) -> {
let dataset = encoder.dataset(dataset)
let output =
json.object([
#("type", json.string("data_update")),
#("dataset", dataset),
])
|> json.to_string()
let assert Ok(_) = mist.send_text_frame(conn, output)
actor.continue(state)
}
mist.Closed | mist.Shutdown -> actor.Stop(process.Normal)
_ -> actor.continue(state)
}
}
fn send_initial_dataset(
conn: mist.WebsocketConnection,
updater_registry: registry.UpdaterRegistry,
) {
let updater_subject = registry.get(updater_registry)
case updater_subject {
Ok(sub) -> {
let messaging.GetDataResponse(initial_data) = updater.get_data(sub)
let dataset = case initial_data {
option.Some(dataset) -> encoder.dataset(dataset)
option.None -> json.null()
}
let output =
json.object([
#("type", json.string("initial_data")),
#("dataset", dataset),
])
|> json.to_string()
let assert Ok(_) = mist.send_text_frame(conn, output)
Nil
}
Error(_) -> Nil
}
}

4
common/.gitignore vendored
View file

@ -1,4 +0,0 @@
*.beam
*.ez
/build
erl_crash.dump

View file

@ -1,18 +0,0 @@
name = "aurinko_common"
version = "1.0.0"
# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
[dev-dependencies]

View file

@ -1,9 +0,0 @@
# This file was generated by Gleam
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
]
[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }

6
frontend/.gitignore vendored
View file

@ -1,6 +0,0 @@
*.beam
*.ez
/build
erl_crash.dump
priv/static/aurinko_frontend.css
priv/static/aurinko_frontend.mjs

View file

@ -1,23 +0,0 @@
name = "aurinko_frontend"
version = "1.0.0"
target = "javascript"
# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
aurinko_common = { path = "../common" }
lustre = ">= 4.3.0 and < 5.0.0"
lustre_ui = ">= 0.6.0 and < 1.0.0"
[dev-dependencies]
lustre_dev_tools = ">= 1.3.4 and < 2.0.0"

View file

@ -1,19 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Aurinko</title>
<link rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%2210 0 100 100%22><text y=%22.90em%22 font-size=%2290%22>🌅</text></svg>" />
<link rel="stylesheet" type="text/css" href="./priv/static/aurinko_frontend.css" />
<link rel="stylesheet" type="text/css" href="./priv/static/lustre-ui.css" />
</head>
<body>
<div id="app"></div>
<script type="module" src="./priv/static/aurinko_frontend.mjs"></script>
</body>
</html>

View file

@ -1,48 +0,0 @@
# This file was generated by Gleam
# You typically do not need to edit this file
packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "aurinko_common", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../common" },
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "fs", version = "8.6.1", build_tools = ["rebar3"], requirements = [], otp_app = "fs", source = "hex", outer_checksum = "61EA2BDAEDAE4E2024D0D25C63E44DCCF65622D4402DB4A2DF12868D1546503F" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ 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.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
{ name = "gleam_httpc", version = "2.2.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "CF76C71002DEECF6DC5D9CA83D962728FAE166B57926BE442D827004D3C7DF1B" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ 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_package_interface", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "CF3BFC5D0997750D9550D8D73A90F4B8D71C6C081B20ED4E70FFBE1E99AFC3C2" },
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "glearray", version = "0.2.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "9C207E05F38D724F464FA921378DB3ABC2B0A2F5821116D8BC8B2CACC68930D5" },
{ name = "glint", version = "0.18.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "5FB54D7732B4105E4AF4D89A7EE6D5E8CF33DA13A3575D0C6ECE470B97958454" },
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" },
{ name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" },
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
{ name = "logging", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "FCB111401BDB4703A440A94FF8CC7DA521112269C065F219C2766998333E7738" },
{ name = "lustre", version = "4.3.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "43642C0602D3E2D6FEC3E24173D68A1F8E646969B53A2B0A5EB61238DDA739C4" },
{ name = "lustre_dev_tools", version = "1.3.4", build_tools = ["gleam"], requirements = ["argv", "filepath", "fs", "gleam_community_ansi", "gleam_erlang", "gleam_http", "gleam_httpc", "gleam_json", "gleam_otp", "gleam_package_interface", "gleam_stdlib", "glint", "glisten", "mist", "simplifile", "spinner", "term_size", "tom", "wisp"], otp_app = "lustre_dev_tools", source = "hex", outer_checksum = "FB056F18870EA7FE2A070264A598CFCDB8F6F24D65FF989F18F3F46C9ABEEE31" },
{ name = "lustre_ui", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_json", "gleam_stdlib", "lustre"], otp_app = "lustre_ui", source = "hex", outer_checksum = "FA1F9E89D89CDD5DF376ED86ABA8A38441CB2E664CD4D402F22A49DA4D7BB56D" },
{ name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" },
{ name = "mist", version = "1.2.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "109B4D64E68C104CC23BB3CC5441ECD479DD7444889DA01113B75C6AF0F0E17B" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "repeatedly", version = "2.1.1", build_tools = ["gleam"], requirements = [], otp_app = "repeatedly", source = "hex", outer_checksum = "38808C3EC382B0CD981336D5879C24ECB37FCB9C1D1BD128F7A80B0F74404D79" },
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "spinner", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "200BA3D4A04D468898E63C0D316E23F526E02514BC46454091975CB5BAE41E8F" },
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "tom", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0831C73E45405A2153091226BF98FB485ED16376988602CC01A5FD086B82D577" },
{ name = "wisp", version = "0.14.0", build_tools = ["gleam"], requirements = ["exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "9F5453AF1F9275E6F8707BC815D6A6A9DF41551921B16FBDBA52883773BAE684" },
]
[requirements]
aurinko_common = { path = "../common" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
lustre = { version = ">= 4.3.0 and < 5.0.0" }
lustre_dev_tools = { version = ">= 1.3.4 and < 2.0.0"}
lustre_ui = { version = ">= 0.6.0 and < 1.0.0" }

View file

@ -1,38 +0,0 @@
import gleam/int
import lustre
import lustre/element.{text}
import lustre/element/html.{button, div, p}
import lustre/event.{on_click}
pub fn main() {
let app = lustre.simple(init, update, view)
let assert Ok(_) = lustre.start(app, "#app", Nil)
Nil
}
fn init(_flags) {
0
}
type Msg {
Incr
Decr
}
fn update(model, msg) {
case msg {
Incr -> model + 1
Decr -> model - 1
}
}
fn view(model) {
let count = int.to_string(model)
div([], [
button([on_click(Incr)], [text(" + ")]),
p([], [text(count)]),
button([on_click(Decr)], [text(" - ")]),
])
}

View file

@ -1,7 +0,0 @@
module.exports = {
content: ["./index.html", "./src/**/*.{gleam,mjs}"],
theme: {
extend: {},
},
plugins: [],
};

View file

@ -1,4 +1,4 @@
name = "aurinko_backend" name = "aurinko"
version = "1.0.0" version = "1.0.0"
target = "erlang" target = "erlang"
@ -28,10 +28,10 @@ form_coder = ">= 0.3.0 and < 1.0.0"
birl = ">= 1.7.1 and < 2.0.0" birl = ">= 1.7.1 and < 2.0.0"
gleam_json = ">= 1.0.1 and < 2.0.0" gleam_json = ">= 1.0.1 and < 2.0.0"
mist = ">= 1.2.0 and < 2.0.0" mist = ">= 1.2.0 and < 2.0.0"
aurinko_common = { path = "../common" }
gleamy_structures = ">= 1.0.0 and < 2.0.0" gleamy_structures = ">= 1.0.0 and < 2.0.0"
glubsub = ">= 1.0.1 and < 2.0.0" glubsub = ">= 1.0.1 and < 2.0.0"
dot_env = ">= 1.0.0 and < 2.0.0" dot_env = ">= 1.0.0 and < 2.0.0"
chip = ">= 0.7.5 and < 1.0.0" chip = ">= 0.7.5 and < 1.0.0"
lustre_ui = ">= 0.6.0 and < 1.0.0"
[dev-dependencies] [dev-dependencies]

View file

@ -2,7 +2,6 @@
# You typically do not need to edit this file # You typically do not need to edit this file
packages = [ packages = [
{ name = "aurinko_common", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../common" },
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, { name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "biscotto", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "biscotto", source = "hex", outer_checksum = "A1CFEA1686FA8ABDE90B76E22775FF29EE8156A64DAC327F48141A68951D662C" }, { name = "biscotto", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "biscotto", source = "hex", outer_checksum = "A1CFEA1686FA8ABDE90B76E22775FF29EE8156A64DAC327F48141A68951D662C" },
{ name = "chip", version = "0.7.5", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "chip", source = "hex", outer_checksum = "1C50EB65CE8617D64FBCCE9A3EA7E6C7BF3E646D8E39B92EA74F68B67476F59D" }, { name = "chip", version = "0.7.5", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "chip", source = "hex", outer_checksum = "1C50EB65CE8617D64FBCCE9A3EA7E6C7BF3E646D8E39B92EA74F68B67476F59D" },
@ -11,6 +10,7 @@ packages = [
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" }, { name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "form_coder", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "form_coder", source = "hex", outer_checksum = "FA27C97AADF66A79E4EEE78D30A50C18660A9FAFD91C27A5BC780DBC753CB8AF" }, { name = "form_coder", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "form_coder", source = "hex", outer_checksum = "FA27C97AADF66A79E4EEE78D30A50C18660A9FAFD91C27A5BC780DBC753CB8AF" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" }, { 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.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, { name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" }, { name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
@ -27,6 +27,7 @@ packages = [
{ name = "htmgrrrl", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "htmerl"], otp_app = "htmgrrrl", source = "hex", outer_checksum = "983492567967DAA64776E005B9E70353368B14E6F87D543E01308B48A7A0398F" }, { name = "htmgrrrl", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "htmerl"], otp_app = "htmgrrrl", source = "hex", outer_checksum = "983492567967DAA64776E005B9E70353368B14E6F87D543E01308B48A7A0398F" },
{ name = "logging", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "FCB111401BDB4703A440A94FF8CC7DA521112269C065F219C2766998333E7738" }, { name = "logging", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "FCB111401BDB4703A440A94FF8CC7DA521112269C065F219C2766998333E7738" },
{ name = "lustre", version = "4.3.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "43642C0602D3E2D6FEC3E24173D68A1F8E646969B53A2B0A5EB61238DDA739C4" }, { name = "lustre", version = "4.3.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "43642C0602D3E2D6FEC3E24173D68A1F8E646969B53A2B0A5EB61238DDA739C4" },
{ name = "lustre_ui", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_json", "gleam_stdlib", "lustre"], otp_app = "lustre_ui", source = "hex", outer_checksum = "FA1F9E89D89CDD5DF376ED86ABA8A38441CB2E664CD4D402F22A49DA4D7BB56D" },
{ name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" }, { name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" },
{ name = "mist", version = "1.2.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "109B4D64E68C104CC23BB3CC5441ECD479DD7444889DA01113B75C6AF0F0E17B" }, { name = "mist", version = "1.2.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "109B4D64E68C104CC23BB3CC5441ECD479DD7444889DA01113B75C6AF0F0E17B" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" }, { name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
@ -37,7 +38,6 @@ packages = [
] ]
[requirements] [requirements]
aurinko_common = { path = "../common" }
birl = { version = ">= 1.7.1 and < 2.0.0" } birl = { version = ">= 1.7.1 and < 2.0.0" }
biscotto = { version = ">= 1.0.0 and < 2.0.0" } biscotto = { version = ">= 1.0.0 and < 2.0.0" }
chip = { version = ">= 0.7.5 and < 1.0.0" } chip = { version = ">= 0.7.5 and < 1.0.0" }
@ -53,6 +53,7 @@ gleamy_structures = { version = ">= 1.0.0 and < 2.0.0" }
glubsub = { version = ">= 1.0.1 and < 2.0.0" } glubsub = { version = ">= 1.0.1 and < 2.0.0" }
htmgrrrl = { version = ">= 0.3.0 and < 1.0.0" } htmgrrrl = { version = ">= 0.3.0 and < 1.0.0" }
lustre = { version = ">= 4.3.0 and < 5.0.0" } lustre = { version = ">= 4.3.0 and < 5.0.0" }
lustre_ui = { version = ">= 0.6.0 and < 1.0.0" }
mist = { version = ">= 1.2.0 and < 2.0.0" } mist = { version = ">= 1.2.0 and < 2.0.0" }
sqlight = { version = ">= 0.9.0 and < 1.0.0" } sqlight = { version = ">= 0.9.0 and < 1.0.0" }
wisp = { version = ">= 0.15.0 and < 1.0.0" } wisp = { version = ">= 0.15.0 and < 1.0.0" }

View file

@ -0,0 +1 @@
../../build/packages/lustre/priv/static/lustre-server-component.mjs

View file

@ -16,8 +16,10 @@ pub fn init(
updater_registry: registry.UpdaterRegistry, updater_registry: registry.UpdaterRegistry,
) { ) {
wisp.configure_logger() wisp.configure_logger()
let assert Ok(priv) = wisp.priv_directory("aurinko")
let wisp_side = wisp.mist_handler(router.handle_request, secret_key_base) let wisp_side =
wisp.mist_handler(router.handle_request(_, priv), secret_key_base)
fn(req) { fn(req) {
case request.path_segments(req) { case request.path_segments(req) {

View file

@ -0,0 +1,84 @@
import gleam/int
import lustre
import lustre/attribute
import lustre/element.{type Element}
import lustre/element/html
import lustre/event
import lustre/ui
// MAIN ------------------------------------------------------------------------
pub fn app(_updater_pubsub, _updater_registry) {
lustre.simple(init, update, view)
}
// MODEL -----------------------------------------------------------------------
type Model =
Int
fn init(initial_count: Int) -> Model {
case initial_count < 0 {
True -> 0
False -> initial_count
}
}
// UPDATE ----------------------------------------------------------------------
pub opaque type Msg {
Incr
Decr
}
fn update(model: Model, msg: Msg) -> Model {
case msg {
Incr -> model + 1
Decr -> model - 1
}
}
// VIEW ------------------------------------------------------------------------
fn view(model: Model) -> Element(Msg) {
let styles = [#("width", "100vw"), #("height", "100vh"), #("padding", "1rem")]
let count = int.to_string(model)
ui.centre(
[attribute.style(styles)],
ui.stack([], [
ui.button([event.on_click(Incr)], [element.text("+")]),
html.slot([]),
html.p([attribute.style([#("text-align", "center")])], [
element.text(count),
]),
ui.button([event.on_click(Decr)], [element.text("-")]),
]),
)
}
// fn send_initial_dataset(
// conn: mist.WebsocketConnection,
// updater_registry: registry.UpdaterRegistry,
// ) {
// let updater_subject = registry.get(updater_registry)
// case updater_subject {
// Ok(sub) -> {
// let messaging.GetDataResponse(initial_data) = updater.get_data(sub)
// let dataset = case initial_data {
// option.Some(dataset) -> encoder.dataset(dataset)
// option.None -> json.null()
// }
// let output =
// json.object([
// #("type", json.string("initial_data")),
// #("dataset", dataset),
// ])
// |> json.to_string()
// let assert Ok(_) = mist.send_text_frame(conn, output)
// Nil
// }
// Error(_) -> Nil
// }
// }

View file

@ -0,0 +1,30 @@
import lustre/attribute
import lustre/element
import lustre/element/html
import lustre/server_component
pub fn view() {
html.html([], [
html.head([], [
html.link([
attribute.rel("stylesheet"),
attribute.href("./static/lustre-ui.css"),
]),
html.title([], "Aurinko"),
]),
html.body([], [
element.element(
"lustre-server-component",
[server_component.route("/ws")],
[],
),
html.script(
[
attribute.type_("module"),
attribute.src("./static/lustre-server-component.mjs"),
],
"",
),
]),
])
}

View file

@ -0,0 +1,19 @@
import aurinko/web/index
import lustre/element
import wisp.{type Request}
pub fn handle_request(req: Request, priv_dir: String) {
use <- wisp.log_request(req)
use <- wisp.rescue_crashes()
use _ <- wisp.handle_head(req)
use <- wisp.serve_static(req, under: "/static", from: priv_dir <> "/static")
case wisp.path_segments(req) {
[] ->
wisp.html_response(
index.view() |> element.to_document_string_builder(),
200,
)
_ -> wisp.not_found()
}
}

71
src/aurinko/web/ws.gleam Normal file
View file

@ -0,0 +1,71 @@
import aurinko/updater/pubsub
import aurinko/updater/registry
import aurinko/web/components/main
import gleam/erlang/process
import gleam/json
import gleam/option
import gleam/otp/actor
import lustre
import lustre/server_component
import mist
type Main =
process.Subject(lustre.Action(main.Msg, lustre.ServerComponent))
pub type State {
State(component: Main)
}
pub fn on_init(
_conn: mist.WebsocketConnection,
updater_pubsub: pubsub.UpdaterPubSub,
updater_registry: registry.UpdaterRegistry,
) {
let self = process.new_subject()
let app = main.app(updater_pubsub, updater_registry)
let assert Ok(main_subject) = lustre.start_actor(app, 0)
process.send(
main_subject,
server_component.subscribe("ws", process.send(self, _)),
)
let state = State(main_subject)
let selector = process.new_selector() |> process.selecting(self, fn(m) { m })
#(state, option.Some(selector))
}
pub fn on_close(state: State) {
process.send(state.component, lustre.shutdown())
Nil
}
pub fn handle_message(
state: State,
conn: mist.WebsocketConnection,
message: mist.WebsocketMessage(lustre.Patch(main.Msg)),
) {
case message {
mist.Text(json) -> {
let action = json.decode(json, server_component.decode_action)
case action {
Ok(action) -> process.send(state.component, action)
Error(_) -> Nil
}
actor.continue(state)
}
mist.Custom(patch) -> {
let assert Ok(_) =
patch
|> server_component.encode_patch
|> json.to_string
|> mist.send_text_frame(conn, _)
actor.continue(state)
}
mist.Closed | mist.Shutdown -> actor.Stop(process.Normal)
_ -> actor.continue(state)
}
}