https://github.com/codefiesta/demo-server
Swift Demo Server using Perfect
https://github.com/codefiesta/demo-server
Last synced: 7 months ago
JSON representation
Swift Demo Server using Perfect
- Host: GitHub
- URL: https://github.com/codefiesta/demo-server
- Owner: codefiesta
- Created: 2017-01-17T05:46:01.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-18T06:49:31.000Z (about 9 years ago)
- Last Synced: 2025-02-15T13:46:40.113Z (12 months ago)
- Language: Swift
- Size: 119 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DemoServer using PerfectTemplate
# Running Locally
To run this project with Swift Package Manager, type ```swift build``` and then run ``` .build/debug/DemoServer```.
To use the example with Xcode, run the **DemoServer** target. This will launch the Perfect HTTP Server.
Navigate in your web browser to [http://localhost:8181/](http://localhost:8181/). Currently the only routes defined are the ***/login*** and ***/secure*** routes.
## URL Routing
The routes are built from the Router.swift file
```swift
var routes = Routes()
routes.add(method: .post, uri: "/login", handler: Router.loginHandler)
routes.add(method: .get, uri: "/secure", handler: Router.secureHandler)
// Test this the server status via command line with curl:
// curl http://0.0.0.0:8181/status --header "Content-Type:application/json"
routes.add(method: .get, uri: "/status", handler: Router.statusHandler)
// Create server object.
let server = HTTPServer()
// Listen on port 8181.
server.serverPort = 8181
// Add our routes.
server.addRoutes(routes)
do {
// Launch the HTTP server
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}
```