Skip to content

Commit 138c738

Browse files
committed
initial
0 parents  commit 138c738

File tree

8 files changed

+267
-0
lines changed

8 files changed

+267
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.build
2+
.swiftpm
3+
.DS_Store

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
install:
2+
swift build -c release && install .build/release/swift-server /usr/local/bin/swift-server

Package.resolved

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

Package.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version:5.7
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "swift-server",
6+
platforms: [
7+
.macOS(.v12),
8+
],
9+
products: [
10+
.executable(name: "swift-server", targets: ["App"])
11+
],
12+
dependencies: [
13+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),
14+
.package(url: "https://github.com/hummingbird-project/hummingbird", from: "1.6.0"),
15+
],
16+
targets: [
17+
.executableTarget(
18+
name: "App",
19+
dependencies: [
20+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
21+
.product(name: "Hummingbird", package: "hummingbird"),
22+
.product(name: "HummingbirdFoundation", package: "hummingbird"),
23+
],
24+
swiftSettings: [
25+
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)),
26+
]
27+
),
28+
.testTarget(
29+
name: "AppTests",
30+
dependencies: [
31+
.byName(name: "App"),
32+
.product(name: "HummingbirdXCT", package: "hummingbird"),
33+
]
34+
),
35+
]
36+
)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Swift server
2+
3+
A static file server written in Swift using Hummingbird.
4+
5+
```shell
6+
git clone [email protected]:BinaryBirds/swift-server.git && cd swift-server && make install
7+
```

Sources/App/App.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ArgumentParser
2+
import Hummingbird
3+
import Foundation
4+
5+
@main
6+
struct App: AsyncParsableCommand, AppArguments {
7+
8+
@Argument(help: "The path of the files")
9+
var path: String = "."
10+
11+
@Option(name: .shortAndLong)
12+
var hostname: String = "127.0.0.1"
13+
14+
@Option(name: .shortAndLong)
15+
var port: Int = 8080
16+
17+
@Option(name: .shortAndLong)
18+
var name: String = "Swift server"
19+
20+
func run() async throws {
21+
setenv("LOG_LEVEL", "warning", 1)
22+
23+
let app = HBApplication(
24+
configuration: .init(
25+
address: .hostname(hostname, port: port),
26+
serverName: name
27+
)
28+
)
29+
try await app.configure(self)
30+
try app.start()
31+
app.wait()
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Hummingbird
2+
import HummingbirdFoundation
3+
import Foundation
4+
5+
public protocol AppArguments {
6+
var path: String { get }
7+
}
8+
9+
extension HBApplication {
10+
11+
func configure(_ args: AppArguments) async throws {
12+
// logger.logLevel = .warning
13+
14+
let home = FileManager.default.homeDirectoryForCurrentUser.path
15+
var publicPath = args.path.replacingOccurrences(of: "~", with: home)
16+
17+
if publicPath.hasPrefix(".") {
18+
publicPath = FileManager.default.currentDirectoryPath + "/" + publicPath
19+
}
20+
21+
middleware.add(
22+
HBFileMiddleware(
23+
publicPath,
24+
searchForIndexHtml: true,
25+
application: self
26+
)
27+
)
28+
}
29+
}

Tests/AppTests/AppTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import XCTest
2+
3+
final class AppTests: XCTestCase {
4+
5+
func testExample() async throws {
6+
// nothing :)
7+
}
8+
}

0 commit comments

Comments
 (0)