-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
415 additions
and
7 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
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,14 @@ | ||
# Sacoge Hummingbird Example | ||
|
||
This example server uses `Sacoge` and `Sacoge Hummingbird` to serve assets. | ||
|
||
The server has a custom sacoge configuration [here](.sacoge) and sets up the `SacogeMiddleware` [here](Sources/Example/Application+build.swift). There is one route, the root route `/`. The root route serves an html document with an `<img>` element referencing `Asset.swift_png`. | ||
|
||
## Running | ||
|
||
* If you run in DEBUG mode (`swift run`), assets are served with `no-cache` | ||
* If you run in RELEASE mode (`swift run -c release`), assets are served with an immutable cache policy | ||
|
||
> ⚠️ **Xcode**: If you want to run the project on Xcode, you need to update the `Example` scheme, edit the scheme, go to `Run > Options > Working Directory` and set the value to the root of this project. | ||
After startin the server, open [`localhost:8080`](http://localhost:8080), it serves the [`swift.png`](my_public_folder/swift.png) image, check the Network logs to and see Cache-Control header. |
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,61 @@ | ||
import Hummingbird | ||
import Logging | ||
import SacogeHummingbird | ||
|
||
/// Application arguments protocol. We use a protocol so we can call | ||
/// `buildApplication` inside Tests as well as in the App executable. | ||
/// Any variables added here also have to be added to `App` in App.swift and | ||
/// `TestArguments` in AppTest.swift | ||
public protocol AppArguments { | ||
var hostname: String { get } | ||
var port: Int { get } | ||
var logLevel: Logger.Level? { get } | ||
} | ||
|
||
// Request context used by application | ||
typealias AppRequestContext = BasicRequestContext | ||
|
||
/// Build application | ||
/// - Parameter arguments: application arguments | ||
public func buildApplication(_ arguments: some AppArguments) async throws | ||
-> some ApplicationProtocol | ||
{ | ||
let environment = Environment() | ||
let logger = { | ||
var logger = Logger(label: "Sacoge Example") | ||
logger.logLevel = | ||
arguments.logLevel ?? environment.get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } | ||
?? .info | ||
return logger | ||
}() | ||
let router = buildRouter() | ||
let app = Application( | ||
router: router, | ||
configuration: .init( | ||
address: .hostname(arguments.hostname, port: arguments.port), | ||
serverName: "Sacoge Example" | ||
), | ||
logger: logger | ||
) | ||
return app | ||
} | ||
|
||
/// Build router | ||
func buildRouter() -> Router<AppRequestContext> { | ||
let router = Router(context: AppRequestContext.self) | ||
// Add middleware | ||
router.addMiddleware { | ||
// logging middleware | ||
LogRequestsMiddleware(.info) | ||
SacogeMiddleware( | ||
"my_public_folder", | ||
externalToInternalMapping: Asset.externalToInternalMapping) | ||
} | ||
|
||
router.get("/") { _, _ -> Response in | ||
return .html(htmlLayout(""" | ||
<img src="\(Asset.swift_png)"> | ||
""")) | ||
} | ||
return router | ||
} |
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 |
---|---|---|
@@ -1,10 +1,27 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
import ArgumentParser | ||
import Hummingbird | ||
import Logging | ||
|
||
@main | ||
struct Example { | ||
static func main() throws { | ||
dump(Asset.externalToInternalMapping) | ||
struct Example: AsyncParsableCommand, AppArguments { | ||
@Option(name: .shortAndLong) | ||
var hostname: String = "127.0.0.1" | ||
|
||
@Option(name: .shortAndLong) | ||
var port: Int = 8080 | ||
|
||
@Option(name: .shortAndLong) | ||
var logLevel: Logger.Level? | ||
|
||
func run() async throws { | ||
let app = try await buildApplication(self) | ||
try await app.runService() | ||
} | ||
} | ||
|
||
/// Extend `Logger.Level` so it can be used as an argument | ||
#if hasFeature(RetroactiveAttribute) | ||
extension Logger.Level: @retroactive ExpressibleByArgument {} | ||
#else | ||
extension Logger.Level: ExpressibleByArgument {} | ||
#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,13 @@ | ||
func htmlLayout(_ content: String) -> String { | ||
""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>sacoge example</title> | ||
</head> | ||
<body> | ||
\(content) | ||
</body> | ||
</html> | ||
""" | ||
} |
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,20 @@ | ||
import Hummingbird | ||
|
||
extension Response { | ||
// Helper to respond with an HTML string | ||
static func html( | ||
status: HTTPResponse.Status = .ok, | ||
extraHeaders: HTTPFields = [:], | ||
_ htmlString: String | ||
) -> Response { | ||
var headers: HTTPFields = [ | ||
.contentType: MediaType.textHtml.description | ||
] | ||
headers.append(contentsOf: extraHeaders) | ||
return .init( | ||
status: .ok, | ||
headers: headers, | ||
body: .init(byteBuffer: .init(string: htmlString)) | ||
) | ||
} | ||
} |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.