@Handler(method: .GET)
struct GetGreeting {
@Query("name")
var name: String
func handle() async throws -> String {
"Hello, \(name)!"
}
}let app = WebServiceApp { environment in
let apiKey = try environment.get("API_KEY")
Homepage(apiKey: apiKey) {
HTTP(host: "127.0.0.1", port: 8080) {
Logging.resolve
} plugins: {
OpenAPI() {
Logging.resolve
}
}
Logging {
StandardOutputLogging(level: .info)
StandardErrorLogging(level: .error)
}
}
}
try await app.run()@Handler(method: .POST, path: "greet")
struct GreetUser {
@Query("locale")
var locale: BCP47Locale = BCP47Locale(languageCode: .english)
@Body
var user: User
@Responding(status: .ok)
var response
func handle() async throws -> String {
switch locale.languageCode {
case .german:
return "Hallo, \(user.name.capitalized)!"
case .french:
return "Bonjour, \(user.name.capitalized)!"
default:
return "Hello, \(user.name.capitalized)!"
}
}
}@Handler(method: .GET, path: "echo")
struct GetEcho {
func handle() async throws -> WebSocketResponse {
WebSocketResponse { inbound, outbound in
for await message in inbound {
outbound.yield(message)
}
}
}
}@Handler(method: .GET, path: "prices")
struct GetPrices {
func handle() async throws -> SSEResponse {
SSEResponse { continuation in
for await price in priceFeed {
continuation.yield(data: price, event: "price")
}
continuation.finish()
}
}
}Article {
HeaderSection {
Heading1 { Text(post.title) }
Time(post.publishedAt)
}
Paragraph { Text(post.lead) }
Figure {
Image().src(post.imageURL)
FigureCaption { Text(post.caption) }
}
}VStack {
Text(string: title)
.fontSize(.largeTitle)
.fontWeight(.bold)
for product in products {
HStack {
Text(string: product.name)
Spacer()
Text(string: product.price)
.fontWeight(.semiBold)
}
.padding(.vertical, ._2)
}
}
.gap(._4)
.padding(._5)Button {
Text(string: "Load more")
}
.hxGet(baseURI + GetArticles.uri(page: nextPage))
.hxTarget("#feed")
.hxSwap(.append)
.hxTrigger("click")