Skip to content

Create your own module

Prerequisites

  • golang coding skills
  • experience with CLI tools

Tiny Systems components are very simple. All you its just implement the interface bellow.

Component interface

go
type Component interface {
  GetInfo() ComponentInfo
  //Handle handles incoming requests
  Handle(ctx context.Context, output Handler, port string, message interface{}) error
  //Ports gets list of ports
  Ports() []NodePort
  //Instance creates new instance with default settings
  Instance() Component
}

Checkout out example module single component example module.

go
package echo

import (
	"context"
	"fmt"
	"github.com/tiny-systems/module/module"
	"github.com/tiny-systems/module/registry"
)

const (
	ComponentName        = "echo"
	InPort        string = "in"
	OutPort       string = "out"
)

type Context any

type InMessage struct {
	Context Context `json:"context" configurable:"true" required:"true" title:"Context" description:"Arbitrary message to be echoed"`
}

type Component struct {
}

func (t *Component) Instance() module.Component {
	return &Component{}
}

func (t *Component) GetInfo() module.ComponentInfo {
	return module.ComponentInfo{
		Name:        ComponentName,
		Description: "Echo",
		Info:        "Sends the same message as it receives",
		Tags:        []string{"Echo", "Demo"},
	}
}

func (t *Component) Handle(ctx context.Context, handler module.Handler, port string, msg interface{}) error {
	if in, ok := msg.(InMessage); ok {
		return handler(ctx, OutPort, in.Context)
	}
	return fmt.Errorf("invalid message")
}

func (t *Component) Ports() []module.Port {
	return []module.Port{
		{
			Name:          InPort,
			Label:         "In",
			Source:        true,
			Configuration: InMessage{},
			Position:      module.Left,
		},
		{
			Name:          OutPort,
			Label:         "Out",
			Source:        false,
			Configuration: new(Context),
			Position:      module.Right,
		},
	}
}

var _ module.Component = (*Component)(nil)

func init() {
	registry.Register(&Component{})
}

Create a module

To create your module you need to use CLI commands from the official golang module github.com/tiny-systems/module

go
package main

import (
	"context"
	"fmt"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	_ "github.com/tiny-systems/main/components/myfancycomponent" // register your component here
	"github.com/tiny-systems/module/cli"
	"os"
	"os/signal"
)

// RootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "server",
	Short: "acme company first module",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

func main() {
	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

  // we register CLI commands from the module library
	cli.RegisterCommands(rootCmd)
	if err := rootCmd.ExecuteContext(ctx); err != nil {
		fmt.Printf("command execute error: %v\n", err)
	}
}


See the list of available commands now.

shell
go run main.go
acme company first module

Usage:
  server [flags]
  server [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  run         Run module
  tools       Developer tools

Flags:
  -h, --help                      help for server
      --platform-api-url string   Platform API URL (default "https://api.tinysystems.io")

Publish module to Tiny Systems directory

  • Make sure you have Docker or it's alternatives installed and running.
  • Create your developer key in your workspace settings page.
shell
go run cmd/main.go tools build --version v0.0.1 --name github.com/acme/first --devkey secret-dev-key-you-can-find-in-your-workspace-settings

That's it!

Your newly created module should appear in the list of modules of your workspace. Now you can deploy it to your cluster.