From 1a0600d03e8e7b60ba9bd77ac66dc67d0ee0ea75 Mon Sep 17 00:00:00 2001
From: Brendan Hansen
- Learn the core ideas behind programming in Onyx.
+ Learn the core ideas behind programming in Onyx, and how to use developer tooling.
Learn
Core packages + External packages
diff --git a/www/templates/pages/docs/env_setup.html b/www/templates/pages/docs/env_setup.html index b8221f5..09ecac0 100644 --- a/www/templates/pages/docs/env_setup.html +++ b/www/templates/pages/docs/env_setup.html @@ -6,6 +6,7 @@The following sections will help set up an environment for you to program Onyx effectively.
diff --git a/www/templates/pages/docs/getting_started.html b/www/templates/pages/docs/getting_started.html index 7d318e7..054c2b3 100644 --- a/www/templates/pages/docs/getting_started.html +++ b/www/templates/pages/docs/getting_started.html @@ -2,10 +2,13 @@ Getting Started {{ endblock }} +{{ let navbar_page = "docs" }} + {{ block "page_content" }}+ This guide walks you through all steps of setting up an HTTP server Onyx, + writing some routes on it, and then deploying it to Wasmer Edge or Spin + using WCGI. +
++ First, you need create a new folder for this project, and initialize that folder as an Onyx project. + See the package manager explainer for more details. +
+ +mkdir my-http-server
+cd my-http-server
+onyx package init
+
+ + Then you need to add the HTTP server package to this project, and synchronize + changes to install the package. +
+# Add the package
+onyx package add http-server
+# Synchronize changes
+onyx package sync
+
+ + Once the package is successfully fetched, you can start writing some code! +
+
+ You can now write the code for your server.
+ Begin by creating a main.onyx
file that will house your code.
+ In this file, you first need to load your packages and use http
package.
+
#load "./lib/packages"
+
+// Include everything from the core library.
+use core {*}
+
+// Include the http package, and specifically use some common symbols.
+use http
+use http.server { Request, Response, route }
+
+
+ Now you write a simple main
function that will create a TCP HTTP server,
+ with one route for the getting "/"
endpoint.
+
main :: () {
+ // Create a router that will route the traffic.
+ router := http.server.router();
+
+ // Register a handler for 'GET /'.
+ router->get("/", (req: &Request, res: &Response) {
+ // Respond with HTML
+ res->html("HTTP Server in Onyx!
");
+
+ // Set status to 200
+ res->status(200);
+
+ // Mark the response as completed
+ res->end();
+ });
+
+
+ // Create a TCP server out of your router.
+ app := http.server.tcp(&router);
+
+ // Serve on port 8000.
+ app->serve(8000);
+}
+
+
+ That is everything you need to create a simple HTTP server that will respond to a GET
+ request on /
with "<h1>HTTP Server in Onyx!<h1>"
.
+
+ You could define more routes in this way, using router->get
or router->post
,
+ but there is a better way described shortly.
+
+ To run your server, you can either use the builtin Onyx runtime, or Wasmer with WASIX support. + These are the only two options for now, as there no other runtimes with fully networking support. +
+ ++ In a later section, you can make this web server use WCGI, so it can be deployed anywhere. +
+ ++ To run using the Onyx runtime (if it is installed), simply run the following command. +
+$ onyx run main.onyx
+[Info][Http-Server] Serving on port 8000
+
+
+ To run using Wasmer and WASIX, you need to compile your server to a WASM binary, then
+ run it with wasmer
.
+
$ # Adding '-r wasi' to target WASI, and '-DWASIX' to add WASIX extensions.
+$ onyx build main.onyx -r wasi -DWASIX
+$ wasmer run --net out.wasm
+[Info][Http-Server] Serving on port 8000
+
+ + To test your server, you can visit localhost:8000 in your browser. + Or, make a request using cURL. +
+$ curl http://localhost:8000
+<h1>HTTP Server in Onyx!<h1>
+
+
+
+ The server you have is perfectly functional, and you could keep adding more routes to it
+ in the same way as above. However, with all of this code in main
, it doesn't
+ scale terribly well.
+
+ An alternative way to register routes is to use Onyx's procedure tags feature + to label certain procedures as request handlers. Then, the router can automatically find + them and register them! +
+ +
+ Here is how this works. First, you move the request handler from before into a new
+ global procedure, and add a #tag
to it.
+
// Tag the procedure with a `route` structure, so it can be found by the router.
+#tag route.{ .GET, "/" }
+index :: (req: &Request, res: &Response) {
+ // Respond with HTML
+ res->html("HTTP Server in Onyx!
");
+
+ // Set status to 200
+ res->status(200);
+
+ // Mark the response as completed
+ res->end();
+}
+
+
+ Now in main
, you can remove the router->get()
call and replace it with router->collect_routes()
.
+ This procedure will find all route
tags in all packages in the code base.
+
main :: () {
+ // Create a router that will route the traffic.
+ router := http.server.router();
+
+ // Collect routes from all packages.
+ router->collect_routes();
+
+ // Create a TCP server out of our router.
+ app := http.server.tcp(&router);
+
+ // Serve on port 8000.
+ app->serve(8000);
+}
+
+ + You can test the server in the same way as before. +
+$ onyx run main.onyx
+[Info][Http-Server] Serving on port 8000
+ $ curl http://localhost:8000
+<h1>HTTP Server in Onyx!<h1>
++ To deploy to Wasmer Edge, you need to first convert the code to use + the Common Gateway Interface (CGI) protocol instead of being a TCP server. +
+ +
+ Simply, replace the http.server.tcp
call with http.server.cgi
.
+
main :: () {
+ // Create a router that will route the traffic.
+ router := http.server.router();
+
+ // Collect routes from all packages.
+ router->collect_routes();
+
+ // Respond using the CGI protocol
+ http.server.cgi(&router);
+}
+
+ + Then compile the code to a WebAssembly module targeting the WASI runtime. +
+$ onyx build main.onyx -r wasi -o my-http-server.wasm
+
+
+ To deploy to Wasmer Edge, create a wasmer.toml
file that contains the following.
+ Replace <your-namespace>
and <your-package-name>
with your details.
+
[package]
+name = "<your-namespace>/<your-package-name>"
+version = "0.1.0"
+description = "My first HTTP server"
+license = "MIT"
+
+[[module]]
+name = "server"
+source = "my-http-server.wasm"
+abi = "wasi"
+
+[[command]]
+name = "server"
+module = "server"
+runner = "https://webc.org/runner/wcgi"
+
+[command.annotations.wasi]
+env = ["SCRIPT_NAME=rust_wcgi"]
+
+[command.annotations.wcgi]
+dialect = "rfc-3875"
+
+
+ Also, create an app.yaml
file with the following contents.
+
---
+kind: wasmer.io/App.v0
+name: <your-app-name>
+package: <your-namespace>/<your-package-name>
+
+ + Then use Wasmer CLI to deploy it. +
+ +$ wasmer deploy
+
+ + Follow the instructions from the Wasmer to test your deployment. +
++ If you want to keep building up your HTTP server, you can look at more examples in the HTTP Server package. +
++ Here is a small list of guides for getting started with common types of Onyx projects. +
++ Get started making an HTTP Server, entirely in Onyx. + This guide also descibes how to deploy your server to Wasmer Edge. +
++ Get started making games and graphical applications using the popular open source Raylib library. +
+Online Linux diff --git a/www/templates/pages/docs/package.html b/www/templates/pages/docs/package.html new file mode 100644 index 0000000..bd37c4d --- /dev/null +++ b/www/templates/pages/docs/package.html @@ -0,0 +1,194 @@ +{{block "title"}}Onyx Package Manager{{endblock}} + +{{ let navbar_page = "docs" }} + +{{ block "page_content" }} + +
+ The Onyx package manager is intentionally very simple. + Onyx does not have a central package repository like NPM, PyPI, or Cargo. + Instead Onyx uses a decentralized approach, that follows this simple rule. +
+ +
+ Every package is a Git repository, with a file called module.onyx
that is responsible for
+ including all other files in the package.
+
+
+ + The job of the package manager is to simply clone each of these Git repositories into a known structure, + and build a file for you to include in your project that loads all of your packages. +
+ ++ The package manager uses Git's tags feature to store version numbers. + This way the package manager can clone exactly one tag, at one commit, to avoid downloading all history of the package, + when all you want is a single version. +
+ +
+ When you publish a package, using onyx package publish
it simply increments the version number in the package file,
+ commits that change, creates a new tag at that commit, and pushes all changes and tags to the Git server.
+ When someone else runs onyx package update
, the new version will be found and updated to.
+
+ To use Onyx's package manager, you must be in an Onyx project.
+ An Onyx project is simply a directory with a file named onyx-pkg.kdl
.
+
+ To create a new project in a directory, you can use the following command, and answer the prompts (or leave any them blank). +
+$ onyx package init
+Creating new project manifest in ./onyx-pkg.kdl.
+
+Package name: example-project
+Package description: A simple example project
+Package url: https://github.com/...
+Package author: Brendan Hansen
+Package version (0.0.1):
+
+ Once your project is created, you can add packages by running onyx package add ...
+
$ onyx package add http-server
+$ # Or
+$ onyx package add https://github.com/onyx-lang/pkg-http-server
+
+ + If you want to specify a particular version of a package, you can add the version number as another argument to the command. +
+$ # Specify version 0.1.2
+$ onyx package add http-server 0.1.2
+
+
+ For the sake of convience, the package manager maintains a list of string templates to try to resolve where your package comes from.
+ Currently by default, there is only one, https://github.com/onyx-lang/pkg-X
.
+ This means that when you try to add http-server
, it will actually end up using https://github.com/onyx-lang/pkg-http-server
.
+ If you want, you can change this list of string templates at the top of ONYX_PATH/tools/onyx-pkg.onyx
.
+
+ You will notice that no files were downloaded and nothing has change in your directory.
+ This is because adding a package simply adds it to the list of dependencies in your onyx-pkg.kdl
+ It does not synchronize all of your packages.
+
+ To do that, simply run onyx package sync
.
+
$ onyx package sync
+ Fetch http://github.com/onyx-lang/pkg-http-server 0.2.24
+
+ + This downloads all new packages, upgrades exist packages, builds native libraries, and creates a new + package file to include. +
+ +
+ Once all packages are synchroized, you can use them in your project but simplying including the package.onyx
+ file automatically created. By default this file lives at lib/packages.onyx
in your project.
+
+ Here is a simple example of using the http-server
package in a project.
+
// main.onyx
+
+#load "./lib/packages" // <--- Have to include the packages file!
+
+use core
+use http
+
+main :: () {
+ router := http.server.router();
+ router->get("/", (req, res) => {
+ res->html("Simple server is working!
");
+ res->status(200);
+ res->end();
+ });
+
+ app := http.server.tcp(&router);
+ app->serve(8000);
+}
+
+
+ Once all of this is setup, your workflow will likely just look like onyx pacakge update
, then onyx package sync
,
+ with a bit of onyx package add ...
sprinkled in there.
+
+ Creating a new package for Onyx is incredibly simple. + There just three things a project needs to become a package. +
onyx-pkg.kdl
filemodule.onyx
file+ Hosting a package on GitHub, GitLab, or any Git server is very simple if you are familiar with Git. + Just make sure everyone that needs to access the package has pull access to the repository. +
+ +
+ Every Onyx project already has an onyx-pkg.kdl
file.
+ Ensure the package section has correct information about your package, and you should be good to go there.
+ See the guide on creating a native library for how to set that up in your project.
+
+ Finally, you need a module.onyx
file.
+ This file simply needs to include all other source files for the package.
+ In a particularly small package, all of the code might go right inside of module.onyx
.
+
+ A typical module.onyx
looks like this.
+
// module.onyx
+
+package package_name_here
+
+#load_all "./src"
+
+
+ This file simply includes everything inside of the src
folder.
+ If needed you can conditionally load source files, depending on your package's needs.
+
+ When your package is ready for a release, you can simply run onyx package publish
.
+ This bumps the version in onyx-pkg.kdl
, creates a new commit, tags that commit with the new version,
+ then pushes all of the changes to the Git server, assuming you have push access.
+
$ onyx package publish
+ Publishing Creating new published version
+ Published Successfully published new version.
+
+ These are official packages hosted on the onyx-lang GitHub account. +
+ +Package | +Description | +
---|---|
{% p.name %} | +{% p.description %} | +
Package | +Description | +
---|---|
{% p.name %} | +{% p.description %} | +
There are currently no third-party packages. | +|
Create your own package and email me@brendanfh.com to have it added. | +