First bits of pure lustre-javascript for user login session
This commit is contained in:
parent
a1e4eb1dff
commit
e6851255dc
41 changed files with 8413 additions and 733 deletions
103
server/src/quizterm.gleam
Normal file
103
server/src/quizterm.gleam
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import backend/roomhandler
|
||||
import backend/sockethandler
|
||||
import backend/statehandler
|
||||
import gleam/bytes_tree
|
||||
import gleam/erlang/application
|
||||
import gleam/erlang/process
|
||||
import gleam/http/request
|
||||
import gleam/http/response.{type Response}
|
||||
import gleam/list
|
||||
import gleam/option.{None}
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import mist.{type ResponseData, File}
|
||||
import web/components/answerlist
|
||||
import web/components/card
|
||||
import web/components/control
|
||||
import web/router
|
||||
import wisp
|
||||
import wisp/wisp_mist
|
||||
|
||||
pub fn main() {
|
||||
wisp.configure_logger()
|
||||
|
||||
let assert Ok(state_handler) = statehandler.initialize()
|
||||
let assert Ok(room_handler) = roomhandler.initialize(state_handler)
|
||||
|
||||
let assert Ok(_) =
|
||||
fn(req) {
|
||||
case request.path_segments(req) {
|
||||
["lustre", "runtime.mjs"] -> serve_runtime()
|
||||
[] | ["index.html"]-> serve_static("root.html")
|
||||
["client.js"] -> serve_static("client.js")
|
||||
["static", file] -> serve_static(file)
|
||||
["socket", "card", id] ->
|
||||
sockethandler.serve(req, card.component(), id, room_handler)
|
||||
["socket", "control", id] ->
|
||||
sockethandler.serve(req, control.component(), id, room_handler)
|
||||
["socket", "slow", id] ->
|
||||
sockethandler.serve_slow(
|
||||
req,
|
||||
answerlist.component(),
|
||||
id,
|
||||
room_handler,
|
||||
state_handler,
|
||||
)
|
||||
_ ->
|
||||
wisp_mist.handler(
|
||||
router.handle_request(room_handler, state_handler, _),
|
||||
"very_secret",
|
||||
)(req)
|
||||
}
|
||||
}
|
||||
|> mist.new
|
||||
|> mist.bind("0.0.0.0")
|
||||
|> mist.port(1234)
|
||||
|> mist.start
|
||||
|
||||
process.sleep_forever()
|
||||
}
|
||||
|
||||
fn serve_static(filename: String) {
|
||||
let assert Ok(priv) = application.priv_directory("quizterm")
|
||||
let surname = string.split(filename, ".") |> list.last
|
||||
let path = priv <> "/static/" <> filename
|
||||
let data =
|
||||
mist.send_file(path, offset: 0, limit: None)
|
||||
|> result.map(fn(file) {
|
||||
echo "SUCCESS " <> filename
|
||||
response.new(200)
|
||||
|> response.set_header("Content-Type", case surname {
|
||||
Ok("css") -> "text/css"
|
||||
Ok("js") -> "application/javascript"
|
||||
Ok(_) | Error(_) -> "text/html"
|
||||
})
|
||||
|> response.set_body(file)
|
||||
})
|
||||
|> result.lazy_unwrap(fn() {
|
||||
echo "FAIL " <> filename
|
||||
response.new(404)
|
||||
|> response.set_body(
|
||||
bytes_tree.from_string("Requested resource not found") |> mist.Bytes,
|
||||
)
|
||||
})
|
||||
echo "Attempting to serve file " <> filename <> " was "
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
fn serve_runtime() -> Response(ResponseData) {
|
||||
let assert Ok(lustre_priv) = application.priv_directory("lustre")
|
||||
let file_path = lustre_priv <> "/static/lustre-server-component.mjs"
|
||||
|
||||
case mist.send_file(file_path, offset: 0, limit: None) {
|
||||
Ok(file) ->
|
||||
response.new(200)
|
||||
|> response.prepend_header("content-type", "application/javascript")
|
||||
|> response.set_body(file)
|
||||
|
||||
Error(_) ->
|
||||
response.new(404)
|
||||
|> response.set_body(mist.Bytes(bytes_tree.new()))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue