krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v2.13.0: Hutch/App/AppConfiguration.swift · raw
1import Foundation
2
3struct AppConfiguration: Sendable {
4 static let defaultHutchStatsBaseURL = URL(string: "https://hutch-stats.zerolabs.sh")!
5 static let hutchStatsBaseURLEnvironmentKey = "HUTCH_STATS_BASE_URL"
6
7 let hutchStatsBaseURL: URL
8
9 init(
10 userDefaults: UserDefaults = .standard,
11 processInfo: ProcessInfo = .processInfo,
12 environment: [String: String]? = nil
13 ) {
14 let resolvedEnvironment = environment ?? processInfo.environment
15
16 if
17 let rawValue = resolvedEnvironment[Self.hutchStatsBaseURLEnvironmentKey],
18 let url = Self.normalizedURL(from: rawValue)
19 {
20 self.hutchStatsBaseURL = url
21 } else if
22 let rawValue = userDefaults.string(forKey: AppStorageKeys.hutchStatsBaseURL),
23 let url = Self.normalizedURL(from: rawValue)
24 {
25 self.hutchStatsBaseURL = url
26 } else {
27 self.hutchStatsBaseURL = Self.defaultHutchStatsBaseURL
28 }
29 }
30
31 private static func normalizedURL(from rawValue: String) -> URL? {
32 guard var components = URLComponents(string: rawValue.trimmingCharacters(in: .whitespacesAndNewlines)) else {
33 return nil
34 }
35
36 if components.path.isEmpty {
37 components.path = "/"
38 }
39
40 return components.url
41 }
42}