https://github.com/perfectlysoft/perfecttemplate
Empty Perfect Starter Project.
https://github.com/perfectlysoft/perfecttemplate
http http-server server-side-swift swift
Last synced: 10 months ago
JSON representation
Empty Perfect Starter Project.
- Host: GitHub
- URL: https://github.com/perfectlysoft/perfecttemplate
- Owner: PerfectlySoft
- License: apache-2.0
- Created: 2016-04-20T22:25:39.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-05-10T01:50:34.000Z (over 6 years ago)
- Last Synced: 2025-04-10T00:18:29.635Z (10 months ago)
- Topics: http, http-server, server-side-swift, swift
- Language: Swift
- Homepage: https://www.perfect.org
- Size: 60.5 KB
- Stars: 222
- Watchers: 20
- Forks: 84
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PerfectTemplate [简体中文](README.zh_CN.md)
Perfect Empty Starter Project
This repository holds a blank Perfect project which can be cloned to serve as a starter for new work. It builds with Swift Package Manager and produces a stand-alone HTTP executable.
## Compatibility with Swift
The master branch of this project currently compiles with **Xcode 10** and the **Swift 4.1** or higher toolchain on Ubuntu.
## Building & Running
The following will clone and build an empty starter project and launch the server on port 8181.
```
git clone https://github.com/PerfectlySoft/PerfectTemplate.git
cd PerfectTemplate
swift run
```
You should see the following output:
```
[INFO] Starting HTTP server localhost on 0.0.0.0:8181
```
This means the server is running and waiting for connections. Access [http://localhost:8181/](http://127.0.0.1:8181/) to see the greeting. Hit control-c to terminate the server.
## Starter Content
The template file contains a simple "hello, world!" request handler and shows how to serve static files, and compress outgoing content.
```swift
import PerfectHTTP
import PerfectHTTPServer
// An example request handler.
// This 'handler' function can be referenced directly in the configuration below.
func handler(request: HTTPRequest, response: HTTPResponse) {
// Respond with a simple message.
response.setHeader(.contentType, value: "text/html")
response.appendBody(string: "Hello, world!Hello, world!")
// Ensure that response.completed() is called when your processing is done.
response.completed()
}
// Configure one server which:
// * Serves the hello world message at :/
// * Serves static files out of the "./webroot"
// directory (which must be located in the current working directory).
// * Performs content compression on outgoing data when appropriate.
var routes = Routes()
routes.add(method: .get, uri: "/", handler: handler)
routes.add(method: .get, uri: "/**",
handler: StaticFileHandler(documentRoot: "./webroot", allowResponseFilters: true).handleRequest)
try HTTPServer.launch(name: "localhost",
port: 8181,
routes: routes,
responseFilters: [
(PerfectHTTPServer.HTTPFilter.contentCompression(data: [:]), HTTPFilterPriority.high)])
```
## Further Information
For more information on the Perfect project, please visit [perfect.org](http://perfect.org).