on master help sign in
3 files changed+21−3
since splitsnapshots
unifiedsplit
Sources/App/Models/SafeDict.swift+11−3 Sources/App/routes.swift+4 Sources/Run/main.swift+6
Sources/App/Models/SafeDict.swift+11−3
@@ -7,6 +7,14 @@
77
88import Foundation
99
10+func getRandomNum(_ min: Int, _ max: Int) -> Int {
11+ #if os(Linux)
12+ return Int(random() % max) + min
13+ #else
14+ return Int(arc4random_uniform(UInt32(max)) + UInt32(min))
15+ #endif
16+}
17+
1018struct SafeDict<Key: Hashable, Value> {
1119 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
1220
@@ -33,9 +41,9 @@ struct SafeDict<Key: Hashable, Value> {
3341 mutating func removeRandom() -> Key? {
3442 var key: Key?
3543 queue.sync(flags: .barrier) {
36− key = dict.keys.randomElement()
37− if let key = key {
38− dict[key] = nil
44+ if dict.count > 0 {
45+ key = Array(dict.keys)[getRandomNum(0, dict.count)]
46+ dict[key!] = nil
3947 }
4048 }
4149 return key
Sources/App/routes.swift+4
@@ -33,6 +33,10 @@ public func routes(_ router: Router) throws {
3333 }
3434 return post
3535 }
36+
37+ router.get("posts") { (req) -> [Post] in
38+ return Array(Post.storage.values)
39+ }
3640}
3741
3842enum DatabaseError: Error, AbortError {
Sources/Run/main.swift+6
@@ -1,3 +1,9 @@
11import App
22
3+#if os(Linux)
4+import SwiftGlibc
5+srandom(UInt32(time(nil)))
6+#endif
7+
8+
39try app(.detect()).run()