All Projects → PerfectExamples → Perfect-URLRouting

PerfectExamples / Perfect-URLRouting

Licence: Apache-2.0 license
Perfect Example Module: URL Routing

Projects that are alternatives of or similar to Perfect-URLRouting

Perfect-HTTPServer
HTTP server for Perfect.
Stars: ✭ 104 (+420%)
Mutual labels:  perfect, server-side-swift
SwiftString
A comprehensive, lightweight string extension for Swift 3.x & 4.0
Stars: ✭ 117 (+485%)
Mutual labels:  perfect, server-side-swift
Perfect-Thread
Core threading library for Perfect Server Side Swift. Includes support for serial and concurrent thread queues, locks, read/write locks and events.
Stars: ✭ 17 (-15%)
Mutual labels:  perfect, server-side-swift
Perfect-HTTP
Base HTTP Support for Perfect.
Stars: ✭ 29 (+45%)
Mutual labels:  perfect, server-side-swift
Perfect-URL-Shortener
An Example URL Shortener System for Perfect
Stars: ✭ 37 (+85%)
Mutual labels:  perfect, server-side-swift
Perfect-Weather
Demonstrate using URL Routes & variables, Fetching of remote data from API's as JSON, reading and transforming to data more appropriately consumable by an API client.
Stars: ✭ 32 (+60%)
Mutual labels:  perfect, server-side-swift
Perfect-Zip
Perfect Zip compression utility.
Stars: ✭ 20 (+0%)
Mutual labels:  perfect, server-side-swift
Perfect-Authentication
OAuth2 Implementations with Facebook, Google, LinkedIn, Slack, SalesForce and GitHub providers.
Stars: ✭ 14 (-30%)
Mutual labels:  perfect, server-side-swift
Perfect
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)
Stars: ✭ 13,890 (+69350%)
Mutual labels:  perfect, server-side-swift
Perfect-JSON-API
An Example JSON API for Perfect
Stars: ✭ 43 (+115%)
Mutual labels:  perfect, server-side-swift
Perfect-XML
XML support for Perfect.
Stars: ✭ 16 (-20%)
Mutual labels:  perfect, server-side-swift
Perfect-WebSocketsServer
Perfect Example Module: WebSockets Server
Stars: ✭ 34 (+70%)
Mutual labels:  perfect, server-side-swift
Perfect-SMTP
SMTP Client for Perfect.
Stars: ✭ 19 (-5%)
Mutual labels:  perfect, server-side-swift
now-swift-example
Example for use Kitura framework and swift lang with https://zeit.co/now.
Stars: ✭ 18 (-10%)
Mutual labels:  server-side-swift
Swift-FS-China
Swift China Community
Stars: ✭ 44 (+120%)
Mutual labels:  perfect
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (+275%)
Mutual labels:  server-side-swift
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (+245%)
Mutual labels:  server-side-swift
Perfect-Crypto
Cryptographic Operations
Stars: ✭ 27 (+35%)
Mutual labels:  server-side-swift
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+235%)
Mutual labels:  server-side-swift
ZEGBot
Build your Telegram Bot with Swift! (works on macOS / Ubuntu)
Stars: ✭ 52 (+160%)
Mutual labels:  server-side-swift

URL Routing

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 3.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

This example illustrates how to set up URL routing to direct requests to your custom handlers.

To use the example with Swift Package Manager, type swift build and then run .build/debug/URLRouting.

To use the example with Xcode, run the URL Routing target. This will launch the Perfect HTTP Server.

Navigate in your web browser to http://localhost:8181/. Experiment with the URL routes which are added.

Issues

We are transitioning to using JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled.

If you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to http://jira.perfect.org:8080/servicedesk/customer/portal/1 and raise it.

A comprehensive list of open issues can be found at http://jira.perfect.org:8080/projects/ISS/issues

Enabling URL Routing

The following code is taken from the example project and shows how to create and add routes to a server.

var routes = Routes()

routes.add(method: .get, uris: ["/", "index.html"], handler: indexHandler)
routes.add(method: .get, uri: "/foo/*/baz", handler: echoHandler)
routes.add(method: .get, uri: "/foo/bar/baz", handler: echoHandler)
routes.add(method: .get, uri: "/user/{id}/baz", handler: echo2Handler)
routes.add(method: .get, uri: "/user/{id}", handler: echo2Handler)
routes.add(method: .post, uri: "/user/{id}/baz", handler: echo3Handler)

// Test this one via command line with curl:
// curl --data "{\"id\":123}" http://0.0.0.0:8181/raw --header "Content-Type:application/json"
routes.add(method: .post, uri: "/raw", handler: rawPOSTHandler)

// Trailing wildcard matches any path
routes.add(method: .get, uri: "**", handler: echo4Handler)

// Routes with a base URI

// Create routes for version 1 API
var api = Routes()
api.add(method: .get, uri: "/call1", handler: { _, response in
	response.setBody(string: "API CALL 1")
	response.completed()
})
api.add(method: .get, uri: "/call2", handler: { _, response in
	response.setBody(string: "API CALL 2")
	response.completed()
})

// API version 1
var api1Routes = Routes(baseUri: "/v1")
// API version 2
var api2Routes = Routes(baseUri: "/v2")

// Add the main API calls to version 1
api1Routes.add(routes: api)
// Add the main API calls to version 2
api2Routes.add(routes: api)
// Update the call2 API
api2Routes.add(method: .get, uri: "/call2", handler: { _, response in
	response.setBody(string: "API v2 CALL 2")
	response.completed()
})

// Add both versions to the main server routes
routes.add(routes: api1Routes)
routes.add(routes: api2Routes)

// Check the console to see the logical structure of what was installed.
print("\(routes.navigator.description)")

// 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)")
}

Handling Requests

The example EchoHandler consists of the following.

func echoHandler(request: HTTPRequest, _ response: HTTPResponse) {
	response.appendBody(string: "Echo handler: You accessed path \(request.path) with variables \(request.urlVariables)")
	response.completed()
}

Using Apache

The following Apache conf snippet can be used to pipe requests for non-existent files through to Perfect when using the URL routing system.

	RewriteEngine on
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule (.*) - [L,NS,H=perfect-handler]

Further Information

For more information on the Perfect project, please visit perfect.org.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].