Skip to content

HTTP 1.1 server for Perfect.

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.zh_CN
Notifications You must be signed in to change notification settings

kjessup/Perfect-HTTPServer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Perfect-HTTPServer 简体中文

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

HTTP/2, 1.1 Server for Perfect

This repository contains the main HTTP 1.1 & HTTP/2 server.

If you are using this server for your Perfect Server-Side Swift project then this will be the main dependency for your project.

.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2)

If you are starting out with Perfect look at the main Perfect repository for details.

If you are beginning a new project with Perfect look at the PerfectTemplate project for starter instructions.

When building on Linux, OpenSSL 1.0.2+ is required for this package. On Ubuntu 14 or some Debian distributions you will need to update your OpenSSL before this package will build.

QuickStart

The following will clone an empty starter project:

git clone https://github.com/PerfectlySoft/PerfectTemplate.git
cd PerfectTemplate

Verify the Package.swift file contains the dependency:

let package = Package(
 name: "PerfectTemplate",
 targets: [],
 dependencies: [
     .Package(url:"https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2)
    ]
)

The name in the Package object in your Package.swift file will be the executable name for your project.

Create the Xcode project:

swift package generate-xcodeproj

The Swift Package Manager will download the required dependencies into a Packages directory and build an appropriate Xcode Project file.

Open the generated PerfectTemplate.xcodeproj file in Xcode.

The project can now build in Xcode and start a server on localhost port 8181.

Important: When a dependancy has been added to the project, the Swift Package Manager must be invoked to generate a new Xcode project file. Be aware that any customizations that have been made to this file will be lost.

Creating an HTTPServer and registering webroot

Open main.swift from the Sources directory and confirm the following code is in place:

verify import statements include

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer

Create an instance of HTTPServer and add routes:

// Create HTTP server.
let server = HTTPServer()

// Register your own routes and handlers
var routes = Routes()
routes.add(method: .get, uri: "/", handler: {
		request, response in
		response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
		response.completed()
	}
)

// Add the routes to the server.
server.addRoutes(routes)

Verify server settings:

// Set a listen port of 8181
server.serverPort = 8181

// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"

Gather command line options and further configure the server. Run the server with --help to see the list of supported arguments. Command line arguments will supplant any of the values set above.

The arguments.swift file provides handlers for command line arguments as well as the configureServer function. Call this to handle any CLI arguments before starting the server process.

configureServer(server)

This code block will launch the server. Remember that any command after server.start() will not be reached.

do {
	// Launch the HTTP server.
	try server.start()
    
} catch PerfectError.networkError(let err, let msg) {
	print("Network error thrown: \(err) \(msg)")
}

In Xcode, you will need to select the executable scheme before you launch the server. This selection will need to be redone each time you generate an Xcode project file from the command line. Set the working directory to be your Project directory by choosing Edit Scheme, select Run, and under Options, check use custom working directory, then choose the project root. Again, this will need to be updated anytime you regenerate your project file.

In Xcode choose run to build and run your server. Test by going to 0.0.0.0:8181 in your browser to see the Hello World message. The server will also serve any static files you place in the webroot directory.

=======

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

About

HTTP 1.1 server for Perfect.

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.zh_CN

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 62.2%
  • Swift 9.4%
  • Assembly 7.1%
  • Ada 4.5%
  • Pascal 3.8%
  • C++ 3.4%
  • Other 9.6%