-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.swift
64 lines (49 loc) · 1.28 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Noze.io Simple Connect based WebServer
// - to compile in Swift 3 invoke: swift build
// - to run result: .build/debug/connect-git
import http
import connect
import console
import child_process
let app = connect()
// Middleware: logs the request
app.use(logger("dev"))
let navigation = [
( "/", "home" ),
( "/status", "git status" ),
( "/log", "git log" )
]
// Middleware: add a common header
app.use { req, res, next in
res.setHeader("Content-Type", "text/html; charset=utf-8")
res.write(
"<h4>URL: \(req.url)</h4>" +
"<div>"
)
let navLinks = navigation.map { "<a href=\"\($0)\">\($1)</a>" }
res.write(navLinks.joined(separator: " | "))
res.write("</div><hr />")
next()
}
// Middleware: git status - output in pre
app.use("/status") { req, res, next in
res.write("<h3>git status</h3>")
res.write("<pre>")
spawn("git", "status").stdout!
.pipe(res, endOnFinish: false)
.unpipePromise
.then {
res.write("</pre>")
res.end()
}
}
// Middleware: git log - parse response and output in table
app.use("/log", gitLog)
// Middleware: Homepage - final all-match
app.use { req, res, next in
res.end("<h3>Welcome to Noze.io!</h3>")
}
// and run the server
app.listen(1337) {
print("Server listening: \($0)")
}