Skip to content

Commit 84c5a0a

Browse files
doc: Adds server docs to readme
1 parent 1aa6ed6 commit 84c5a0a

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

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

Sources/HaystackServer/HaystackServer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import Foundation
22
import Haystack
33

4+
/// A HaystackServer is a server that implements the Haystack API.
5+
/// It translates API calls into operations on the underlying data stores.
46
public class HaystackServer: API {
57
let recordStore: RecordStore
68
let historyStore: HistoryStore
79
let watchStore: WatchStore
8-
10+
911
let onInvokeAction: (Haystack.Ref, String, [String : any Haystack.Val]) async throws -> Haystack.Grid
1012
let onEval: (String) async throws -> Haystack.Grid
1113

@@ -163,7 +165,7 @@ public class HaystackServer: API {
163165
try await watchStore.removeIds(watchId: watchId, ids: ids)
164166
return GridBuilder().toGrid()
165167
}
166-
168+
167169
public func watchUnsubDelete(watchId: String) async throws -> Haystack.Grid {
168170
try await watchStore.delete(watchId: watchId)
169171
return GridBuilder().toGrid()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vapor
22

33
public extension HTTPMediaType {
4+
/// The `application/zinc` media type: https://project-haystack.org/doc/docHaystack/Zinc
45
static let zinc = HTTPMediaType(type: "text", subType: "zinc", parameters: ["charset": "utf-8"])
56
}

Sources/HaystackServerVapor/HaystackRouteCollection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Haystack
22
import Vapor
33

4+
/// A route collection that exposes Haystack API endpoints.
45
public struct HaystackRouteCollection: RouteCollection {
56

67
/// This instance defines all Haystack API processing that is done server-side.

0 commit comments

Comments
 (0)