forked from lune-org/lune
-
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.
Add more examples, global types for net APIs
- Loading branch information
1 parent
a8b5765
commit 28d6817
Showing
8 changed files
with
157 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Header1,Header2,Header3 | ||
Hello,World,! | ||
1,2,3 | ||
Foo,Bar,Baz |
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,59 @@ | ||
--> A utility script that prints out a CSV | ||
--> file in a prettified format to stdout | ||
|
||
local LINE_SEPARATOR = "\n" | ||
local COMMA_SEPARATOR = "," | ||
|
||
local path = process.args[1] or ".lune/data/test.csv" | ||
|
||
assert(path ~= nil and #path > 0, "No input file path was given") | ||
assert(not fs.isDir(path), "Input file path was a dir, not a file") | ||
assert(fs.isFile(path), "Input file path does not exist") | ||
|
||
-- Read all the lines of the wanted file | ||
local rawLines = string.split(fs.readFile(path), LINE_SEPARATOR) | ||
|
||
-- Split the raw lines into header and table of data | ||
local csvHeader = string.split(rawLines[1], COMMA_SEPARATOR) | ||
local csvTable = {} | ||
for index = 2, #rawLines, 1 do -- NOTE: We skip the first line here, that's the header | ||
csvTable[index - 1] = string.split(rawLines[index], COMMA_SEPARATOR) | ||
end | ||
|
||
-- Gather the maximum widths of strings for alignment | ||
local maxWidths = {} | ||
|
||
for index, header in csvHeader do | ||
maxWidths[index] = #header | ||
end | ||
|
||
for _, row in csvTable do | ||
for index, value in row do | ||
maxWidths[index] = math.max(maxWidths[index], #value) | ||
end | ||
end | ||
|
||
local totalWidth = 0 | ||
for _, width in maxWidths do | ||
totalWidth += width | ||
end | ||
|
||
-- Print it all out | ||
|
||
local function printRow(row: { string }) | ||
local paddedValues = {} | ||
for index, value in row do | ||
local spacing = string.rep(" ", maxWidths[index] - #value) | ||
table.insert(paddedValues, string.format(" %s%s ", value, spacing)) | ||
end | ||
print(string.format("┃ %s ┃", table.concat(paddedValues, "┃"))) | ||
end | ||
|
||
local thiccLine = string.rep("━", totalWidth + #csvHeader * 3 + 1) | ||
print(string.format("┏%s┓", thiccLine)) | ||
printRow(csvHeader) | ||
print(string.format("┣%s┫", thiccLine)) | ||
for _, row in csvTable do | ||
printRow(row) | ||
end | ||
print(string.format("┗%s┛", thiccLine)) |
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,41 @@ | ||
--> A basic webserver that echoes the given request | ||
--> body at /ping and otherwise responds 404 "Not Found" | ||
|
||
local PORT = if process.env.PORT ~= nil and #process.env.PORT > 0 | ||
then assert(tonumber(process.env.PORT), "Failed to parse port from env") | ||
else 8080 | ||
|
||
-- Create our responder functions | ||
|
||
local function pong(request: NetRequest): NetResponse | ||
return { | ||
body = `Pong!\n{request.path}\n{request.body}"`, | ||
} | ||
end | ||
|
||
local function notFound(request: NetRequest): NetResponse | ||
return { | ||
status = 404, | ||
body = "Not Found", | ||
} | ||
end | ||
|
||
-- Exit our example after a small delay, if you copy this | ||
-- example just remove this part to keep the server running | ||
|
||
task.delay(2, function() | ||
print("Shutting down...") | ||
task.wait(1) | ||
process.exit(0) | ||
end) | ||
|
||
-- Run the server on port 8080 | ||
|
||
print(`Listening on port {PORT} 🚀`) | ||
net.serve(PORT, function(request) | ||
if string.sub(request.path, 1, 6) == "/ping" then | ||
return pong(request) | ||
else | ||
return notFound(request) | ||
end | ||
end) |
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
File renamed without changes.
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