Skip to content

Commit 06c9fe5

Browse files
authored
fix: Using package as framework (#15)
* fix: Using package as framework * open up framework * remove leaf * doc updates
1 parent 5a4b6e6 commit 06c9fe5

File tree

13 files changed

+240
-204
lines changed

13 files changed

+240
-204
lines changed

Package.resolved

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

Package.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ let package = Package(
1515
],
1616
dependencies: [
1717
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.69.1")),
18-
.package(url: "https://github.com/vapor/leaf.git", .upToNextMajor(from: "4.2.4")),
1918
.package(url: "https://github.com/netreconlab/Parse-Swift.git",
2019
.upToNextMajor(from: "5.0.0-beta.6")),
2120
],
@@ -24,17 +23,16 @@ let package = Package(
2423
name: "ParseServerSwift",
2524
dependencies: [
2625
.product(name: "Vapor", package: "vapor"),
27-
.product(name: "Leaf", package: "leaf"),
2826
.product(name: "ParseSwift", package: "Parse-Swift")
29-
],
30-
swiftSettings: [
31-
// Enable better optimizations when building in Release configuration. Despite the use of
32-
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
33-
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
34-
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
35-
]
36-
),
37-
.executableTarget(name: "Run", dependencies: [.target(name: "ParseServerSwift")]),
27+
]),
28+
.executableTarget(name: "Run",
29+
dependencies: [.target(name: "ParseServerSwift")],
30+
swiftSettings: [
31+
// Enable better optimizations when building in Release configuration. Despite the use of
32+
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
33+
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
34+
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
35+
]),
3836
.testTarget(name: "ParseServerSwiftTests", dependencies: [
3937
.target(name: "ParseServerSwift"),
4038
.product(name: "XCTVapor", package: "vapor"),

README.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,28 @@ and [Vapor](https://github.com/vapor/vapor). `ParseServerSwift` provides many ad
2222

2323
Technically, complete apps can be written with `ParseServerSwift`, the only difference is that this code runs in your `ParseServerSwift` rather than running on the user’s mobile device. When you update your Cloud Code, it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application. This lets you change app behavior on the fly and add new features faster.
2424

25+
## Installing ParseServerSwift
26+
Setup your Vapor project by following the [directions](https://www.kodeco.com/11555468-getting-started-with-server-side-swift-with-vapor-4) for installing and setting up your project on macOS or linux.
27+
28+
Then add `ParseServerSwift` to `dependencies` in your `Package.swift` file:
29+
30+
```swift
31+
// swift-tools-version:5.5.2
32+
import PackageDescription
33+
34+
let package = Package(
35+
name: "YOUR_PROJECT_NAME",
36+
dependencies: [
37+
.package(url: "https://github.com/netreconlab/ParseServerSwift", .upToNextMajor(from: "0.1.2")),
38+
]
39+
)
40+
```
41+
42+
Adding `ParseServerSwift` will allow you to quickly add Vapor routes to your server.
43+
2544
## Configure ParseServerSwift to Connect to Your Parse Servers
2645
### Environment Variables
27-
The following enviroment variables are available and can be configured directly or through `.env`, `.env.production`, etc. See the [Vapor Docs for more details](https://docs.vapor.codes/basics/environment/).
46+
It is recommended to copy/modify [ParseServerSwift/Sources/ParseServerSwift/configure.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/configure.swift) to your project. The following enviroment variables are available and can be configured directly or through `.env`, `.env.production`, etc. See the [Vapor Docs for more details](https://docs.vapor.codes/basics/environment/).
2847

2948
```
3049
PARSE_SERVER_SWIFT_HOST_NAME: cloud-code # The name of your host. If you are running in Docker it should be same name as the docker service
@@ -36,8 +55,6 @@ PARSE_SERVER_SWIFT_PRIMARY_KEY: primaryKey # (Required) The master key of your P
3655
PARSE_SERVER_SWIFT_WEBHOOK_KEY: webookKey # The webhookKey of your Parse Server
3756
```
3857

39-
If you need to customize your configuration you will need to edit [ParseServerSwift/Sources/ParseServerSwift/configure.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/configure.swift) directly.
40-
4158
### WebhookKey
4259
The `webhookKey` should match the [webhookKey on the Parse Server](https://github.com/parse-community/parse-server/blob/42c954318926823446326c95188b844e19954711/src/Options/Definitions.js#L491-L494).
4360

@@ -63,7 +80,7 @@ To start your server type, `swift run` in the terminal of the project root direc
6380

6481
## Writing Cloud Code
6582
### Creating `ParseObject`'s
66-
It is recommended to add all of your `ParseObject`'s to [ParseServerSwift/Sources/ParseServerSwift/Models](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/Models). An example `GameScore` model is provided:
83+
It is recommended to add all of your `ParseObject`'s in a folder called `Models` similar to [ParseServerSwift/Sources/ParseServerSwift/Models](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/Models). For example, the `GameScore` model is below:
6784

6885
```swift
6986
import Foundation
@@ -97,10 +114,10 @@ struct GameScore: ParseObject {
97114
```
98115

99116
#### The `ParseUser` Model
100-
Be sure to add all of the additional properties you have in your `_User` class to the `User` model which is located at [ParseServerSwift/Sources/ParseServerSwift/Models/User.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/Models/User.swift)
117+
Be sure to add all of the additional properties you have in your `_User` class to the `User` model. An example `User` model can be found at [ParseServerSwift/Sources/ParseServerSwift/Models/User.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/Models/User.swift)
101118

102119
### Creating New Cloud Code Routes
103-
Adding routes for `ParseHooks` are as simple as adding [routes in Vapor](https://docs.vapor.codes/basics/routing/). `ParseServerSwift` provides some additional methods to routes to easily create and register [Hook Functions](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehookfunctionable) and [Hook Triggers](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehooktriggerable/). All routes should be added to [ParseServerSwift/Sources/ParseServerSwift/routes.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/routes.swift)
120+
Adding routes for `ParseHooks` are as simple as adding [routes in Vapor](https://docs.vapor.codes/basics/routing/). `ParseServerSwift` provides some additional methods to routes to easily create and register [Hook Functions](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehookfunctionable) and [Hook Triggers](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehooktriggerable/). All routes should be added to the `routes.swift` file in your project. Example `ParseServerSwift` routes can be found in [ParseServerSwift/Sources/ParseServerSwift/routes.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/routes.swift).
104121

105122
### Cloud Code Functions
106123
Cloud Code Functions can also take parameters. It's recommended to place all paramters in
@@ -109,7 +126,9 @@ Cloud Code Functions can also take parameters. It's recommended to place all par
109126
```swift
110127
// A Parse Hook Function route.
111128
app.post("hello",
112-
name: "hello") { req async throws -> ParseHookResponse<String> in
129+
name: "hello",
130+
parseServerURLStrings: parseServerURLStrings,
131+
hooks: hooks) { req async throws -> ParseHookResponse<String> in
113132
if let error: ParseHookResponse<String> = checkHeaders(req) {
114133
return error
115134
}
@@ -118,12 +137,14 @@ app.post("hello",
118137

119138
// If a User called the request, fetch the complete user.
120139
if parseRequest.user != nil {
121-
parseRequest = try await parseRequest.hydrateUser(request: req)
140+
parseRequest = try await parseRequest.hydrateUser(request: req,
141+
parseServerURLStrings: parseServerURLStrings)
122142
}
123143

124144
// To query using the User's credentials who called this function,
125145
// use the options() method from the parseRequest
126-
let options = try parseRequest.options(req)
146+
let options = try parseRequest.options(req,
147+
parseServerURLStrings: parseServerURLStrings)
127148
let scores = try await GameScore.query.findAll(options: options)
128149
req.logger.info("Scores this user can access: \(scores)")
129150
return ParseHookResponse(success: "Hello world!")
@@ -135,7 +156,9 @@ app.post("hello",
135156
// A Parse Hook Trigger route.
136157
app.post("score", "save", "before",
137158
className: GameScore.className,
138-
triggerName: .beforeSave) { req async throws -> ParseHookResponse<GameScore> in
159+
triggerName: .beforeSave,
160+
parseServerURLStrings: parseServerURLStrings,
161+
hooks: hooks) { req async throws -> ParseHookResponse<GameScore> in
139162
if let error: ParseHookResponse<GameScore> = checkHeaders(req) {
140163
return error
141164
}
@@ -144,7 +167,8 @@ app.post("score", "save", "before",
144167

145168
// If a User called the request, fetch the complete user.
146169
if parseRequest.user != nil {
147-
parseRequest = try await parseRequest.hydrateUser(request: req)
170+
parseRequest = try await parseRequest.hydrateUser(request: req,
171+
parseServerURLStrings: parseServerURLStrings)
148172
}
149173

150174
guard let object = parseRequest.object else {
@@ -161,7 +185,9 @@ app.post("score", "save", "before",
161185
// Another Parse Hook Trigger route.
162186
app.post("score", "find", "before",
163187
className: GameScore.className,
164-
triggerName: .beforeFind) { req async throws -> ParseHookResponse<[GameScore]> in
188+
triggerName: .beforeFind,
189+
parseServerURLStrings: parseServerURLStrings,
190+
hooks: hooks) { req async throws -> ParseHookResponse<[GameScore]> in
165191
if let error: ParseHookResponse<[GameScore]> = checkHeaders(req) {
166192
return error
167193
}
@@ -186,7 +212,9 @@ app.post("score", "find", "before",
186212
// Another Parse Hook Trigger route.
187213
app.post("user", "login", "after",
188214
className: User.className,
189-
triggerName: .afterLogin) { req async throws -> ParseHookResponse<Bool> in
215+
triggerName: .afterLogin,
216+
parseServerURLStrings: parseServerURLStrings,
217+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
190218
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
191219
return error
192220
}
@@ -199,7 +227,9 @@ app.post("user", "login", "after",
199227

200228
// A Parse Hook Trigger route for `ParseFile`.
201229
app.on("file", "save", "before",
202-
triggerName: .beforeSave) { req async throws -> ParseHookResponse<Bool> in
230+
triggerName: .beforeSave,
231+
parseServerURLStrings: parseServerURLStrings,
232+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
203233
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
204234
return error
205235
}
@@ -212,7 +242,9 @@ app.on("file", "save", "before",
212242

213243
// Another Parse Hook Trigger route for `ParseFile`.
214244
app.post("file", "delete", "before",
215-
triggerName: .beforeDelete) { req async throws -> ParseHookResponse<Bool> in
245+
triggerName: .beforeDelete,
246+
parseServerURLStrings: parseServerURLStrings,
247+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
216248
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
217249
return error
218250
}
@@ -225,7 +257,9 @@ app.post("file", "delete", "before",
225257

226258
// A Parse Hook Trigger route for `ParseLiveQuery`.
227259
app.post("connect", "before",
228-
triggerName: .beforeConnect) { req async throws -> ParseHookResponse<Bool> in
260+
triggerName: .beforeConnect,
261+
parseServerURLStrings: parseServerURLStrings,
262+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
229263
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
230264
return error
231265
}
@@ -239,7 +273,9 @@ app.post("connect", "before",
239273
// Another Parse Hook Trigger route for `ParseLiveQuery`.
240274
app.post("score", "subscribe", "before",
241275
className: GameScore.className,
242-
triggerName: .beforeSubscribe) { req async throws -> ParseHookResponse<Bool> in
276+
triggerName: .beforeSubscribe,
277+
parseServerURLStrings: parseServerURLStrings,
278+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
243279
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
244280
return error
245281
}
@@ -253,7 +289,9 @@ app.post("score", "subscribe", "before",
253289
// Another Parse Hook Trigger route for `ParseLiveQuery`.
254290
app.post("score", "event", "after",
255291
className: GameScore.className,
256-
triggerName: .afterEvent) { req async throws -> ParseHookResponse<Bool> in
292+
triggerName: .afterEvent,
293+
parseServerURLStrings: parseServerURLStrings,
294+
hooks: hooks) { req async throws -> ParseHookResponse<Bool> in
257295
if let error: ParseHookResponse<Bool> = checkHeaders(req) {
258296
return error
259297
}
@@ -263,4 +301,5 @@ app.post("score", "event", "after",
263301
req.logger.info("A LiveQuery event occured: \(parseRequest)")
264302
return ParseHookResponse(success: true)
265303
}
304+
}
266305
```

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public extension ParseHookRequestable {
2323
In a single Parse Server environment, use options().
2424
*/
2525
func options(_ request: Request,
26-
parseServerURLStrings: [String] = parseServerURLStrings) throws -> API.Options {
26+
parseServerURLStrings: [String]) throws -> API.Options {
2727
var options = self.options()
2828
options.insert(.serverURL(try serverURLString(request.url,
2929
parseServerURLStrings: parseServerURLStrings)))
@@ -42,7 +42,7 @@ public extension ParseHookRequestable {
4242
*/
4343
func hydrateUser(options: API.Options = [],
4444
request: Request,
45-
parseServerURLStrings: [String] = parseServerURLStrings) async throws -> Self {
45+
parseServerURLStrings: [String]) async throws -> Self {
4646
var updatedOptions = try self.options(request, parseServerURLStrings: parseServerURLStrings)
4747
updatedOptions = updatedOptions.union(options)
4848
return try await withCheckedThrowingContinuation { continuation in

0 commit comments

Comments
 (0)