Skip to content

Commit 181568e

Browse files
committed
Merge branch 'develop'
2 parents 03d11f0 + c5e1b30 commit 181568e

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let package = Package(
1919
.package(url: "https://github.com/Macro-swift/Macro.git",
2020
from: "1.0.2"),
2121
.package(url: "https://github.com/AlwaysRightInstitute/mustache.git",
22-
from: "1.0.1")
22+
from: "1.0.2")
2323
],
2424

2525
targets: [
@@ -44,7 +44,8 @@ let package = Package(
4444
.product(name: "MacroCore", package: "Macro"),
4545
.product(name: "fs", package: "Macro"),
4646
.product(name: "http", package: "Macro"),
47-
"connect", "mime", "mustache"
47+
"connect", "mime",
48+
.product(name: "Mustache", package: "Mustache")
4849
], exclude: [ "README.md" ]),
4950
.target(name: "MacroExpress", dependencies: [
5051
.product(name: "MacroCore", package: "Macro"),

Sources/connect/Session.swift

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import protocol MacroCore.EnvironmentKey
1111
import class http.IncomingMessage
1212
import class http.ServerResponse
1313
import struct Foundation.UUID
14+
import struct NIOConcurrencyHelpers.NIOLock
1415

1516
fileprivate let sessionIdCookie = Cookie(name: "NzSID", maxAge: 3600)
1617

@@ -42,17 +43,19 @@ public func session(store s : SessionStore = InMemorySessionStore(),
4243
return next()
4344
}
4445

46+
// TODO: This should do a session-checkout.
4547
// retrieve from store
4648
s.get(sessionID: sessionID) { err, session in
47-
guard err == nil else {
48-
console.log("could not retrieve session with ID \(sessionID): \(err!)")
49+
if let rerr = err {
50+
req.log
51+
.notice("could not retrieve session with ID \(sessionID): \(rerr)")
4952
ctx.configureNewSession()
5053
return next()
5154
}
5255

5356
guard let rsession = session else {
54-
console.log("No error, but could not retrieve session with ID" +
55-
" \(sessionID)")
57+
req.log.notice(
58+
"No error, but could not retrieve session with ID \(sessionID)")
5659
ctx.configureNewSession()
5760
return next()
5861
}
@@ -61,9 +64,10 @@ public func session(store s : SessionStore = InMemorySessionStore(),
6164
req.environment[SessionKey.self] = rsession
6265
ctx.pushSessionCookie()
6366
_ = res.onceFinish {
67+
// TODO: This should do the session-checkin.
6468
s.set(sessionID: sessionID, session: req.session) { err in
6569
if let err = err {
66-
console.error("could not save session \(sessionID): \(err)")
70+
req.log.error("could not save session \(sessionID): \(err)")
6771
}
6872
}
6973
}
@@ -122,7 +126,7 @@ class SessionContext {
122126
_ = res.onceFinish {
123127
store.set(sessionID: newSessionID, session: req.session) { err in
124128
if let err = err {
125-
console.error("could not save new session \(newSessionID): \(err)")
129+
req.log.error("could not save new session \(newSessionID): \(err)")
126130
}
127131
}
128132
}
@@ -140,20 +144,24 @@ public class Session {
140144
//
141145
// req.session["a"] = 10
142146
//
143-
// Think about it, kinda non-obvious ;-)
147+
// kinda non-obvious.
144148

149+
// This should not be concurrent but use a checkin/checkout system.
150+
fileprivate let lock = NIOLock()
145151
public var values = Dictionary<String, Any>()
146152

147153
public subscript(key: String) -> Any? {
148154
set {
149-
if let v = newValue { values[key] = v }
150-
else { values.removeValue(forKey: key) }
155+
lock.withLock {
156+
if let v = newValue { values[key] = v }
157+
else { _ = values.removeValue(forKey: key) }
158+
}
151159
}
152-
get { return values[key] }
160+
get { return lock.withLock { values[key] } }
153161
}
154162

155163
public subscript(int key: String) -> Int {
156-
guard let v = values[key] else { return 0 }
164+
guard let v = lock.withLock({ values[key] }) else { return 0 }
157165
if let iv = v as? Int { return iv }
158166
#if swift(>=5.10)
159167
if let i = (v as? any BinaryInteger) { return Int(i) }
@@ -228,14 +236,16 @@ public extension SessionStore {
228236

229237
public class InMemorySessionStore : SessionStore {
230238

239+
// Well, there should be a checkout/checkin system?!
240+
fileprivate let lock = NIOLock()
231241
var store : [ String : Session ]
232242

233243
public init() {
234244
store = [:]
235245
}
236246

237247
public func get(sessionID sid: String, _ cb: ( Error?, Session? ) -> Void ) {
238-
guard let session = store[sid] else {
248+
guard let session = lock.withLock({ store[sid] }) else {
239249
cb(SessionStoreError.SessionNotFound, nil)
240250
return
241251
}
@@ -245,7 +255,7 @@ public class InMemorySessionStore : SessionStore {
245255
public func set(sessionID sid: String, session: Session,
246256
_ cb: ( Error? ) -> Void )
247257
{
248-
store[sid] = session
258+
lock.withLock { store[sid] = session }
249259
cb(nil)
250260
}
251261

@@ -256,21 +266,21 @@ public class InMemorySessionStore : SessionStore {
256266
}
257267

258268
public func destroy(sessionID sid: String, _ cb: ( String ) -> Void) {
259-
store.removeValue(forKey: sid)
269+
lock.withLock { _ = store.removeValue(forKey: sid) }
260270
cb(sid)
261271
}
262272

263273
public func clear(cb: ( Error? ) -> Void ) {
264-
store.removeAll()
274+
lock.withLock { store.removeAll() }
265275
cb(nil)
266276
}
267277

268278
public func length(cb: ( Error?, Int) -> Void) {
269-
cb(nil, store.count)
279+
cb(nil, lock.withLock { store.count })
270280
}
271281

272282
public func all(cb: ( Error?, [ Session ] ) -> Void) {
273-
let values = Array(store.values)
283+
let values = Array(lock.withLock { store.values })
274284
cb(nil, values)
275285
}
276286
}

Sources/express/Mustache.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// Noze.io / Macro
44
//
55
// Created by Helge Heß on 02/06/16.
6-
// Copyright © 2016-2020 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2016-2024 ZeeZide GmbH. All rights reserved.
77
//
88

99
import func fs.readFile
1010
import func fs.readFileSync
1111
import enum fs.path
1212
import let MacroCore.console
13-
import mustache
13+
import Mustache
1414

1515
let mustacheExpress : ExpressEngine = { path, options, done in
1616
fs.readFile(path, "utf8") { err, str in
@@ -25,7 +25,7 @@ let mustacheExpress : ExpressEngine = { path, options, done in
2525
return
2626
}
2727

28-
let parser = MustacheParser()
28+
var parser = MustacheParser()
2929
let tree = parser.parse(string: template)
3030

3131
let ctx = ExpressMustacheContext(path: path, object: options)
@@ -53,7 +53,7 @@ class ExpressMustacheContext : MustacheDefaultRenderingContext {
5353
return nil
5454
}
5555

56-
let parser = MustacheParser()
56+
var parser = MustacheParser()
5757
let tree = parser.parse(string: template)
5858
return tree
5959
}

0 commit comments

Comments
 (0)