// // Server.swift // QueueCube // // Created by James Magahern on 6/10/25. // import Foundation struct Server: Identifiable, Codable, Equatable { let serviceName: String? let baseURL: URL var id: String { baseURL.absoluteString } var api: API { API(baseURL: baseURL) } var displayName: String { if let serviceName { return serviceName.queueCubeServiceName } let components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)! return components.host ?? baseURL.absoluteString } init?(serviceName: String?, host: String, port: UInt16) { self.serviceName = serviceName // Assumes this is the local service discovery path, which is http // Bounjour gives us the interface sometimes, which we can handle, but need to percent encode. let host = host.replacingOccurrences(of: "%", with: "%25") guard let url = URL(string: "http://\(host):\(port)/api") else { return nil } self.baseURL = url } init(baseURL: URL) { self.serviceName = nil self.baseURL = baseURL } } extension String { var queueCubeServiceName: String { let regex = /.* \((.*)\)/ if let match = try? regex.firstMatch(in: self) { return String(match.output.1) } return self } }