Skip to content

Commit

Permalink
Local serve public files (#15)
Browse files Browse the repository at this point in the history
* Serve public files

* Log

* Log

* Fix

* CLeanup
  • Loading branch information
AndrewBarba authored Jun 21, 2023
1 parent 3048743 commit bf811eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "f7c46552983b06b0958a1a4c8bc5199406ae4c8a",
"version" : "2.51.0"
"revision" : "6213ba7a06febe8fef60563a4a7d26a4085783cf",
"version" : "2.54.0"
}
}
],
Expand Down
15 changes: 15 additions & 0 deletions Plugins/VercelPackager/Server/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const http = require('http')
const path = require('path')
const fs = require('fs')
const port = Number(process.argv[2] || 7676)

async function invoke(payload) {
Expand Down Expand Up @@ -26,7 +28,20 @@ async function readBody(stream) {
})
}

function serveStaticFile(req, res) {
const localPath = path.join(process.env.SWIFT_PROJECT_DIRECTORY, 'public', req.url)
const data = fs.readFileSync(localPath)
res.writeHead(200, {})
res.end(data)
}

const server = http.createServer(async (req, res) => {
try {
serveStaticFile(req, res)
return
} catch (err) {
// ignore
}
try {
const method = req.method
const path = req.url
Expand Down
3 changes: 2 additions & 1 deletion Plugins/VercelPackager/VercelOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public struct VercelOutput {
arguments: [
projectDirectory.appending([".build", "checkouts", "Vercel", "Plugins", "VercelPackager", "Server", "server.js"]).string,
port
]
],
environment: ["SWIFT_PROJECT_DIRECTORY": projectDirectory.string]
)
}

Expand Down

0 comments on commit bf811eb

Please sign in to comment.