Skip to content

Commit d0964db

Browse files
Merge pull request #7 from NeedleInAJayStack/feat/server
Adds server implementation
2 parents 885ea10 + 84c5a0a commit d0964db

33 files changed

+3527
-73
lines changed

Package.resolved

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,23 @@ let package = Package(
3030
"HaystackClientNIO"
3131
]
3232
),
33+
.library(
34+
name: "HaystackServer",
35+
targets: [
36+
"HaystackServer"
37+
]
38+
),
39+
.library(
40+
name: "HaystackServerVapor",
41+
targets: [
42+
"HaystackServerVapor"
43+
]
44+
),
3345
],
3446
dependencies: [
3547
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
36-
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
48+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
49+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
3750
],
3851
targets: [
3952
.target(
@@ -62,7 +75,21 @@ let package = Package(
6275
.product(name: "AsyncHTTPClient", package: "async-http-client"),
6376
]
6477
),
65-
78+
.target(
79+
name: "HaystackServer",
80+
dependencies: [
81+
"Haystack",
82+
]
83+
),
84+
.target(
85+
name: "HaystackServerVapor",
86+
dependencies: [
87+
"Haystack",
88+
"HaystackServer",
89+
.product(name: "Vapor", package: "vapor")
90+
]
91+
),
92+
6693
// Tests
6794
.testTarget(
6895
name: "HaystackTests",
@@ -80,6 +107,14 @@ let package = Package(
80107
name: "HaystackClientDarwinIntegrationTests",
81108
dependencies: ["HaystackClientDarwin"]
82109
),
110+
.testTarget(
111+
name: "HaystackServerTests",
112+
dependencies: ["HaystackServer"]
113+
),
114+
.testTarget(
115+
name: "HaystackServerVaporTests",
116+
dependencies: ["HaystackServerVapor", .product(name: "XCTVapor", package: "vapor")]
117+
),
83118
]
84119
)
85120
#else
@@ -97,10 +132,23 @@ let package = Package(
97132
"HaystackClientNIO"
98133
]
99134
),
135+
.library(
136+
name: "HaystackServer",
137+
targets: [
138+
"HaystackServer"
139+
]
140+
),
141+
.library(
142+
name: "HaystackServerVapor",
143+
targets: [
144+
"HaystackServerVapor"
145+
]
146+
),
100147
],
101148
dependencies: [
102149
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
103-
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
150+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
151+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
104152
],
105153
targets: [
106154
.target(
@@ -122,7 +170,21 @@ let package = Package(
122170
.product(name: "AsyncHTTPClient", package: "async-http-client"),
123171
]
124172
),
125-
173+
.target(
174+
name: "HaystackServer",
175+
dependencies: [
176+
"Haystack"
177+
]
178+
),
179+
.target(
180+
name: "HaystackServerVapor",
181+
dependencies: [
182+
"Haystack",
183+
"HaystackServer",
184+
.product(name: "Vapor", package: "vapor")
185+
]
186+
),
187+
126188
// Tests
127189
.testTarget(
128190
name: "HaystackTests",
@@ -136,6 +198,14 @@ let package = Package(
136198
name: "HaystackClientNIOIntegrationTests",
137199
dependencies: ["HaystackClientNIO"]
138200
),
201+
.testTarget(
202+
name: "HaystackServerTests",
203+
dependencies: ["HaystackServer"]
204+
),
205+
.testTarget(
206+
name: "HaystackServerVaporTests",
207+
dependencies: ["HaystackServerVapor", .product(name: "XCTVapor", package: "vapor")]
208+
),
139209
]
140210
)
141-
#endif
211+
#endif

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ See below for available libraries and descriptions.
4343

4444
### Haystack
4545

46-
This contains the
46+
This contains the
4747
[Haystack type-system primitives](https://project-haystack.org/doc/docHaystack/Kinds)
4848
and utilities to interact with them.
4949

@@ -101,20 +101,55 @@ Once you create a client, you can use it to make requests:
101101
```swift
102102
func yesterdaysValues() async throws -> Grid {
103103
let client = ...
104-
104+
105105
// Open and authenticate. This must be called before requests can be made
106106
try await client.open()
107-
107+
108108
// Request the historical values for @28e7fb7d-e20316e0
109109
let grid = try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday)
110-
110+
111111
// Close the client session and log out
112112
try await client.close()
113-
113+
114114
return grid
115115
}
116116
```
117117

118+
### HaystackServerVapor
119+
120+
A server for the [Haystack HTTP API](https://project-haystack.org/doc/docHaystack/HttpApi) that uses
121+
[Vapor](https://github.com/vapor/vapor). It's separated from the `HaystackServer` package so that clients may use
122+
alternative server technologies if they choose, including Hummingbird or NIO directly.
123+
124+
It exposes a `HaystackRouteCollection` that can be registered on a Vapor
125+
application:
126+
127+
```swift
128+
let app = Application()
129+
try app.register(collection: HaystackRouteCollection(delegate: ...))
130+
```
131+
132+
The delegate is a protocol that can be implemented however the user sees fit, although the standard Haystack
133+
implementation is defined in `HaystackServer`.
134+
135+
### HaystackServer
136+
137+
This defines the standard functionality and data processing of Haystack API servers, based on generic backing data
138+
stores. In most cases, Haystack servers should use the `HaystackServer` class and customize storage behavior by
139+
implementing the `RecordStore`, `HistoryStore`, and `WatchStore` protocols.
140+
141+
```swift
142+
struct InfluxHistoryStore: HistoryStore {
143+
// Define storage behavior here
144+
...
145+
}
146+
147+
let server = HaystackServer(
148+
historyStore: InfluxHistoryStore(),
149+
...
150+
)
151+
```
152+
118153
## License
119154

120155
This package is licensed under the Academic Free License 3.0 for maximum compatibility with

0 commit comments

Comments
 (0)