Skip to content

Commit 79662fa

Browse files
committed
Merge branch 'develop'
2 parents 70af1a9 + 96bf7ef commit 79662fa

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let package = Package(
1414
.executable(name: "httpd-helloworld", targets: [ "httpd-helloworld" ]),
1515
.executable(name: "connect-static", targets: [ "connect-static" ]),
1616
.executable(name: "express-simple", targets: [ "express-simple" ]),
17+
.executable(name: "httpproxy", targets: [ "httpproxy" ]),
1718
.executable(name: "servedocc", targets: [ "servedocc" ]),
1819
.executable(name: "todomvc", targets: [ "todomvc" ]),
1920
.executable(name : "express-simple-lambda",
@@ -24,7 +25,7 @@ let package = Package(
2425
// A lot of packages for demonstration purposes, only add what you
2526
// actually need in your own project.
2627
.package(url: "https://github.com/Macro-swift/Macro.git",
27-
from: "0.8.8"),
28+
from: "0.8.10"),
2829
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
2930
from: "0.8.6"),
3031
.package(url: "https://github.com/Macro-swift/MacroLambda.git",
@@ -35,6 +36,7 @@ let package = Package(
3536

3637
targets: [
3738
.target(name: "httpd-helloworld", dependencies: [ "Macro" ]),
39+
.target(name: "httpproxy", dependencies: [ "Macro" ]),
3840
.target(name: "connect-static", dependencies: [ "MacroExpress" ]),
3941
.target(name: "express-simple", dependencies: [ "MacroExpress", "cows" ]),
4042
.target(name: "servedocc", dependencies: [ "MacroExpress" ]),

Sources/httpd-helloworld/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/swift sh
22

3-
import Macro // @Macro-swift ~> 0.5.4
3+
import Macro // @Macro-swift
44

55
http.createServer { req, res in
66
// log request
7-
console.log("\(req.method) \(req.url)")
7+
req.log.log("\(req.method) \(req.url)")
88

99
// set content type to HTML
1010
res.writeHead(200, [ "Content-Type": "text/html" ])
@@ -21,6 +21,6 @@ http.createServer { req, res in
2121
// finish up
2222
res.end()
2323
}
24-
.listen(1337, "0.0.0.0") { server in
25-
console.log("Server listening on http://0.0.0.0:1337/")
24+
.listen(1337) { server in
25+
server.log.log("Server listening on http://*:1337/")
2626
}

Sources/httpproxy/main.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/swift sh
2+
3+
import Macro // @Macro-swift
4+
import Foundation
5+
6+
http.createServer { req, res in
7+
// log request
8+
req.log.log("\(req.method) \(req.url)")
9+
10+
guard let url = URL(string: req.url),
11+
let scheme = url.scheme, let host = url.host else
12+
{
13+
req.log.error("Invalid URL:", req.url)
14+
res.writeHead(400)
15+
res.end()
16+
return
17+
}
18+
19+
let options = http.ClientRequestOptions(
20+
headers : req.headers, // TODO: Add 'Via' and such, drop the proper ones
21+
protocol : "\(scheme):",
22+
host : host,
23+
method : req.method,
24+
path : url.path
25+
)
26+
27+
let proxiedRequest = http.request(options) { proxiedResponse in
28+
res.writeHead(proxiedResponse.status,
29+
headers: proxiedResponse.headers)
30+
31+
// send proxied response body to the original client
32+
proxiedResponse
33+
.pipe(res)
34+
.onError { error in
35+
req.log.error("proxy response delivery failed:", error)
36+
}
37+
}
38+
.onError { error in
39+
req.log.error("proxy request delivery failed:", error)
40+
res.writeHead(500)
41+
res.end()
42+
}
43+
44+
// Send the request body to the target server
45+
_ = req.pipe(proxiedRequest)
46+
}
47+
.listen(1337) { server in
48+
server.log.log("Server listening on http://0.0.0.0:1337/")
49+
}

Sources/servedocc/main.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ let indexPath = archivePath + "/index.html"
2929
let docPath = archivePath + "/data/documentation"
3030

3131
guard fs.existsSync(archivePath) else {
32-
print("Specified file does not exist:", archivePath)
32+
console.error("Specified file does not exist:", archivePath)
3333
process.exit(2)
3434
}
3535
guard fs.existsSync(indexPath), fs.existsSync(docPath) else {
36-
print("File does not look like a DocC archive:", archivePath)
36+
console.error("File does not look like a DocC archive:", archivePath)
3737
process.exit(3)
3838
}
3939

4040
guard let dataIndex = (try? fs.readdirSync(docPath))?
4141
.first(where: { $0.hasSuffix(".json")} )
4242
else {
43-
print("File does not look like a DocC archive, missing data index:",
44-
archivePath)
43+
console.error("File does not look like a DocC archive, missing data index:",
44+
archivePath)
4545
process.exit(3)
4646
}
4747
let dataIndexPath = docPath + "/" + dataIndex
4848

4949

50-
// MARK: - Serve Individual
50+
// MARK: - Serve Individual Files
5151

5252
/// This serves individual, fixed files.
5353
func serveFile(_ path: String) -> Middleware {

0 commit comments

Comments
 (0)