mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-01 10:02:36 +03:00
418d1fe1c2
- Dunno why, but swifter.framework includes many swift related dylibs which are approx. 8MB big.
72 lines
2.4 KiB
Swift
72 lines
2.4 KiB
Swift
/**
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
* See LICENSE
|
|
*/
|
|
|
|
import Foundation
|
|
|
|
class HttpServerService {
|
|
|
|
typealias HtmlPreviewPair = StateActionPair<UuidState<MainWindow.State>, HtmlPreviewTool.Action>
|
|
typealias MainWindowPair = StateActionPair<UuidState<MainWindow.State>, MainWindow.Action>
|
|
|
|
init(port: Int) {
|
|
let resourceUrl = Bundle.main.resourceURL!
|
|
self.githubCssUrl = resourceUrl.appendingPathComponent("markdown/github-markdown.css")
|
|
|
|
do {
|
|
try self.server.start(in_port_t(port))
|
|
NSLog("server started on http://localhost:\(port)")
|
|
|
|
let previewResourceUrl = resourceUrl.appendingPathComponent("preview")
|
|
|
|
self.server["\(MarkdownReducer.basePath)/:path"] = shareFilesFromDirectory(previewResourceUrl.path)
|
|
self.server.GET["\(MarkdownReducer.basePath)/github-markdown.css"] = shareFile(githubCssUrl.path)
|
|
|
|
self.server["\(HtmlPreviewToolReducer.basePath)/:path"] = shareFilesFromDirectory(previewResourceUrl.path)
|
|
self.server.GET["\(HtmlPreviewToolReducer.basePath)/github-markdown.css"] = shareFile(githubCssUrl.path)
|
|
} catch {
|
|
NSLog("ERROR server could not be started on port \(port)")
|
|
}
|
|
}
|
|
|
|
func applyHtmlPreview(_ pair: HtmlPreviewPair) {
|
|
let state = pair.state.payload
|
|
|
|
guard let serverUrl = state.htmlPreview.server, let htmlFileUrl = state.htmlPreview.htmlFile else {
|
|
return
|
|
}
|
|
|
|
let basePath = serverUrl.payload.deletingLastPathComponent().path
|
|
|
|
self.server.GET[serverUrl.payload.path] = shareFile(htmlFileUrl.path)
|
|
self.server["\(basePath)/:path"] = shareFilesFromDirectory(htmlFileUrl.parent.path)
|
|
}
|
|
|
|
func applyMainWindow(_ pair: MainWindowPair) {
|
|
guard case .setCurrentBuffer = pair.action else {
|
|
return
|
|
}
|
|
|
|
let preview = pair.state.payload.preview
|
|
guard case .markdown = preview.status,
|
|
let buffer = preview.buffer,
|
|
let html = preview.html,
|
|
let server = preview.server
|
|
else {
|
|
return
|
|
}
|
|
|
|
NSLog("Serving \(html) on \(server)")
|
|
|
|
let htmlBasePath = server.deletingLastPathComponent().path
|
|
|
|
self.server["\(htmlBasePath)/:path"] = shareFilesFromDirectory(buffer.deletingLastPathComponent().path)
|
|
self.server.GET[server.path] = shareFile(html.path)
|
|
self.server.GET["\(htmlBasePath)/github-markdown.css"] = shareFile(self.githubCssUrl.path)
|
|
}
|
|
|
|
fileprivate let server = HttpServer()
|
|
fileprivate let githubCssUrl: URL
|
|
}
|