1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 11:35:35 +03:00
vimr/VimR/HttpServerService.swift

73 lines
2.4 KiB
Swift
Raw Normal View History

2017-01-25 00:39:19 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import Swifter
2017-04-02 18:27:12 +03:00
class HttpServerService {
2017-04-30 10:25:15 +03:00
typealias HtmlPreviewPair = StateActionPair<UuidState<MainWindow.State>, HtmlPreviewTool.Action>
typealias MainWindowPair = StateActionPair<UuidState<MainWindow.State>, MainWindow.Action>
2017-01-25 00:39:19 +03:00
2017-02-06 20:57:50 +03:00
init(port: Int) {
2017-04-30 10:25:15 +03:00
let resourceUrl = Bundle.main.resourceURL!
self.githubCssUrl = resourceUrl.appendingPathComponent("markdown/github-markdown.css")
2017-02-06 21:58:10 +03:00
2017-01-25 00:39:19 +03:00
do {
2017-02-06 20:57:50 +03:00
try self.server.start(in_port_t(port))
2017-01-25 00:39:19 +03:00
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)
2017-01-25 00:39:19 +03:00
} catch {
NSLog("ERROR server could not be started on port \(port)")
}
}
2017-04-30 10:25:15 +03:00
func applyHtmlPreview(_ pair: HtmlPreviewPair) {
let state = pair.state.payload
2017-03-24 19:47:08 +03:00
2017-04-30 10:25:15 +03:00
guard let serverUrl = state.htmlPreview.server, let htmlFileUrl = state.htmlPreview.htmlFile else {
return
2017-04-02 18:27:12 +03:00
}
2017-03-24 19:47:08 +03:00
2017-04-30 10:25:15 +03:00
let basePath = serverUrl.payload.deletingLastPathComponent().path
2017-04-30 10:25:15 +03:00
self.server.GET[serverUrl.payload.path] = shareFile(htmlFileUrl.path)
self.server["\(basePath)/:path"] = shareFilesFromDirectory(htmlFileUrl.parent.path)
2017-04-02 18:27:12 +03:00
}
2017-03-24 19:47:08 +03:00
2017-04-30 10:25:15 +03:00
func applyMainWindow(_ pair: MainWindowPair) {
guard case .setCurrentBuffer = pair.action else {
return
}
2017-02-03 00:39:05 +03:00
2017-04-30 10:25:15 +03:00
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
}
2017-04-30 10:25:15 +03:00
NSLog("Serving \(html) on \(server)")
2017-04-02 18:27:12 +03:00
2017-04-30 10:25:15 +03:00
let htmlBasePath = server.deletingLastPathComponent().path
2017-04-02 18:27:12 +03:00
2017-04-30 10:25:15 +03:00
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)
2017-01-25 00:39:19 +03:00
}
2017-04-30 10:25:15 +03:00
fileprivate let server = HttpServer()
fileprivate let githubCssUrl: URL
2017-04-02 18:27:12 +03:00
}