Skip to content

Commit a21f2dc

Browse files
authored
HTTP server docs (#529)
* http server docs * Minor fix * Minor fix * spellcheck * spellcheck * spellcheck * spellcheck * spellcheck * spellcheck * asdasdadsf
1 parent a88f736 commit a21f2dc

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/en/manuals/editor-scripts.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,68 @@ Language server definition table may specify:
245245
- `command` (required) - an array of command and its arguments
246246
- `watched_files` - an array of tables with `pattern` keys (a glob) that will trigger the server's [watched files changed](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didChangeWatchedFiles) notification.
247247

248+
## HTTP server
249+
250+
Every running instance of the editor has an HTTP server running. The server can be extended using editor scripts. To extend the editor HTTP server, you need to add `get_server_routes` editor script function — it should return the additional routes:
251+
```lua
252+
print("My route: " .. http.server.url .. "/my-extension")
253+
254+
function M.get_server_routes()
255+
return {
256+
http.server.route("/my-extension", "GET", function(request)
257+
return http.server.response(200, "Hello world!")
258+
end)
259+
}
260+
end
261+
```
262+
After reloading the editor scripts, you'll see the following output in the console: `My route: http://0.0.0.0:12345/my-extension`. If you open this link in the browser, you'll see your `"Hello world!"` message.
263+
264+
The input `request` argument is a simple Lua table with information about the request. It contains keys such as `path` (URL path segment that starts with `/`), request `method` (e.g. `"GET"`), `headers` (a table with lower-case header names), and optionally `query` (the query string) and `body` (if the route defines how to interpret the body). For example, if you want to make a route that accepts JSON body, you define it with a `"json"` converter parameter:
265+
```lua
266+
http.server.route("/my-extension/echo-request", "POST", "json", function(request)
267+
return http.server.json_response(request)
268+
end)
269+
```
270+
You can test this endpoint in the command line using `curl` and `jq`:
271+
```sh
272+
curl 'http://0.0.0.0:12345/my-extension/echo-request?q=1' -X POST --data '{"input": "json"}' | jq
273+
{
274+
"path": "/my-extension/echo-request",
275+
"method": "POST",
276+
"query": "q=1",
277+
"headers": {
278+
"host": "0.0.0.0:12345",
279+
"content-type": "application/x-www-form-urlencoded",
280+
"accept": "*/*",
281+
"user-agent": "curl/8.7.1",
282+
"content-length": "17"
283+
},
284+
"body": {
285+
"input": "json"
286+
}
287+
}
288+
```
289+
The route path supports patterns that can be extracted from the request path and provided to the handler function as a part of the request, e.g.:
290+
```lua
291+
http.server.route("/my-extension/setting/{category}.{key}", function(request)
292+
return http.server.response(200, tostring(editor.get("/game.project", request.category .. "." .. request.key)))
293+
end)
294+
```
295+
Now, if you open e.g. `http://0.0.0.0:12345/my-extension/setting/project.title`, you'll see the title of your game taken from the `/game.project` file.
296+
297+
In addition to a single segment paths pattern, you can also match the rest of the URL path using `{*name}` syntax. For example, here is a simple file server endpoint that serves files from the project root:
298+
```lua
299+
http.server.route("/my-extension/files/{*file}", function(request)
300+
local attrs = editor.external_file_attributes(request.file)
301+
if attrs.is_file then
302+
return http.server.external_file_response(request.file)
303+
else
304+
return 404
305+
end
306+
end)
307+
```
308+
Now, opening e.g. `http://0.0.0.0:12345/my-extension/files/main/main.collection` in the browser will display the contents of the `main/main.collection` file.
309+
248310
## Editor scripts in libraries
249311

250312
You can publish libraries for other people to use that contain commands, and they will be automatically picked up by the editor. Hooks, on the other hand, can't be picked up automatically, since they have to be defined in a file that is in a root folder of a project, but libraries expose only subfolders. This is intended to give more control over build process: you still can create lifecycle hooks as simple functions in `.lua` files, so users of your library can require and use them in their `/hooks.editor_script`.

0 commit comments

Comments
 (0)