mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 04:50:36 +00:00
Add more examples, global types for net APIs
This commit is contained in:
parent
a8b57653f3
commit
28d6817c49
8 changed files with 157 additions and 32 deletions
4
.lune/data/test.csv
Normal file
4
.lune/data/test.csv
Normal file
|
@ -0,0 +1,4 @@
|
|||
Header1,Header2,Header3
|
||||
Hello,World,!
|
||||
1,2,3
|
||||
Foo,Bar,Baz
|
|
59
.lune/examples/csv_printer.luau
Normal file
59
.lune/examples/csv_printer.luau
Normal file
|
@ -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))
|
41
.lune/examples/server.luau
Normal file
41
.lune/examples/server.luau
Normal file
|
@ -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)
|
|
@ -8,7 +8,7 @@ print("Hello, lune! 🌙")
|
|||
Using a function from another module
|
||||
]==]
|
||||
|
||||
local module = require("./module")
|
||||
local module = require("./modules/module")
|
||||
module.sayHello()
|
||||
|
||||
--[==[
|
||||
|
|
|
@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- Added global types for networking & child process APIs
|
||||
- `net.request` gets `NetFetchParams` and `NetFetchResponse` for its argument and return value
|
||||
- `net.serve` gets `NetRequest` and `NetResponse` for the handler function argument and return value
|
||||
- `process.spawn` gets `ProcessSpawnOptions` for its third and optional parameter
|
||||
|
||||
## `0.2.1` - February 3rd, 2023
|
||||
|
||||
### Added
|
||||
|
|
|
@ -29,8 +29,8 @@ You can also download pre-built binaries for most systems directly from the GitH
|
|||
|
||||
## ✏️ Writing Lune Scripts
|
||||
|
||||
Check out the examples on how to write a script in the [.lune](.lune) folder ! <br />
|
||||
A great starting point and walkthrough of Lune can be found in the [Hello, Lune](.lune/hello_lune.luau) example.
|
||||
A great starting point and walkthrough of Lune can be found in [Hello, Lune](.lune/hello_lune.luau). <br />
|
||||
More examples of how to write Lune scripts can be found in the [examples](.lune/examples/) folder.
|
||||
|
||||
<details>
|
||||
<summary><b>🔎 List of APIs</b></summary>
|
||||
|
|
|
@ -210,6 +210,35 @@ declare fs: {
|
|||
isDir: (path: string) -> boolean,
|
||||
}
|
||||
|
||||
type NetMethod = "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH"
|
||||
|
||||
export type NetFetchParams = {
|
||||
url: string,
|
||||
method: NetMethod?,
|
||||
headers: { [string]: string }?,
|
||||
body: string?,
|
||||
}
|
||||
export type NetFetchResponse = {
|
||||
ok: boolean,
|
||||
statusCode: number,
|
||||
statusMessage: string,
|
||||
headers: { [string]: string },
|
||||
body: string,
|
||||
}
|
||||
|
||||
export type NetRequest = {
|
||||
path: string,
|
||||
query: string,
|
||||
method: NetMethod,
|
||||
headers: { [string]: string },
|
||||
body: string,
|
||||
}
|
||||
export type NetResponse = {
|
||||
status: number?,
|
||||
headers: { [string]: string }?,
|
||||
body: string?,
|
||||
}
|
||||
|
||||
--[=[
|
||||
@class net
|
||||
|
||||
|
@ -226,18 +255,7 @@ declare net: {
|
|||
@param config The URL or request config to use
|
||||
@return A dictionary representing the response for the request
|
||||
]=]
|
||||
request: (config: string | {
|
||||
url: string,
|
||||
method: ("GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH")?,
|
||||
headers: { [string]: string }?,
|
||||
body: string?,
|
||||
}) -> {
|
||||
ok: boolean,
|
||||
statusCode: number,
|
||||
statusMessage: string,
|
||||
headers: { [string]: string },
|
||||
body: string,
|
||||
},
|
||||
request: (config: string | NetFetchParams) -> NetFetchResponse,
|
||||
--[=[
|
||||
@within net
|
||||
|
||||
|
@ -250,17 +268,7 @@ declare net: {
|
|||
@param port The port to use for the server
|
||||
@param handler The handler function to use for the server
|
||||
]=]
|
||||
serve: (port: number, handler: (request: {
|
||||
path: string,
|
||||
query: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH",
|
||||
headers: { [string]: string }?,
|
||||
body: string?,
|
||||
}) -> (string | {
|
||||
status: number?,
|
||||
headers: { [string]: string }?,
|
||||
body: string?,
|
||||
})) -> (),
|
||||
serve: (port: number, handler: (request: NetRequest) -> (string | NetResponse)) -> (),
|
||||
--[=[
|
||||
@within net
|
||||
|
||||
|
@ -282,6 +290,15 @@ declare net: {
|
|||
jsonDecode: (encoded: string) -> any,
|
||||
}
|
||||
|
||||
type ProcessSpawnOptionsStdio = "inherit" | "default"
|
||||
|
||||
export type ProcessSpawnOptions = {
|
||||
cwd: string?,
|
||||
env: { [string]: string }?,
|
||||
shell: (boolean | string)?,
|
||||
stdio: ProcessSpawnOptionsStdio?,
|
||||
}
|
||||
|
||||
--[=[
|
||||
@class process
|
||||
|
||||
|
@ -342,12 +359,7 @@ declare process: {
|
|||
spawn: (
|
||||
program: string,
|
||||
params: { string }?,
|
||||
options: {
|
||||
cwd: string?,
|
||||
env: { [string]: string }?,
|
||||
shell: (boolean | string)?,
|
||||
stdio: ("inherit" | "default")?,
|
||||
}?
|
||||
options: ProcessSpawnOptions?
|
||||
) -> {
|
||||
ok: boolean,
|
||||
code: number,
|
||||
|
|
Loading…
Reference in a new issue