-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.swift
executable file
·54 lines (37 loc) · 1.18 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
#!/usr/bin/swift sh
import MacroExpress // @Macro-swift
import Northwind // @Lighter-swift/NorthwindSQLite.swift
dotenv.config()
let db = Northwind.module! // This grabs the read-only DB embedded in the pkg
let app = express()
app.use(logger("dev"))
app.use(bodyParser.urlencoded())
app.set("view engine", "html")
app.set("views", __dirname() + "/views")
// MARK: - Setup Routes
// This is an explicit registration for a specific record fetch.
// curl http://localhost:1337/api/products | jq .
app.get("/api/products") { _, res in
res.send(try db.products.fetch())
}
// But w/ 5.7+ we can also dynamically register the typesafe handlers.
// curl http://localhost:1337/api/Supplier | jq .
app.get(db, prefix: "/api/")
// Those hook up the HTML pages/templates.
app
.get("/products.html", products)
.get("/products/:id/", product)
app.get("/") { _, res in
res.render("index")
}
// MARK: - Open URL in Browser in startup
#if DEBUG && os(macOS) && canImport(AppKit)
import AppKit
setTimeout(100) {
NSWorkspace.shared.open(URL(string: "http://localhost:1337/")!)
}
#endif
// MARK: - Start Server
app.listen(1337) {
console.log("Server listening on http://localhost:1337")
}