PerfectCORS is a Swift package which enables CORS in server side Swift projects hosted by Perfect server.
To install PerfectCORS with package manager add dependencies to your Package.swift
file.
import PackageDescription
let package = Package(
name: "YourApp",
products: [
.library(name: "YourApp", targets: ["YourApp"]),
],
dependencies: [
.package(url: "https://github.com/mczachurski/PerfectCORS.git", from: "1.2.1")
],
targets: [
.target(name: "YourApp", dependencies: ["PerfectCORS"]),
.testTarget(name: "YourAppTest", dependencies: ["YourApp"]),
]
)
import PerfectHTTP
import PerfectHTTPServer
// Register your own routes and handlers
var routes = Routes()
// Create CORS filter.
let corsFilter = CORSFilter()
let requestFilters: [(HTTPRequestFilter, HTTPFilterPriority)] = [
(corsFilter, HTTPFilterPriority.high)
]
do {
let server = HTTPServer()
server.setRequestFilters(requestFilters)
server.serverName = "www.example.ca"
server.serverPort = 8181
server.addRoutes(routes)
// Launch the HTTP server.
try server.start()
} catch {
fatalError("\(error)") // fatal error launching one of the servers
}
import PerfectHTTP
import PerfectHTTPServer
// Register your own routes and handlers
var routes = Routes()
// Create CORS filter.
let corsFilter = CORSFilter(origin: ["http://example.com"], methods: [.get, .post])
let requestFilters: [(HTTPRequestFilter, HTTPFilterPriority)] = [
(corsFilter, HTTPFilterPriority.high)
]
do {
let server = HTTPServer()
server.setRequestFilters(requestFilters)
server.serverName = "www.example.ca"
server.serverPort = 8181
server.addRoutes(routes)
// Launch the HTTP server.
try server.start()
} catch {
fatalError("\(error)") // fatal error launching one of the servers
}
You can set custom CORS parameters during creating CORS filter. Below there is description of all parameters.
Property | Type | Description |
---|---|---|
origin | [String]? | List of allowed origins. Configures the values returned in Access-Control-Allow-Origin header (more information). If list is empty all origins are allowed. |
methods | [HTTPMethod]? | List of allowed HTTP methods. Configures the values returned in Access-Control-Allow-Methods header (more information). If list is empty methods: GET , POST , PUT , DELETE , PATCH are allowed. |
allowedHeaders | [HTTPRequestHeader.Name]? | List of allowed HTTP headers. Configures the values returned in Access-Control-Allow-Headers header (more information). If list is empty all headers from request are allowed. |
exposedHeaders | [HTTPResponseHeader.Name]? | List of exposed HTTP headers. Configures the values returned in Access-Control-Expose-Headers header (more information). If list is empty no custom headers are exposed. |
maxAge | Double? | Information about CORS cache. Configures the value returned in Access-Control-Max-Age header. Indicates how long,the results of a preflight request (that is the information contained in the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers) can be cached (more information). If is not specified header is omitted. |
credentials | Bool? | Information about exposing credentials header. Configures the value returned in Access-Control-Allow-Credentials. The Access-Control-Allow-Credentials response header indicates whether or not the,response to the request can be exposed to the page. It can be exposed when the true value is returned (more information). If is not specified header is omitted. |
This project is licensed under the terms of the MIT license.