magic

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

Liphium Magic

This project is still experimental and in early stages. Feel free to test it out, but expect major changes, bugs and of course also new features.

Liphium Magic is a suite of tools to help build tests and provide a better developer experience when developing web services in Golang. We made it because we felt like it the barrier of making a contribution to our own projects was too high. When working on applications in a team, it's important that everyone can easily start the project and also use the same tools everyone else is using. Like with dependencies, when you need a new database for something, you shouldn't have to tell everyone in your team to complete extra steps just for their app to run again. When someone first joins your project, they can ideally set everything up with one command. That's the vision of Magic, our all-in-one developer experience toolkit. For testing your app, both automatically and manually, as well as running it on your own machine without complex setup.

The path to this goal is of course a long one, and we also know that, so for now Magic can only really help with PostgreSQL databases. It only supports this one simple database and not more. It's all we use in our apps and in the future, when also have the need for it, we will likely integrate more services into Magic or build a nice abstraction layer that makes it easy to integrate different services.

We hope you'll enjoy this project, feel free to open an issue in case anything annoys you or you see potential for a new feature. Be nice and maybe we'll add it. We're working on this in our freetime though, so don't expect us to answer all. Before creating a pull request for something, please consult us in the issues. We're looking forward to having a discussion with you.

System requirements

  • Desktop operating system (Windows, macOS or Linux)
  • Docker (must be installed and the Go toolchain must have permissions to access the socket)
  • Golang (you're not making a Go application without it)

Application limitations

Magic only supports specific services, and while we do plan on increasing the amount of supported services, for now we only support the services listed below. If your application needs anything else, you're currently not the target audience for Magic.

Supported databases
  • PostgreSQL

Other services may be supported in the future.

Features

  • Make your app runnable with one command on any machine that meets the System requirements
  • Develop scripts that interact with your application or the database
    • Allows sharing of tools you're using for testing
  • Test your application using integration tests (they can also call your scripts)
    • Test with a real database using a real connection

Usage

1. Add Magic to your project:

go get -u github.com/Liphium/magic/v2@latest

2. Wrap your main function with magic.Start (please take a look at the real project example for how to really to do this, this just serves as a showcase):

// ...

func main() {
	magic.Start(magic.Config{
		AppName: "magic-example",
		PlanDeployment: func(ctx *mconfig.Context) {
			// Create a PostgreSQL database for the posts service
			postsDB := ctx.NewPostgresDatabase("posts")

			// Set up environment variables for the application
			ctx.WithEnvironment(mconfig.Environment{
				// Database connection environment variables
				"DB_HOST":     postsDB.Host(ctx),
				"DB_PORT":     postsDB.Port(ctx),
				"DB_USER":     postsDB.Username(),
				"DB_PASSWORD": postsDB.Password(),
				"DB_DATABASE": postsDB.DatabaseName(ctx),
			})
		},
		StartFunction: Start,
	})
}

func Start() {
    // Start your application here (we have to take over your main function to be able to run code before)
}

// ...

3. You can now use go run . to run your app and a database will be created in a Docker container near you.

Become a great wizard! If you want to be a real great one though, I would take a look at the real project example to actually see how it's done.

Documentation

Because this project is new, there is no real documentation yet. However, you can look into the examples folder. It should contain about everything you need for now.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppStarted

func AppStarted()

This function has to be called when your app successfully started.

It's used to start the test once your app is up and running in testing. The test runner does have a timeout of 10 seconds though, so if you're app takes longer than that to startup, you can modify the timeout in your Magic config.

func GetTestRunner

func GetTestRunner() *mrunner.Runner

Get the current runner active while testing.

For this to work, please make sure you call PrepareTesting in your TestMain function.

func PrepareTesting

func PrepareTesting(t *testing.M, config Config)

Call this function in any TestMain function if you want some tests to rely on your actual app or database.

When calling this method, Magic will automatically start your app and any required containers, just like when you normally run your app.

Please make sure to call the magic.AppStarted() function for the test runner to work properly. You can read its comments if you should still have questions about what it does. You can adjust the timeout for exiting in the Magic config.

The handler will be called once everything is ready.

func Ptr

func Ptr[T any](obj T) *T

A little helper to convert anything to a pointer.

func Start

func Start(config Config)

Start your application with Magic. Make sure to provide all the arguments in the config that are required.

Types

type Config

type Config struct {
	// Required. This app name will be used in the container name of databases or other services we may start for you automatically. Make sure you have no other project using the same name.
	AppName string

	// Required. This function will be executed to plan all the containers you want to start. You may create databases and more using the context passed into this function.
	PlanDeployment func(ctx *mconfig.Context)

	// Required. This should start your app like normal. Expect all the database and other containers to be started at this point. Magic will make sure they're all ready by doing health checks and stuff.
	StartFunction func()

	// Add scripts that could be useful while developing this app.
	Scripts []scripting.Script

	// This is how long Magic waits for your app to finally start up before just killing any test runner. Default: 10 seconds.
	//
	// Hint: You can use magic.Ptr(duration) to convert your duration to a pointer, we use a pointer here to be able to detect it being not set.
	TestAppTimeout *time.Duration
}

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

func (*Factory) IsProfileLocked

func (f *Factory) IsProfileLocked(profile string) bool

Check if a profile is locked (a magic instance is running)

func (*Factory) LockFile

func (f *Factory) LockFile(profile string) string

Get the location of the lock file for a profile

func (*Factory) MagicDirectory

func (f *Factory) MagicDirectory() string

Get the current magic cache directory

func (*Factory) PlanFile

func (f *Factory) PlanFile(profile string) string

Get the location of the plan file for a profile

func (*Factory) TryLockProfile

func (f *Factory) TryLockProfile(profile string) error

Trys to lock the lock file for the profile. If no error is returned, the profile was locked.

func (*Factory) Unlock

func (f *Factory) Unlock() error

Unlock the lock of the current factory in case one is there

func (*Factory) WarnIfNotIgnored

func (f *Factory) WarnIfNotIgnored()

Print a warning in case .magic is not in the current .gitignore

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL