Repository: vapor/vapor
File: Sources/Vapor/Commands/ServeCommand.swift (Swift)

Code before:
                ?? context.options["bind"]?.split(separator: ":").first.flatMap(String.init),
            port: context.options["port"].flatMap(Int.init)
                // 0.0.0.0:8080, :8080, parse port
                ?? context.options["bind"]?.split(separator: ":").last.flatMap(String.init).flatMap(Int.init)
        )
    }
    
    private func start(hostname: String?, port: Int?) -> EventLoopFuture<Void> {
        // determine which hostname / port to bind to
        let hostname = hostname ?? self.config.hostname
        let port = port ?? self.config.port
        
        // print starting message
        self.console.print("Server starting on ", newLine: false)
        let scheme = config.tlsConfig == nil ? "http" : "https"
        self.console.output("\(scheme)://" + hostname, style: .init(color: .cyan), newLine: false)
        self.console.output(":" + port.description, style: .init(color: .cyan))
        
        let signalQueue = DispatchQueue(label: "codes.vapor.server.shutdown")
        let signalSource = DispatchSource.makeSignalSource(signal: SIGINT, queue: signalQueue)
        signalSource.setEventHandler {
            _ = self.runningServer?.close()
            signalSource.cancel()
        }
        let signalSourceTERM = DispatchSource.makeSignalSource(signal: SIGTERM, queue: signalQueue)
        signalSourceTERM.setEventHandler {
            _ = self.runningServer?.close()
            signalSourceTERM.cancel()
        }
        signal(SIGTERM, SIG_IGN)
        signalSourceTERM.resume()
        
        // start the actual HTTPServer
        let server = HTTPServer(config: self.config, on: self.application.eventLoopGroup)
        let delegate = ServerDelegate(application: self.application)
        return server.start(delegate: delegate).flatMap {
            self.runningServer = server
            return server.onClose.map {
                self.console.print("Server shutting down...")
            }
        }
    }
}


Review comment (suggestion):
Small nitpick, this `signalSource` gets a suffix where as the previous one doesn't. That could be a little confusing. A better solution here might be to embed a small function that configures the signal source for a given signal.

Code after:
                ?? context.options["bind"]?.split(separator: ":").first.flatMap(String.init),
            port: context.options["port"].flatMap(Int.init)
                // 0.0.0.0:8080, :8080, parse port
                ?? context.options["bind"]?.split(separator: ":").last.flatMap(String.init).flatMap(Int.init)
        )
    }
    
    private func start(hostname: String?, port: Int?) -> EventLoopFuture<Void> {
        // determine which hostname / port to bind to
        let hostname = hostname ?? self.config.hostname
        let port = port ?? self.config.port
        
        // print starting message
        self.console.print("Server starting on ", newLine: false)
        let scheme = config.tlsConfig == nil ? "http" : "https"
        self.console.output("\(scheme)://" + hostname, style: .init(color: .cyan), newLine: false)
        self.console.output(":" + port.description, style: .init(color: .cyan))
        
        let signalQueue = DispatchQueue(label: "codes.vapor.server.shutdown")
        func makeSignalSource(_ code: Int32) {
            let source = DispatchSource.makeSignalSource(signal: code, queue: signalQueue)
            source.setEventHandler {
                _ = self.runningServer?.close()
                source.cancel()
            }
            source.resume()
            signal(code, SIG_IGN)
        }
        makeSignalSource(SIGTERM)
        makeSignalSource(SIGINT)
        
        // start the actual HTTPServer
        let server = HTTPServer(config: self.config, on: self.application.eventLoopGroup)
        let delegate = ServerDelegate(application: self.application)
        return server.start(delegate: delegate).flatMap {
            self.runningServer = server
            return server.onClose.map {
                self.console.print("Server shutting down...")
            }
        }
    }
}