fxgomysqlserver

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 11 Imported by: 0

README

Fx Go MySQL Server Module

ci go report codecov Deps PkgGoDev

Fx module for Go Mysql Server.

Overview

This module integrates an embedded Go MySQL server into your Yokai application.

This is made for development / testing purposes, not to replace a real MySQL server for production applications.

It can be configured to accept connections:

  • via TCP
  • via a socket
  • via memory

Make sure to acknowledge the underlying vendor limitations.

Installation

First install the module:

go get github.com/ankorstore/yokai-contrib/fxgomysqlserver

Then activate it in your application bootstrapper:

// internal/bootstrap.go
package internal

import (
	"github.com/ankorstore/yokai/fxcore"
	"github.com/ankorstore/yokai-contrib/fxgomysqlserver"
)

var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
	// load fxgomysqlserver module
	fxgomysqlserver.FxGoMySQLServerModule,
	// ...
)

Configuration

Configuration reference:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: true
modules:
  gomysqlserver:
    config:
      transport: tcp            # transport to use: tcp, socket or memory (tcp by default)
      socket: /tmp/mysql.sock   # socket path (/tmp/mysql.sock by default)
      user: user                # database user name (user by default)
      password: password        # database user password (password by default)
      host: localhost           # database host (localhost by default)
      port: 3306                # database port (3306 by default)
      database: db              # database name (db by default)
    log:
      enabled: true             # to enable server logs
    trace:
      enabled: true             # to enable server traces

Usage

TCP transport

You can configure the server to accept TCP connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: tcp
      user: user
      password: password
      host: localhost
      port: 3306
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@tcp(localhost:3306)/db")
Socket transport

You can configure the server to accept socket connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: socket
      socket: /tmp/mysql.sock 
      user: user
      password: password
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@unix(/tmp/mysql.sock)/db")
Memory transport

You can configure the server to accept memory connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: memory
      user: user
      password: password
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@memory(bufconn)/db")

Testing

The memory transport avoids to open TCP ports or sockets, making it particularly lean and useful for testing purposes.

In you test configuration:

# ./configs/config.test.yaml
modules:
  gomysqlserver:
    config:
      transport: memory

In you application bootstrapper:

// internal/bootstrap.go
package internal

import (
	"testing"
	
	"github.com/ankorstore/yokai-contrib/fxgomysqlserver"
)

// RunTest starts the application in test mode, with an optional list of [fx.Option].
func RunTest(tb testing.TB, options ...fx.Option) {
	tb.Helper()

	// ...

	Bootstrapper.RunTestApp(
		tb,
		// enable and start the server
		fxgomysqlserver.FxGoMySQLServerModule,
		// apply per test options
		fx.Options(options...),
	)
}

You can check the tests of this module to get testing examples.

Documentation

Index

Constants

View Source
const ModuleName = "gomysqlserver"

ModuleName is the module name.

Variables

FxGoMySQLServerModule is the Fx go-mysql-server module.

Functions

func NewFxGoMySQLServer

func NewFxGoMySQLServer(p FxGoMySQLServerParam) (*sqle.Server, error)

NewFxGoMySQLServer returns a new sqle.Server instance.

func NewFxGoMySQLServerConfig

func NewFxGoMySQLServerConfig(p FxGoMySQLServerConfigParam) (*config.GoMySQLServerConfig, error)

NewFxGoMySQLServerConfig returns a new config.GoMySQLServerConfig instance.

Types

type FxGoMySQLServerConfigParam

type FxGoMySQLServerConfigParam struct {
	fx.In
	Config *yokaiconfig.Config
}

FxGoMySQLServerConfigParam allows injection of the required dependencies in [NewGoMySQLServerConfig].

type FxGoMySQLServerModuleInfo

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

FxGoMySQLServerModuleInfo is a module info collector for gomysqlserver.

func NewFxGoMySQLServerModuleInfo

func NewFxGoMySQLServerModuleInfo(config *config.GoMySQLServerConfig, server *server.Server) *FxGoMySQLServerModuleInfo

NewFxGoMySQLServerModuleInfo returns a new FxGoMySQLServerModuleInfo.

func (*FxGoMySQLServerModuleInfo) Data

func (i *FxGoMySQLServerModuleInfo) Data() map[string]interface{}

Data return the data of the module info.

func (*FxGoMySQLServerModuleInfo) Name

Name return the name of the module info.

type FxGoMySQLServerParam

type FxGoMySQLServerParam struct {
	fx.In
	LifeCycle      fx.Lifecycle
	ServerFactory  server.GoMySQLServerFactory
	ServerConfig   *config.GoMySQLServerConfig
	Config         *yokaiconfig.Config
	Logger         *log.Logger
	TracerProvider oteltrace.TracerProvider
}

FxGoMySQLServerParam allows injection of the required dependencies in NewFxGoMySQLServer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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