Skip to content
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

Lw/public ip argument #86

Merged
merged 6 commits into from
Feb 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 7 additions & 24 deletions Sources/Vapor/Core/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,6 @@ public class Application {
routes.forEach(router.register)
}

/**
Returns the string value of an
argument passed to the executable
in the format --name=value
*/
func argument(name: String) -> String? {
for argument in Process.arguments {
if argument.hasPrefix("--\(name)=") {
return argument.split("=")[1]
}
}

return nil
}

/**
Boots the chosen server driver and
runs on the supplied port.
Expand All @@ -109,27 +94,25 @@ public class Application {
var port = inPort

//grab process args
if let workDir = self.argument("workDir") {
print("Work dir override: \(workDir)")
if let workDir = Process.valueFor(argument: "workDir") {
Log.info("Work dir override: \(workDir)")
self.dynamicType.workDir = workDir
}

if let portString = self.argument("port") {
if let portInt = Int(portString) {
print("Port override: \(portInt)")
port = portInt
}
if let portString = Process.valueFor(argument: "port"), let portInt = Int(portString) {
Log.info("Port override: \(portInt)")
port = portInt
}


do {
try self.server.boot(port: port)

print("Server has started on port \(port)")
Log.info("Server has started on port \(port)")

self.loop()
} catch {
print("Server start error: \(error)")
Log.info("Server start error: \(error)")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Fixes/LinuxFixes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func dispatch_async( queue: Int, _ block: () -> () ) {
pthread_detach( pthread )
}
else {
print( "pthread_create() error" )
Log.error( "pthread_create() error" )
}
}

Expand Down
30 changes: 30 additions & 0 deletions Sources/Vapor/Process/Process.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Process.swift
// Vapor
//
// Created by Logan Wright on 2/27/16.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import Foundation

extension Process {

/**
Returns the string value of an
argument passed to the executable
in the format --name=value

- parameter argument: the name of the argument to get the value for
- parameter arguments: arguments to search within. Primarily used for testing through injection

- returns: the value matching the argument if possible
*/
internal static func valueFor(argument name: String, inArguments arguments: [String] = Process.arguments) -> String? {
for argument in arguments where argument.hasPrefix("--\(name)=") {
return argument.split("=").last
}
return nil
}

}
7 changes: 4 additions & 3 deletions Sources/Vapor/Server/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum SocketError: ErrorType {
}

public class Socket: Hashable, Equatable {

public class func tcpSocketForListen(port: in_port_t, maxPendingConnection: Int32 = SOMAXCONN) throws -> Socket {

#if os(Linux)
Expand All @@ -69,18 +69,19 @@ public class Socket: Hashable, Equatable {
}
Socket.setNoSigPipe(socketFileDescriptor)

let ip = Process.valueFor(argument: "ip") ?? "0.0.0.0"
#if os(Linux)
var addr = sockaddr_in()
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Socket.htonsPort(port)
addr.sin_addr = in_addr(s_addr: in_addr_t(0))
addr.sin_addr = in_addr(s_addr: inet_addr(ip))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
#else
var addr = sockaddr_in()
addr.sin_len = __uint8_t(sizeof(sockaddr_in))
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Socket.htonsPort(port)
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
addr.sin_addr = in_addr(s_addr: inet_addr(ip))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
#endif

Expand Down
36 changes: 36 additions & 0 deletions Tests/Vapor/ProcessTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ProcessTests.swift
// Vapor
//
// Created by Logan Wright on 2/27/16.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import XCTest
@testable import Vapor

#if os(Linux)
extension ProcessTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testArgumentExtraction", testArgumentExtraction)
]
}
}
#endif

