Add basic Vapor server for the dev command

This commit is contained in:
Max Desiatov 2020-05-05 23:02:06 +01:00
parent 00507280de
commit 3c5904790e
No known key found for this signature in database
GPG Key ID: FE08EBF9CF58CBA2
15 changed files with 4405 additions and 3 deletions

6
.gitignore vendored
View File

@ -88,3 +88,9 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode
iOSInjectionProject/
# Vapor
Public
# Node.js
node_modules

View File

@ -10,6 +10,42 @@
"version": "1.1.1"
}
},
{
"package": "async-kit",
"repositoryURL": "https://github.com/vapor/async-kit.git",
"state": {
"branch": null,
"revision": "48a719dec79ea3cbbc71ca9b29f69df7cd5794cd",
"version": "1.0.1"
}
},
{
"package": "console-kit",
"repositoryURL": "https://github.com/vapor/console-kit.git",
"state": {
"branch": null,
"revision": "7a97a5ea7fefe61cf2c943242113125b0f396a98",
"version": "4.1.0"
}
},
{
"package": "HypertextLiteral",
"repositoryURL": "https://github.com/NSHipster/HypertextLiteral",
"state": {
"branch": null,
"revision": "3e45da849e507d171c7264146176bb834a01be4f",
"version": "0.0.2"
}
},
{
"package": "routing-kit",
"repositoryURL": "https://github.com/vapor/routing-kit.git",
"state": {
"branch": null,
"revision": "e7f2d5bd36dc65a9edb303541cb678515a7fece3",
"version": "4.1.0"
}
},
{
"package": "SKQueue",
"repositoryURL": "https://github.com/MaxDesiatov/SKQueue.git",
@ -28,6 +64,42 @@
"version": "0.0.5"
}
},
{
"package": "swift-backtrace",
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",
"state": {
"branch": null,
"revision": "f2fd8c4845a123419c348e0bc4b3839c414077d5",
"version": "1.2.0"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "d67ac68d09a95443303e9d6e37b34e7ba101d5f1",
"version": "1.0.1"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
}
},
{
"package": "swift-metrics",
"repositoryURL": "https://github.com/apple/swift-metrics.git",
"state": {
"branch": null,
"revision": "708b960b4605abb20bc55d65abf6bad607252200",
"version": "2.0.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
@ -63,6 +135,24 @@
"revision": "ae213938e151964aa691f0e902462fbe06baeeb6",
"version": "2.7.1"
}
},
{
"package": "vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "c70d45fe9cce525df5b5f4793a232596cd85484b",
"version": "4.5.0"
}
},
{
"package": "websocket-kit",
"repositoryURL": "https://github.com/vapor/websocket-kit.git",
"state": {
"branch": null,
"revision": "021edd1ca55451ad15b3e84da6b4064e4b877b34",
"version": "2.1.0"
}
}
]
},

View File

@ -5,11 +5,13 @@ import PackageDescription
let package = Package(
name: "carton",
platforms: [.macOS(.v10_15)],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.0.5")),
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.11.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.5.0"),
.package(url: "https://github.com/MaxDesiatov/SKQueue.git", .branch("master")),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.1.1"),
.package(url: "https://github.com/NSHipster/HypertextLiteral", from: "0.0.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@ -18,9 +20,10 @@ let package = Package(
name: "carton",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "NIOHTTP2Server", package: "swift-nio-http2"),
.product(name: "Vapor", package: "vapor"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "SKQueue", package: "SKQueue"),
"SKQueue",
"HypertextLiteral",
]
),
.testTarget(

View File

@ -4,4 +4,8 @@ struct Dev: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Watch the current directory, host the app, rebuild on change."
)
func run() throws {
try Server.run()
}
}

View File

@ -0,0 +1,12 @@
import HypertextLiteral
import Vapor
extension HTML: ResponseEncodable {
public func encodeResponse(for request: Request) -> EventLoopFuture<Response> {
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "text/html")
return request.eventLoop.makeSucceededFuture(.init(
status: .ok, headers: headers, body: .init(string: description)
))
}
}

View File

@ -0,0 +1,12 @@
import Vapor
struct Server {
static func run() throws {
var env = Environment.development
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer { app.shutdown() }
try configure(app)
try app.run()
}
}

View File

@ -0,0 +1,10 @@
import Vapor
// configures your application
public func configure(_ app: Application) throws {
let directory = app.directory.publicDirectory
app.middleware.use(FileMiddleware(publicDirectory: directory))
// register routes
try routes(app)
}

View File

@ -0,0 +1,19 @@
import HypertextLiteral
import Vapor
func routes(_ app: Application) throws {
app.get { _ -> HTML in
#"""
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="index.js"></script>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
"""#
}
}

16
TestApp/Package.resolved Normal file
View File

@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "JavaScriptKit",
"repositoryURL": "https://github.com/kateinoigakukun/JavaScriptKit",
"state": {
"branch": null,
"revision": "1edcf707dcb06f50e980c6cac542f226361dc124",
"version": "0.3.0"
}
}
]
},
"version": 1
}

19
TestApp/Package.swift Normal file
View File

@ -0,0 +1,19 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "CartonTestApp",
dependencies: [
.package(url: "https://github.com/kateinoigakukun/JavaScriptKit", from: "0.3.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "TestApp",
dependencies: ["JavaScriptKit"]
),
]
)

View File

@ -0,0 +1,3 @@
import JavaScriptKit
print("Hello world!")

37
entrypoint/index.js Normal file
View File

@ -0,0 +1,37 @@
import { SwiftRuntime } from "javascript-kit-swift";
import { WASI } from "@wasmer/wasi";
import { WasmFs } from "@wasmer/wasmfs";
const swift = new SwiftRuntime();
// Instantiate a new WASI Instance
const wasmFs = new WasmFs();
let wasi = new WASI({
args: [],
env: {},
bindings: {
...WASI.defaultBindings,
fs: wasmFs.fs,
},
});
const startWasiTask = async () => {
// Fetch our Wasm File
const response = await fetch("/main.wasm");
const responseArrayBuffer = await response.arrayBuffer();
// Instantiate the WebAssembly file
const wasm_bytes = new Uint8Array(responseArrayBuffer).buffer;
let { instance } = await WebAssembly.instantiate(wasm_bytes, {
wasi_snapshot_preview1: wasi.wasiImport,
javascript_kit: swift.importObjects(),
});
swift.setInsance(instance);
// Start the WebAssembly WASI instance!
wasi.start(instance);
// Output what's inside of /dev/stdout!
const stdout = await wasmFs.getStdOut();
console.log(stdout);
};
startWasiTask();

4128
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

32
package.json Normal file
View File

@ -0,0 +1,32 @@
{
"name": "carton",
"version": "0.0.1",
"description": "📦 Watcher, bundler, and test runner for your SwiftWasm apps ",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MaxDesiatov/carton.git"
},
"keywords": [
"swift",
"webassembly",
"swiftwasm",
"bundler"
],
"author": "Max Desiatov",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/MaxDesiatov/carton/issues"
},
"homepage": "https://github.com/MaxDesiatov/carton#readme",
"devDependencies": {
"@wasmer/wasi": "^0.10.2",
"@wasmer/wasmfs": "^0.10.2",
"javascript-kit-swift": "^0.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
}
}

11
webpack.config.js Normal file
View File

@ -0,0 +1,11 @@
const path = require("path");
const outputPath = path.resolve(__dirname, "Public");
module.exports = {
entry: "./entrypoint/index.js",
mode: "development",
output: {
filename: "index.js",
path: outputPath,
},
};