-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
service provider support #52
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
13b3344
Refactored to use an Application instance
shnhrrsn 561c53f
Added service providers
shnhrrsn ab1ac16
Removed singelton from Application
shnhrrsn 8e85b54
Renamed ServiceProvider to Provider
shnhrrsn f10f574
Updated Application init to include an optional providers parameter
shnhrrsn 28f96a3
Added Application.start(port:) to start the server + auto-boot the app
shnhrrsn a42c608
Refactored providers to use a single boot(application:) class method …
shnhrrsn c663f5d
Added Application + Provider to the Xcode project
shnhrrsn 4e296b2
Merged Server into Application
shnhrrsn f4827d7
move routes to application class
tanner0101 7ab8e22
simplify provider api
tanner0101 3e4fa21
`Request.Handler` cleanup + middleware
tanner0101 ff078c8
re-add boot providers
tanner0101 6a464a2
readme updates
tanner0101 46edf0a
Updated SessionMiddleware to conform to updated Middleware protocol
shnhrrsn cbdb713
updating application integration to use new router driver
tanner0101 b9c7012
request handler comes pre converted
loganwright 81ad1ce
ensuring global Route set as driver by default
loganwright af8a3a2
removing unnecessary response()
loganwright eb165cb
convertiblehandler => Router.Handler
loganwright 9cbfdbd
moving get / post etc. methods back to application and updating surro…
loganwright 145ffc1
Router => Branch Router, Handler moved to application, errors propoga…
loganwright 66c054b
adding host support for desired syntax
loganwright File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import Foundation | ||
|
||
#if os(Linux) | ||
import Glibc | ||
#endif | ||
|
||
public class Application { | ||
public static let VERSION = "0.1.9" | ||
|
||
/** | ||
The router driver is responsible | ||
for returning registered `Route` handlers | ||
for a given request. | ||
*/ | ||
public let router: RouterDriver | ||
|
||
/** | ||
The server driver is responsible | ||
for handling connections on the desired port. | ||
This property is constant since it cannot | ||
be changed after the server has been booted. | ||
*/ | ||
public var server: ServerDriver | ||
|
||
/** | ||
`Middleware` will be applied in the order | ||
it is set in this array. | ||
|
||
Make sure to append your custom `Middleware` | ||
if you don't want to overwrite default behavior. | ||
*/ | ||
public var middleware: [Middleware.Type] | ||
|
||
|
||
/** | ||
Provider classes that have been registered | ||
with this application | ||
*/ | ||
public var providers: [Provider.Type] | ||
|
||
/** | ||
The work directory of your application is | ||
the directory in which your Resources, Public, etc | ||
folders are stored. This is normally `./` if | ||
you are running Vapor using `.build/xxx/App` | ||
*/ | ||
public static var workDir = "./" { | ||
didSet { | ||
if !self.workDir.hasSuffix("/") { | ||
self.workDir += "/" | ||
} | ||
} | ||
} | ||
|
||
internal var host: String = "*" | ||
|
||
/** | ||
Initialize the Application. | ||
*/ | ||
public init(router: RouterDriver = BranchRouter(), server: ServerDriver = SocketServer()) { | ||
self.server = server | ||
self.router = router | ||
|
||
self.middleware = [] | ||
self.providers = [] | ||
|
||
self.middleware.append(SessionMiddleware) | ||
} | ||
|
||
|
||
public func bootProviders() { | ||
for provider in self.providers { | ||
provider.boot(self) | ||
} | ||
} | ||
|
||
|
||
/** | ||
Boots the chosen server driver and | ||
runs on the supplied port. | ||
*/ | ||
public func start(port inPort: Int = 80) { | ||
self.bootProviders() | ||
|
||
self.server.delegate = self | ||
|
||
var port = inPort | ||
|
||
//grab process args | ||
for argument in Process.arguments { | ||
if argument.hasPrefix("--workDir=") { | ||
let workDirString = argument.split("=")[1] | ||
self.dynamicType.workDir = workDirString | ||
print("Work dir override: \(workDirString)") | ||
} else if argument.hasPrefix("--port=") { | ||
let portString = argument.split("=")[1] | ||
if let portInt = Int(portString) { | ||
print("Port override: \(portInt)") | ||
port = portInt | ||
} | ||
} | ||
} | ||
|
||
do { | ||
try self.server.boot(port: port) | ||
|
||
print("Server has started on port \(port)") | ||
|
||
self.loop() | ||
} catch { | ||
print("Server start error: \(error)") | ||
} | ||
} | ||
|
||
/** | ||
Starts an infinite loop to keep the server alive while it | ||
waits for inbound connections. | ||
*/ | ||
func loop() { | ||
#if os(Linux) | ||
while true { | ||
sleep(1) | ||
} | ||
#else | ||
NSRunLoop.mainRunLoop().run() | ||
#endif | ||
} | ||
} | ||
|
||
extension Application: ServerDriverDelegate { | ||
|
||
public func serverDriverDidReceiveRequest(request: Request) -> Response { | ||
var handler: Request.Handler | ||
|
||
// Check in routes | ||
if let routerHandler = router.route(request) { | ||
handler = routerHandler | ||
} else { | ||
// Check in file system | ||
let filePath = self.dynamicType.workDir + "Public" + request.path | ||
|
||
let fileManager = NSFileManager.defaultManager() | ||
var isDir: ObjCBool = false | ||
|
||
if fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) { | ||
// File exists | ||
if let fileBody = NSData(contentsOfFile: filePath) { | ||
var array = [UInt8](count: fileBody.length, repeatedValue: 0) | ||
fileBody.getBytes(&array, length: fileBody.length) | ||
|
||
return Response(status: .OK, data: array, contentType: .Text) | ||
} else { | ||
handler = { _ in | ||
return Response(error: "Could not open file.") | ||
} | ||
} | ||
} else { | ||
// Default not found handler | ||
handler = { _ in | ||
return Response(status: .NotFound, text: "Page not found") | ||
} | ||
} | ||
} | ||
|
||
// Loop through middlewares in order | ||
for middleware in self.middleware { | ||
handler = middleware.handle(handler) | ||
} | ||
|
||
do { | ||
return try handler(request: request) | ||
} catch { | ||
return Response(error: "Server Error: \(error)") | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public protocol Provider { | ||
static func boot(application: Application) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the final catch all. In case no routes, middleware, or providers caught the error, it will be returned as a 500.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, this actually makes the other implementation easier and more concise. Updated based on comments