Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement rendering of multiple modules in HTML
  • Loading branch information
MaxDesiatov committed Sep 22, 2024
commit b3f13e5e16dacdbd27a501af58c04b10f2b850f4
15 changes: 3 additions & 12 deletions Host/Sources/Server/App+Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ private func serveFile(path: FilePath) async throws -> Response {
})
}

private func serveHTML(_ string: String) -> Response {
.init(
status: .ok,
headers: .init(dictionaryLiteral: (.contentType, "text/html")),
body: .init(byteBuffer: .init(string: string))
)
}

enum UploadError: Error {
case invalidFileName
}
Expand All @@ -62,10 +54,9 @@ func buildRouter() -> Router<AppRequestContext> {
}

.get("/") { _, _ in
Response(
status: .temporaryRedirect,
headers: [.location: "/public/index.html"]
)
IndexPage(modules: [
.init(name: "Mix", path: "/public/.build/wasm32-unknown-none-wasm/release/swift-audio.wasm")
])
}

.get("/public/**") { req, ctx in
Expand Down
54 changes: 52 additions & 2 deletions Host/Sources/Server/IndexPage.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
import Hummingbird
import SystemPackage

struct IndexPage {
private func serveHTML(_ string: String) -> Response {
.init(
status: .ok,
headers: .init(dictionaryLiteral: (.contentType, "text/html"), (.contentLength, "\(string.utf8.count)")),
body: .init(byteBuffer: .init(string: string))
)
}

struct IndexPage: ResponseGenerator {
struct Module {
let name: String
let path: FilePath
}

let modules: [Module]
}

func response(from request: Request, context: some RequestContext) throws -> Response {
serveHTML(
"""
<html>
<head>
<meta charset="utf-8">
<title>Swift Audio Workstation</title>
</head>
<body>
<script type="module" src="/public/Sources/JavaScript/index.js">
</script>
<style>
body {
background-color: black;
padding: 1rem;
}
#wasm-logger {
font-family: sans-serif;
color: white;
}
</style>
</body>
\(modules.map { module in
"""
<h1 id="wasm-logger">\(module.name)</h1>
<div
class="moduleNode"
data-module-path="\(module.path)"
style="display: flex; flex-direction: column; align-items: flex-start; gap: 1rem;"
>
<canvas class="plotter" width="1000" height="210"></canvas>
<audio class="audio" type="audio.wav" controls></audio>
</div>
"""
}.joined(separator: "\n"))
</html>
"""
)
}
}