0001    #if os(Linux)
0002    import Glibc
0003    #else
0004    import Darwin
0005    #endif
0006    
0007    import Nest
0008    import Commander
0009    import Inquiline
0010    
0011    
0012    extension Address : ArgumentConvertible {
0013      public init(parser: ArgumentParser) throws {
0014        if let value = parser.shift() {
0015          if value.hasPrefix("unix:") {
0016            let prefixEnd = value.startIndex.advancedBy(5)
0017            self = .UNIX(path: value[prefixEnd ..< value.endIndex])
0018          } else {
0019            let components = value.characters.split(":").map(String.init)
0020            if components.count != 2 {
0021              throw ArgumentError.InvalidType(value: value, type: "hostname and port separated by `:`.", argument: nil)
0022            }
0023    
0024            if let port = UInt16(components[1]) {
0025              self = .IP(hostname: components[0], port: port)
0026            } else {
0027              throw ArgumentError.InvalidType(value: components[1], type: "number", argument: "port")
0028            }
0029          }
0030        } else {
0031          throw ArgumentError.MissingValue(argument: nil)
0032        }
0033      }
0034    }
0035    
0036    
0037    extension ArgumentConvertible {
0038      init(string: String) throws {
0039        try self.init(parser: ArgumentParser(arguments: [string]))
0040      }
0041    }
0042    
0043    
0044    class MultiOption
Curassow.swift:88
    MultiOption("bind", [Address.IP(hostname: "0.0.0.0", port: 8000)], description: "The address to bind sockets."),
<T
Curassow.swift:45
  typealias ValueType = [T]
Curassow.swift:64
      let value = try T(string: value)
Curassow.swift:70
        let value = try T(string: value)
: ArgumentConvertible> : ArgumentDescriptor { 0045 typealias ValueType
Curassow.swift:50
  let `default`: ValueType
Curassow.swift:53
  init(_ name: String, _ `default`: ValueType, flag: Character? = nil, description: String? = nil) {
Curassow.swift:60
  func parse(parser: ArgumentParser) throws -> ValueType {
Curassow.swift:61
    var options: ValueType = []
= [T] 0046 0047 let name
Curassow.swift:54
    self.name = name
: String 0048 let flag
Curassow.swift:55
    self.flag = flag
Curassow.swift:68
    if let flag = flag {
: Character? 0049 let description
Curassow.swift:56
    self.description = description
: String? 0050 let `default`
Curassow.swift:57
    self.`default` = `default`
: ValueType 0051 var type: ArgumentType { return .Option } 0052 0053 init(_ name: String, _ `default`: ValueType, flag: Character? = nil, description: String? = nil) { 0054 self.name = name 0055 self.flag = flag 0056 self.description = description 0057 self.`default` = `default` 0058 } 0059 0060 func parse(parser: ArgumentParser) throws -> ValueType { 0061 var options: ValueType = [] 0062 0063 while let value = try parser.shiftValueForOption(name) { 0064 let value = try T(string: value) 0065 options.append(value) 0066 } 0067 0068 if let flag = flag { 0069 while let value = try parser.shiftValueForFlag(flag) { 0070 let value = try T(string: value) 0071 options.append(value) 0072 } 0073 } 0074 0075 if options.isEmpty { 0076 return `default` 0077 } 0078 0079 return options 0080 } 0081 } 0082 0083 0084 @noreturn public func serve(closure: RequestType -> ResponseType) { 0085 command( 0086 Option("worker-type", "sync"), 0087 Option("workers", 1, description: "The number of processes for handling requests."), 0088 MultiOption("bind", [Address.IP(hostname: "0.0.0.0", port: 8000)], description: "The address to bind sockets."), 0089 Option("timeout", 30, description: "Amount of seconds to wait on a worker without activity before killing and restarting the worker.") 0090 ) { workerType, workers, addresses, timeout in 0091 var configuration = Configuration() 0092 configuration.addresses = addresses 0093 configuration.timeout = timeout 0094 0095 if workerType == "synchronous" || workerType == "sync" { 0096 let arbiter = Arbiter<SynchronousWorker>(configuration: configuration, workers: workers, application: closure) 0097 try arbiter.run() 0098 } else { 0099 throw ArgumentError.InvalidType(value: workerType, type: "worker type", argument: "worker-type") 0100 } 0101 }.run() 0102 } 0103