forked from TechEmpower/FrameworkBenchmarks
-
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
Shagit Ziganshin
committed
Jul 4, 2024
1 parent
8438981
commit cf1fc17
Showing
12 changed files
with
275 additions
and
2 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,2 @@ | ||
.build/ | ||
.swiftpm/ |
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,12 @@ | ||
Packages | ||
.build | ||
xcuserdata | ||
*.xcodeproj | ||
DerivedData/ | ||
.DS_Store | ||
db.sqlite | ||
.swiftpm | ||
.env | ||
.env.* | ||
! .env.example | ||
.vscode |
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,44 @@ | ||
// swift-tools-version:5.10 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "vapor-swifql-ikiga", | ||
platforms: [ | ||
.macOS(.v13) | ||
], | ||
products: [ | ||
.executable(name: "app", targets: ["App"]) | ||
], | ||
dependencies: [ | ||
// 💧 A server-side Swift web framework. | ||
.package(url: "https://github.com/vapor/vapor.git", from: "4.99.3"), | ||
// 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors | ||
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"), | ||
// json encoder/decoder | ||
.package(url: "https://github.com/orlandos-nl/IkigaJSON.git", from: "2.0.0"), | ||
// sql builder | ||
.package(url: "https://github.com/SwifQL/VaporBridges.git", from: "1.0.0-rc"), | ||
.package(url: "https://github.com/SwifQL/PostgresBridge.git", from: "1.0.0-rc"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "App", | ||
dependencies: [ | ||
.product(name: "Vapor", package: "vapor"), | ||
.product(name: "NIOCore", package: "swift-nio"), | ||
.product(name: "NIOPosix", package: "swift-nio"), | ||
.product(name: "VaporBridges", package: "VaporBridges"), | ||
.product(name: "PostgresBridge", package: "PostgresBridge"), | ||
.product(name: "IkigaJSON", package: "IkigaJSON") | ||
], | ||
path: "Sources", | ||
swiftSettings: swiftSettings | ||
) | ||
] | ||
) | ||
|
||
var swiftSettings: [SwiftSetting] { [ | ||
.enableUpcomingFeature("DisableOutwardActorInference"), | ||
.enableExperimentalFeature("StrictConcurrency"), | ||
] } |
12 changes: 12 additions & 0 deletions
12
frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/Models/Fortune.swift
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,12 @@ | ||
import Bridges | ||
|
||
final class Fortune: Table { | ||
@Column("id") | ||
var id: Int32? | ||
|
||
@Column("message") | ||
var message: String | ||
|
||
init() {} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/Models/World.swift
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,11 @@ | ||
import Bridges | ||
|
||
final class World: Table { | ||
@Column("id") | ||
var id: Int32? | ||
|
||
@Column("randomnumber") | ||
var randomnumber: Int | ||
|
||
init() {} | ||
} |
69 changes: 69 additions & 0 deletions
69
frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/configure.swift
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,69 @@ | ||
import IkigaJSON | ||
import PostgresBridge | ||
import Vapor | ||
import VaporBridges | ||
|
||
|
||
extension DatabaseHost { | ||
public static var DbHost: DatabaseHost { | ||
return .init( | ||
hostname: "tfb-database", | ||
port: 5432, | ||
username: "benchmarkdbuser", | ||
password: "benchmarkdbpassword", | ||
tlsConfiguration: nil | ||
) | ||
} | ||
} | ||
|
||
extension DatabaseIdentifier { | ||
public static var Db: DatabaseIdentifier { | ||
.init(name: "hello_world", host: .DbHost) | ||
} | ||
} | ||
|
||
|
||
public func configure(_ app: Application) throws { | ||
var decoder = IkigaJSONDecoder() | ||
decoder.settings.dateDecodingStrategy = .iso8601 | ||
ContentConfiguration.global.use(decoder: decoder as ContentDecoder, for: .json) | ||
|
||
var encoder = IkigaJSONEncoder() | ||
encoder.settings.dateEncodingStrategy = .iso8601 | ||
ContentConfiguration.global.use(encoder: encoder as ContentEncoder, for: .json) | ||
|
||
app.http.server.configuration.serverName = "Vapor" | ||
app.logger.logLevel = .notice | ||
app.logger.notice("💧 VAPOR") | ||
app.logger.notice("System.coreCount: \(System.coreCount)") | ||
|
||
try routes(app) | ||
|
||
try app.run() | ||
} | ||
|
||
public func routes(_ app: Application) throws { | ||
app.get("plaintext") { req async in | ||
"Hello, world!" | ||
} | ||
|
||
app.get("json") { req async in | ||
["message": "Hello, world!"] | ||
} | ||
|
||
app.get("db") { req throws -> EventLoopFuture<World> in | ||
req.postgres.connection(to: DbHost) { conn throws -> _ in | ||
guard let world = try World | ||
.select | ||
.where(\World.$id == Int.random(in: 1...10_000)) | ||
.execute(on: conn) | ||
.first(decoding: World.self) | ||
else { | ||
throw Abort(.notFound) | ||
} | ||
|
||
return world | ||
} | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/json.swift
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,52 @@ | ||
// Setup custom JSONEncoder/Decoder | ||
|
||
import IkigaJSON | ||
import Vapor | ||
|
||
|
||
extension IkigaJSONEncoder: ContentEncoder { | ||
public func encode<E: Encodable>( | ||
_ encodable: E, | ||
to body: inout ByteBuffer, | ||
headers: inout HTTPHeaders | ||
) throws { | ||
headers.contentType = .json | ||
try self.encodeAndWrite(encodable, into: &body) | ||
} | ||
|
||
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws where E : Encodable { | ||
var encoder = self | ||
encoder.userInfo = userInfo | ||
headers.contentType = .json | ||
try encoder.encodeAndWrite(encodable, into: &body) | ||
} | ||
|
||
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws where E : Encodable { | ||
var encoder = self | ||
encoder.userInfo = userInfo | ||
headers.contentType = .json | ||
try encoder.encodeAndWrite(encodable, into: &body) | ||
} | ||
} | ||
|
||
extension IkigaJSONDecoder: ContentDecoder { | ||
public func decode<D: Decodable>( | ||
_ decodable: D.Type, | ||
from body: ByteBuffer, | ||
headers: HTTPHeaders | ||
) throws -> D { | ||
return try self.decode(D.self, from: body) | ||
} | ||
|
||
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws -> D where D : Decodable { | ||
let decoder = IkigaJSONDecoder(settings: settings) | ||
decoder.settings.userInfo = userInfo | ||
return try decoder.decode(D.self, from: body) | ||
} | ||
|
||
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws -> D where D : Decodable { | ||
let decoder = IkigaJSONDecoder(settings: settings) | ||
decoder.settings.userInfo = userInfo | ||
return try decoder.decode(D.self, from: body) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/main.swift
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,12 @@ | ||
import Vapor | ||
import PostgresBridge | ||
import IkigaJSON | ||
import Logging | ||
|
||
var env = try Environment.detect() | ||
try LoggingSystem.bootstrap(from: &env) | ||
|
||
let app = Application(env) | ||
defer { app.shutdown() } | ||
|
||
try configure(app) |
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,29 @@ | ||
# ================================ | ||
# Build image | ||
# ================================ | ||
FROM swift:5.10 as build | ||
WORKDIR /build | ||
|
||
# Copy entire repo into container | ||
COPY ./vapor-swifql-ikiga . | ||
|
||
# Compile with optimizations | ||
RUN swift build \ | ||
-c release \ | ||
-Xswiftc -enforce-exclusivity=unchecked | ||
|
||
# ================================ | ||
# Run image | ||
# ================================ | ||
FROM swift:5.10-slim | ||
WORKDIR /run | ||
|
||
# Copy build artifacts | ||
COPY --from=build /build/.build/release /run | ||
|
||
# Copy Swift runtime libraries | ||
COPY --from=build /usr/lib/swift/ /usr/lib/swift/ | ||
|
||
EXPOSE 8080 | ||
|
||
ENTRYPOINT ["./app", "serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"] |