-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added vapor example * CI names
- Loading branch information
Showing
11 changed files
with
211 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.build | ||
xcuserdata/ | ||
DerivedData/ | ||
.vscode | ||
Package.resolved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// swift-tools-version: 5.10 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "HummingbirdDemo", | ||
platforms: [ | ||
.macOS(.v14), | ||
], | ||
products: [ | ||
.executable(name: "App", targets: ["App"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/vapor/vapor", from: "4.102.0"), | ||
.package(path: "../../"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "App", | ||
dependencies: [ | ||
.product(name: "Vapor", package: "vapor"), | ||
.product(name: "ElementaryHTMX", package: "elementary-htmx"), | ||
], | ||
resources: [ | ||
.copy("Public"), | ||
] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# ElementaryHTMX + Vapor Demo | ||
|
||
## Running the example | ||
|
||
Run the app and open http://localhost:8080 in the browser | ||
|
||
```sh | ||
swift run App | ||
``` | ||
|
||
## Dev mode with auto-reload on save | ||
|
||
The `swift-dev` script auto-reloads open browser tabs on source file changes. | ||
|
||
It is using [watchexec](https://github.com/watchexec/watchexec) and [browsersync](https://browsersync.io/). | ||
|
||
### Install required tools | ||
|
||
Use homebrew and npm to install the following (tested on macOS): | ||
|
||
```sh | ||
npm install -g browser-sync | ||
brew install watchexec | ||
``` | ||
|
||
### Run app in watch-mode | ||
|
||
This will watch all swift files in the demo package, build on-demand, and re-sync the browser page | ||
|
||
```sh | ||
swift dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Vapor | ||
|
||
@main | ||
struct App { | ||
static func main() async throws { | ||
let app = try await Application.make() | ||
try app.middleware.use(FileMiddleware(bundle: .module)) | ||
|
||
addRoutes(to: app) | ||
|
||
#if DEBUG | ||
app.lifecycle.use(BrowserSyncHandler()) | ||
#endif | ||
|
||
try await app.execute() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
#if DEBUG | ||
struct BrowserSyncHandler: LifecycleHandler { | ||
func didBoot(_: Application) throws { | ||
let p = Process() | ||
p.executableURL = URL(string: "file:///bin/sh") | ||
p.arguments = ["-c", "browser-sync reload"] | ||
do { | ||
try p.run() | ||
} catch { | ||
print("Could not auto-reload: \(error)") | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Elementary | ||
import Vapor | ||
|
||
struct HTMLResponse<Content: HTML> { | ||
@HTMLBuilder var content: Content | ||
} | ||
|
||
extension HTMLResponse: Vapor.AsyncResponseEncodable { | ||
func encodeResponse(for request: Request) async throws -> Response { | ||
Response( | ||
status: .ok, | ||
headers: ["Content-Type": "text/html"], | ||
body: .init(string: content.render()) | ||
) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Elementary | ||
import Vapor | ||
|
||
func addRoutes(to app: Application) { | ||
app.get("") { _ in | ||
HTMLResponse { | ||
MainPage() | ||
} | ||
} | ||
|
||
app.get("result") { request in | ||
let x = try request.query.get(Int.self, at: "x") | ||
let y = try request.query.get(Int.self, at: "y") | ||
return HTMLResponse { | ||
ResultView(x: x, y: y) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Elementary | ||
import ElementaryHTMX | ||
|
||
struct MainPage: HTMLDocument { | ||
var title: String { "Vapor + Elementary + HTMX" } | ||
|
||
var head: some HTML { | ||
meta(.charset(.utf8)) | ||
script(.src("/htmx.min.js")) {} | ||
} | ||
|
||
var body: some HTML { | ||
header { | ||
h2 { "Vapor + Elementary + HTMX" } | ||
} | ||
main { | ||
div { | ||
// example of using hx-target and hx-swap | ||
form(.hx.get("/result"), .hx.target("#result"), .hx.swap(.innerHTML)) { | ||
div(.class("grid")) { | ||
input(.type(.number), .name("x"), .value("1"), .required) | ||
span { " + " } | ||
input(.type(.number), .name("y"), .value("2"), .required) | ||
input(.type(.submit), .value("Calculate")) | ||
} | ||
} | ||
} | ||
div(.id("result")) { | ||
p { i { "Result will be calculated on the server" } } | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct ResultView: HTML { | ||
let x: Int | ||
let y: Int | ||
|
||
var content: some HTML { | ||
p { | ||
"\(x) + \(y) = " | ||
b { "\(x + y)" } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
touch .build/browser-dev-sync | ||
browser-sync start -p localhost:8080 & | ||
|
||
watchexec -w Sources -e .swift -r 'swift build --product App && touch .build/browser-dev-sync' & | ||
watchexec -w .build/browser-dev-sync --ignore-nothing -r '.build/debug/App' |