|
| 1 | +#!/usr/bin/swift sh |
| 2 | + |
| 3 | +import MacroExpress // @Macro-swift |
| 4 | + |
| 5 | + |
| 6 | +// MARK: - Parse Commandline Arguments & Usage |
| 7 | + |
| 8 | +func usage() { |
| 9 | + let tool = path.basename(process.argv.first ?? "servedocc") |
| 10 | + print( |
| 11 | + """ |
| 12 | + Usage: \(tool) <docc archive folder> |
| 13 | + |
| 14 | + Example: |
| 15 | + |
| 16 | + \(tool) SlothCreator.doccarchive |
| 17 | + |
| 18 | + """ |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +guard process.argv.count == 2 else { |
| 23 | + usage() |
| 24 | + process.exit(1) |
| 25 | +} |
| 26 | + |
| 27 | +let archivePath = process.argv[1] |
| 28 | +let indexPath = archivePath + "/index.html" |
| 29 | +let docPath = archivePath + "/data/documentation" |
| 30 | + |
| 31 | +guard fs.existsSync(archivePath) else { |
| 32 | + print("Specified file does not exist:", archivePath) |
| 33 | + process.exit(2) |
| 34 | +} |
| 35 | +guard fs.existsSync(indexPath), fs.existsSync(docPath) else { |
| 36 | + print("File does not look like a DocC archive:", archivePath) |
| 37 | + process.exit(3) |
| 38 | +} |
| 39 | + |
| 40 | +guard let dataIndex = (try? fs.readdirSync(docPath))? |
| 41 | + .first(where: { $0.hasSuffix(".json")} ) |
| 42 | +else { |
| 43 | + print("File does not look like a DocC archive, missing data index:", |
| 44 | + archivePath) |
| 45 | + process.exit(3) |
| 46 | +} |
| 47 | +let dataIndexPath = docPath + "/" + dataIndex |
| 48 | + |
| 49 | + |
| 50 | +// MARK: - Serve Individual |
| 51 | + |
| 52 | +/// This serves individual, fixed files. |
| 53 | +func serveFile(_ path: String) -> Middleware { |
| 54 | + return { req, res, next in |
| 55 | + fs.createReadStream(path) |
| 56 | + .pipe(res) |
| 57 | + .onError { error in |
| 58 | + console.error("Failed to serve \(path):", error) |
| 59 | + res.status(500) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// MARK: - Configure and Start the Server |
| 66 | + |
| 67 | +let staticDirs = [ "css", "data", "downloads", "images", "img", |
| 68 | + "index", "js", "videos" ] |
| 69 | +let staticFiles = [ "favicon.ico", "favicon.svg", "theme-settings.json" ] |
| 70 | + |
| 71 | +let app = express() |
| 72 | +app.use(logger("dev")) |
| 73 | + |
| 74 | +// Map all matching requests to this index.html |
| 75 | +app.get("/documentation/*", serveFile(indexPath)) |
| 76 | +app.get("/tutorials/*", serveFile(indexPath)) |
| 77 | +app.get("/data/documentation.json", serveFile(dataIndexPath)) |
| 78 | + |
| 79 | +for path in staticDirs { // serve the whole directory ("/*" match) |
| 80 | + app.get("/" + path + "/*", serveStatic(archivePath)) |
| 81 | +} |
| 82 | +for path in staticFiles { // just serve the specific file |
| 83 | + app.get("/" + path, serveStatic(archivePath)) |
| 84 | +} |
| 85 | + |
| 86 | +// redirects |
| 87 | +app.get("/tutorials") { _, res, _ in res.redirect("/tutorials/") } |
| 88 | + .get("/documentation") { _, res, _ in res.redirect("/documentation/") } |
| 89 | + .get { _, res, _ in res.redirect("/documentation/") } |
| 90 | + |
| 91 | +app.listen(1337) { |
| 92 | + console.log("Server listening on: http://localhost:1337/") |
| 93 | + console.log("DocC Archive:", archivePath) |
| 94 | +} |
0 commit comments