ton of refactoring, dropped lustre for a wasm frontend in rust

This commit is contained in:
2025-05-07 19:38:31 -06:00
parent 76522cc053
commit 91547b9c2e
18 changed files with 543 additions and 224 deletions
+1
View File
@@ -2,3 +2,4 @@
*.ez
/build
erl_crash.dump
config.toml
+9
View File
@@ -14,6 +14,15 @@ version = "1.0.0"
[dependencies]
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
storail = ">= 3.0.0 and < 4.0.0"
tom = ">= 1.1.1 and < 2.0.0"
simplifile = ">= 2.2.1 and < 3.0.0"
glint = ">= 1.2.1 and < 2.0.0"
shared = { path = "../shared" }
mist = ">= 4.0.7 and < 5.0.0"
argv = ">= 1.0.2 and < 2.0.0"
gleam_erlang = ">= 0.34.0 and < 1.0.0"
gleam_http = ">= 4.0.0 and < 5.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
+46
View File
@@ -0,0 +1,46 @@
# 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 = "directories", version = "1.2.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "D13090CFCDF6759B87217E8DDD73A75903A700148A82C1D33799F333E249BF9E" },
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
{ name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" },
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.3.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A7DD6C07B7DA49A6E28796058AA89E651D233B357D5607006D70619CD89DAAAB" },
{ name = "glint", version = "1.2.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "2214C7CEFDE457CEE62140C3D4899B964E05236DA74E4243DFADF4AF29C382BB" },
{ name = "glisten", version = "7.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "1A53CF9FB3231A93FF7F1BD519A43DC968C1722F126CDD278403A78725FC5189" },
{ name = "gramps", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "59194B3980110B403EE6B75330DB82CDE05FC8138491C2EAEACBC7AAEF30B2E8" },
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
{ name = "mist", version = "4.0.7", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gleam_yielder", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "F7D15A1E3232E124C7CE31900253633434E59B34ED0E99F273DEE61CDB573CDD" },
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
{ name = "shared", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], source = "local", path = "../shared" },
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
{ name = "snag", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "7E9F06390040EB5FAB392CE642771484136F2EC103A92AE11BA898C8167E6E17" },
{ name = "storail", version = "3.0.0", build_tools = ["gleam"], requirements = ["directories", "filepath", "gleam_crypto", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "storail", source = "hex", outer_checksum = "D032EE5C89AA4B6306FF81929BF9B5DD2583E9F743F0047788AE5F1F52AFE3B4" },
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
{ name = "tom", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0910EE688A713994515ACAF1F486A4F05752E585B9E3209D8F35A85B234C2719" },
]
[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" }
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
glint = { version = ">= 1.2.1 and < 2.0.0" }
mist = { version = ">= 4.0.7 and < 5.0.0" }
shared = { path = "../shared" }
simplifile = { version = ">= 2.2.1 and < 3.0.0" }
storail = { version = ">= 3.0.0 and < 4.0.0" }
tom = { version = ">= 1.1.1 and < 2.0.0" }
+71
View File
@@ -0,0 +1,71 @@
import app/web
import gleam/bytes_tree
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import gleam/option.{None}
import gleam/result
import gleam/string
import mist.{type Connection, type ResponseData}
pub fn handle_request(
request: Request(Connection),
ctx: web.Context,
) -> Response(ResponseData) {
case request.path_segments(request) {
[] -> serve_frontend()
["static", "frontend.wasm"] -> serve_wasm(ctx)
_ -> response.set_body(response.new(404), mist.Bytes(bytes_tree.new()))
}
}
const game_body = "
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>TITLE</title>
<style>
html,
body,
canvas {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
position: absolute;
background: black;
z-index: 0;
}
</style>
</head>
<body>
<canvas id=\"glcanvas\" tabindex='1'></canvas>
<!-- Minified and statically hosted version of https://github.com/not-fl3/macroquad/blob/master/js/mq_js_bundle.js -->
<script src=\"https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js\"></script>
<script>load(\"static/frontend.wasm\");</script>
</body>
</html>
"
fn serve_frontend() -> Response(ResponseData) {
response.new(200)
|> response.set_body(mist.Bytes(bytes_tree.from_string(game_body)))
|> response.set_header("content-type", "text/html")
}
fn serve_wasm(ctx: web.Context) -> Response(ResponseData) {
let wasm_path = string.join([ctx.config.static_path, "frontend.wasm"], "/")
mist.send_file(wasm_path, offset: 0, limit: None)
|> result.map(fn(file) {
response.new(200)
|> response.prepend_header("content-type", "application/wasm")
|> response.set_body(file)
})
|> result.lazy_unwrap(fn() {
response.new(404)
|> response.set_body(mist.Bytes(bytes_tree.new()))
})
}
+7
View File
@@ -0,0 +1,7 @@
import config
import session
import storail
pub type Context {
Context(config: config.Config, sessions: storail.Collection(session.Session))
}
+71 -2
View File
@@ -1,5 +1,74 @@
import gleam/io
import app/router
import app/web
import argv
import config
import gleam/erlang/process
import gleam/result
import glint
import mist
import session
import storail
pub type Error {
ConfigError(config.ConfigError)
}
pub fn error_string(err: Error) -> String {
case err {
ConfigError(err) -> config.error_to_string(err)
}
}
pub fn setup_session_collection(
config: storail.Config,
) -> storail.Collection(session.Session) {
storail.Collection(
name: "Sessions",
to_json: session.serialize,
decoder: session.decoder(),
config:,
)
}
pub fn run_server() -> glint.Command(Result(Nil, Error)) {
use <- glint.command_help("Runs the backend of Play of the Game")
use _, args, _ <- glint.command()
let cfg_path = case args {
[] -> "config.toml"
[cfg_path, ..] -> cfg_path
}
use cfg <- result.try(
result.map_error(config.from_file(cfg_path), fn(err) {
let err = ConfigError(err)
err
}),
)
let storail_cfg = storail.Config(cfg.storage_path)
let ctx =
web.Context(config: cfg, sessions: setup_session_collection(storail_cfg))
let handler = router.handle_request(_, ctx)
let assert Ok(_) =
handler
|> mist.new
|> mist.bind(cfg.host)
|> mist.port(cfg.port)
|> mist.start_http
process.sleep_forever()
Ok(Nil)
}
pub fn main() {
io.println("Hello from backend!")
glint.new()
|> glint.with_name("Play Of The Game")
|> glint.pretty_help(glint.default_pretty_help())
|> glint.add(at: [], do: run_server())
|> glint.run(argv.load().arguments)
}
+71
View File
@@ -0,0 +1,71 @@
import gleam/result
import gleam/string
import simplifile
import tom
pub type ConfigError {
IOError(simplifile.FileError)
TomlError(tom.ParseError)
MissingKey(tom.GetError)
}
pub type Config {
Config(storage_path: String, host: String, port: Int, static_path: String)
}
pub fn error_to_string(err: ConfigError) -> String {
let msg = case err {
IOError(err) -> simplifile.describe_error(err)
TomlError(err) -> {
case err {
tom.Unexpected(got, expected) ->
"Unexpected: Got: " <> got <> "Expected: " <> expected
tom.KeyAlreadyInUse(path) ->
"Key already used at: " <> string.join(path, ".")
}
}
MissingKey(err) -> {
case err {
tom.NotFound(path) -> "Missing Key: " <> string.join(path, ".")
tom.WrongType(path, expected, got) ->
"Wrong type at: "
<> string.join(path, ".")
<> ", got: "
<> got
<> "expected: "
<> expected
}
}
}
"Config error: " <> msg
}
pub fn from_file(path: String) -> Result(Config, ConfigError) {
use config_str <- result.try(result.map_error(
simplifile.read(from: path),
IOError,
))
use config_tom <- result.try(result.map_error(
tom.parse(config_str),
TomlError,
))
use storage_path <- result.try(result.map_error(
tom.get_string(config_tom, ["storage_path"]),
MissingKey,
))
use port <- result.try(result.map_error(
tom.get_int(config_tom, ["port"]),
MissingKey,
))
use host <- result.try(result.map_error(
tom.get_string(config_tom, ["host"]),
MissingKey,
))
use static_path <- result.try(result.map_error(
tom.get_string(config_tom, ["static_path"]),
MissingKey,
))
Ok(Config(storage_path:, port:, host:, static_path:))
}