Simplify EzServer menu and serve original Web Console

This commit is contained in:
SantaSpeen committed 2026-11-22 03:12:27 +03:00
1 parent ee7e18490f
commit d748f7cc69
16 files changed
+290 -245

No files matched your search

+31 -1
View File
@@ -1,5 +1,6 @@
import Foundation
import Network
import UniformTypeIdentifiers
private struct APIVersion: Encodable {
let name: String
@@ -67,11 +68,12 @@ final class LocalServer {
listener.start(queue: queue)
}
func stop() {
func stop(completion: (() -> Void)? = nil) {
queue.async { [self] in
listener.cancel()
connections.values.forEach { $0.cancel() }
connections.removeAll()
completion?()
}
}
@@ -137,6 +139,11 @@ final class LocalServer {
return
}
if target.path == "/webconsole" || target.path.hasPrefix("/webconsole/") {
serveWebConsole(path: segments, on: connection)
return
}
let apiURL = "http://\(hosts[0])/api/v1"
let versions = [
"1.0.0", "1.0.1", "1.1.0", "1.2.0", "1.3.0", "1.3.1", "1.3.2", "2.0.0",
@@ -170,6 +177,29 @@ final class LocalServer {
}
}
private func serveWebConsole(path: [String], on connection: NWConnection) {
guard let root = Bundle.main.resourceURL?.appendingPathComponent("webconsole", isDirectory: true)
.resolvingSymlinksInPath() else {
send(["error": "Web Console assets are unavailable"], status: "404 Not Found", on: connection)
return
}
let components = path.count == 1 ? ["index.html"] : Array(path.dropFirst())
let file = components.reduce(root) { $0.appendingPathComponent($1) }.resolvingSymlinksInPath()
guard file.path.hasPrefix(root.path + "/"),
let metadata = try? file.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey]),
metadata.isRegularFile == true, let bytes = metadata.fileSize,
let handle = try? FileHandle(forReadingFrom: file) else {
send(["error": "Web Console asset not found"], status: "404 Not Found", on: connection)
return
}
let type = UTType(filenameExtension: file.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
let headers = responseHeaders(status: "200 OK", type: type, bytes: bytes, extra: "")
connection.send(content: headers, completion: .contentProcessed { [weak self] error in
guard let self, error == nil else { connection.cancel(); return }
self.stream(handle, remaining: bytes, on: connection)
})
}
private func serveLibrary(path: [String], query: [URLQueryItem], authorization: String?,
on connection: NWConnection) {
switch library.response(path: path, query: query, authorization: authorization) {