cli/README.md

4.7 KiB

Core.Framework

Core is an opinionated framework for building Go applications. It was written to ensure information exchange between the front-end and back-end of a Web3 environment.

There is no assumption of what front-end framework you use or what back-end framework you use.

However, Core provides a set of services beneficial for building applications for the Web3 environment.

With working defaults, Core is designed to be as simple as possible, yet powerful enough to be used in various applications.

Libraries used in Core

Features

Core

Core allows you to easily instantiate complex services within a locked-down environment enforcing logical enclaves.

Basic Usage

package main

import (
    "cmd/demo"
	
    "https://github.com/Snider/Core"
)

func main() {
	demoService := core.New(
		core.WithService(demo.Register),
		core.WithServiceLock(), // This can go anywhere in the chain to be the Final Service (provides security clarity)
	)
	demoService.Run() // handled by wails, not implemented yet
	demoService.Wait() // handled by wails, not implemented yet
	demoService.Close() // handled by wails, not implemented yet
}

Multiple Services

package main

import (
	"cmd/demo"

	"https://github.com/Snider/Core"
)

func main() {
	coreService := core.New(
		core.WithService(demo.Register),
		core.WithService(demo.RegisterDemo2),
		core.WithServiceLock(),
	)

	rickService := core.New(
		core.WithService(demo.Register),
		core.WithService(demo.RegisterDemo2),
		core.WithServiceLock(),
	)
	mortyService := core.New(
		core.WithService(demo.Register),
		core.WithService(demo.RegisterDemo2),
		core.WithServiceLock(),
	)

	core.Mod[API](coreService,"demo").name = "demo"
	core.Mod[API](rickService).name = "demo2"
	core.Mod[API](mortyService).name = "demo2"

}

Integrating your own services

package demo

import "github.com/Snider/Core"

// this instance is the singleton instance of the demo module.
var instance *Demo

type Demo struct {
	name string
	core *core.Core
}

func Register(c *core.Core) error {
	instance = &Demo{
		core: c,
	}
	if err := c.RegisterModule("demo", instance); err != nil {
		return err
	}
	c.RegisterAction(handleActionCall)
	return nil
}

func handleActionCall(c *core.Core, msg core.Message) error {
	return nil
}


Core.Display

Display is a Wails.io service that provides the ability to open windows. Core.Display remembers the locations of your users windows and will reopen them when the application is restarted. Also allows you to adjust your windowing programmatically via JavaScript bindings, and for it to be persistent.

Open A Window On Startup

func (d *API) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
	d.OpenWindow(
		OptName("main"),
		OptHeight(900),
		OptWidth(1280),
		OptURL("/"),
		OptTitle("Core"),
	)
	return nil
}
window := d.OpenWindow(
	OptName("main"),
	OptHeight(900),
	OptWidth(1280),
	OptURL("/"),
	OptTitle("Core"),
)

Full Wails Example

package main

import (
	"embed"

	"github.com/Snider/Core"
	"github.com/Snider/Core/display"
	"github.com/wailsapp/wails/v3/pkg/application"
)

//go:embed all:public/*
var assets embed.FS

func main() {

	app := application.New(application.Options{
		Assets: application.AssetOptions{
			Handler: application.AssetFileServerFS(assets),
		},
	})

	app.RegisterService(application.NewService(core.Service(
		core.WithWails(app), // Provides the Wails application instance to core services
		core.WithAssets(assets), // Provides the embed.FS to core services
		core.WithService(display.Register), // Provides the ability to open windows
		core.WithService(config.Register), // Provides the ability to persist UI state (windows reopen where they closed)
		core.WithServiceLock(), // locks core from accepting new services blocking access to IPC
	)))

	err := app.Run()
	if err != nil {
		panic(err)
	}
}