class ProcessTests: XCTestCase {

func testArgumentExtraction() {
let testArguments = ["--ip=123.45.1.6", "--port=8080", "--workDir=WorkDirectory"]

let ip = Process.valueFor(argument: "ip", inArguments: testArguments)
XCTAssert(ip == "123.45.1.6")

let port = Process.valueFor(argument: "port", inArguments: testArguments)
XCTAssert(port == "8080")

let workDir = Process.valueFor(argument: "workDir", inArguments: testArguments)
XCTAssert(workDir == "WorkDirectory")
}
}
17 changes: 17 additions & 0 deletions XcodeProject/Vapor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
3A91D3F71C7F6B6600EA3CA0 /* StringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C352E6E21C62BC6A00E26467 /* StringExtensions.swift */; };
3AEEF41D1C7F6D4A0099CBAE /* Vapor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A91D3CC1C7F6AF900EA3CA0 /* Vapor.framework */; };
3AEEF4531C7F72C30099CBAE /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AEEF4181C7F6CBF0099CBAE /* main.swift */; };
800EFA561C822EE200F6C066 /* Process.swift in Sources */ = {isa = PBXBuildFile; fileRef = 800EFA551C822EE200F6C066 /* Process.swift */; };
800EFA581C8231EE00F6C066 /* ProcessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 800EFA571C8231EE00F6C066 /* ProcessTests.swift */; };
C3433FD41C7D119D009F0876 /* HashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3433FD21C7D119D009F0876 /* HashTests.swift */; };
C38808641C62C0390067DADD /* ResponseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C38808631C62C0390067DADD /* ResponseTests.swift */; };
C3943DC31C7663E80014F4EE /* RouterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3943DC21C7663E80014F4EE /* RouterTests.swift */; };
Expand Down Expand Up @@ -96,6 +98,8 @@
3A91D3D01C7F6AF900EA3CA0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3AEEF4181C7F6CBF0099CBAE /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = main.swift; path = ../Sources/VaporDev/main.swift; sourceTree = SOURCE_ROOT; };
3AEEF44A1C7F71080099CBAE /* VaporDev */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = VaporDev; sourceTree = BUILT_PRODUCTS_DIR; };
800EFA551C822EE200F6C066 /* Process.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Process.swift; sourceTree = "<group>"; };
800EFA571C8231EE00F6C066 /* ProcessTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProcessTests.swift; path = ../../Tests/Vapor/ProcessTests.swift; sourceTree = "<group>"; };
802030001C77C956009B8655 /* Vapor+JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Vapor+JSON.swift"; path = "../Sources/Vapor/JSON/Vapor+JSON.swift"; sourceTree = SOURCE_ROOT; };
802030101C78CAC2009B8655 /* Branch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Branch.swift; path = ../Sources/Vapor/Router/Branch.swift; sourceTree = SOURCE_ROOT; };
802030161C78CAE1009B8655 /* BranchRouter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BranchRouter.swift; path = ../Sources/Vapor/Router/BranchRouter.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -204,6 +208,7 @@
C3EDEA821C68419B00754727 /* Crypto */,
80202FFF1C77C94C009B8655 /* JSON */,
C304C8AF1C63051E00058C92 /* Fixes */,
800EFA541C822ECF00F6C066 /* Process */,
3A91D3CE1C7F6AF900EA3CA0 /* Vapor.h */,
3A91D3D01C7F6AF900EA3CA0 /* Info.plist */,
);
Expand All @@ -230,6 +235,15 @@
name = Dependencies;
sourceTree = "<group>";
};
800EFA541C822ECF00F6C066 /* Process */ = {
isa = PBXGroup;
children = (
800EFA551C822EE200F6C066 /* Process.swift */,
);
name = Process;
path = ../../Sources/Vapor/Process;
sourceTree = "<group>";
};
80202FFF1C77C94C009B8655 /* JSON */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -358,6 +372,7 @@
C38808631C62C0390067DADD /* ResponseTests.swift */,
C3943DC21C7663E80014F4EE /* RouterTests.swift */,
3492B2EE1C787DD600D8E588 /* RouteTests.swift */,
800EFA571C8231EE00F6C066 /* ProcessTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -612,6 +627,7 @@
3A91D3F71C7F6B6600EA3CA0 /* StringExtensions.swift in Sources */,
3A91D3E01C7F6B6600EA3CA0 /* BranchRouter.swift in Sources */,
3A91D3E51C7F6B6600EA3CA0 /* Response.swift in Sources */,
800EFA561C822EE200F6C066 /* Process.swift in Sources */,
3A91D3EF1C7F6B6600EA3CA0 /* Provider.swift in Sources */,
3A659FC51C80368F00C105F5 /* ResourceController.swift in Sources */,
3A91D3DA1C7F6B6600EA3CA0 /* Application+Route.swift in Sources */,
Expand Down Expand Up @@ -654,6 +670,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
800EFA581C8231EE00F6C066 /* ProcessTests.swift in Sources */,
C38808641C62C0390067DADD /* ResponseTests.swift in Sources */,
3492B2EF1C787DD600D8E588 /* RouteTests.swift in Sources */,
C3F683361C7D1EF300033AA2 /* ControllerTests.swift in Sources */,
Expand Down