ποΈ Ticketing Service Example
This example shows how to build a simple ticketing service using Nylon, supporting create, view, delete, update, and book operations β with plugins written in Go and Rust.
π¦ Requirements
Section titled βπ¦ Requirementsβ- Nylon >= 1.0.0-beta.0
- Go
- Rust
π Getting Started
Section titled βπ Getting Startedβ1. Create a new project directory
Section titled β1. Create a new project directoryβnylon plugin init ticketing-servicecd ticketing-service
2. Add a new plugin (Go & Rust)
Section titled β2. Add a new plugin (Go & Rust)βnylon plugin add ticketing-svc --lang go --type ffinylon plugin add mid-authz --lang rust --type ffi
π Project Structure
Section titled βπ Project Structureβticketing-service/βββ c/β βββ nylon.h # C header for FFI pluginsβββ build/β βββ ticketing-svc.so # Compiled Go pluginβ βββ mid-authz.so # Compiled Rust pluginβββ config/β βββ conf.d/β β βββ __plugins.yaml # Auto-generated by Nylon CLIβ β βββ routes.yaml # Route configurationβ βββ config.yaml # Global Nylon configβββ ffi/β βββ ticketing-svc/β β βββ main.go # Main Go plugin codeβ β βββ go.modβ β βββ go.sumβ βββ mid-authz/β βββ Cargo.tomlβ βββ src/β βββ lib.rs # Main Rust plugin codeβββ plugins.yaml # Declare plugin metadataβββ .gitignoreβββ README.md
3. Start the Dev Server
Section titled β3. Start the Dev Serverβnylon plugin dev# Watches for changes and automatically rebuilds plugins
4. Write a plugin
Section titled β4. Write a pluginβ//go:build cgopackage main
/*#include "../../c/nylon.h"*/import "C"import ( "unsafe"
"github.com/AssetsArt/nylon/sdk/go/sdk")
func main() {}
func SendResponse(sdk_dispatcher *sdk.Dispatcher) C.FfiOutput { output := sdk_dispatcher.ToBytes() return C.FfiOutput{ ptr: (*C.uchar)(C.CBytes(output)), len: C.ulong(len(output)), }}
func InputToDispatcher(ptr *C.uchar, input_len C.int) *sdk.Dispatcher { input := C.GoBytes(unsafe.Pointer(ptr), C.int(input_len)) dispatcher := sdk.WrapDispatcher(input) return dispatcher}
/*This function is used to free the memory allocated by the plugin.It is called by the Nylon runtime.*///export plugin_freefunc plugin_free(ptr *C.uchar) { C.free(unsafe.Pointer(ptr))}
//export create_ticketfunc create_ticket(ptr *C.uchar, input_len C.int) C.FfiOutput { dispatcher := InputToDispatcher(ptr, input_len) http_ctx := dispatcher.SwitchDataToHttpContext()
// TODO: Implement the create ticket logic
// set http end and data dispatcher.SetHttpEnd(true) // set http end to true dispatcher.SetData(http_ctx.ToBytes()) // set data to http context return SendResponse(dispatcher)}
π Notes & Tips
Section titled βπ Notes & Tipsβ- Hot-reload: Save your plugin code and
nylon plugin dev
will auto-rebuild and reload plugins. - Route mapping: Map HTTP routes to plugin entrypoints via
routes.yaml
. - Multi-language support: You can combine Go, Rust, Zig plugins in a single project.
- Explore more: See Nylon plugin docs for all SDK options.
Happy Coding!