Git Product home page Git Product logo

go-garage's Introduction

PkgGoDev Go codecov codebeat badge Go Report Card License

go-garage

What is a garage? It is place where we store many useful tools which may be needed in the future. And this framework like a garage. It contains many useful tools/things for fast creating microservice with http-servers, swagger, different databases, message brokers, metrics, logger.

Purposes

For fast creating simple microservices, without designing architecture, with simple config.

Features

go-garage supports:

  • databases:
  • message broker:
    • rabbitmq with autoreconect on fail connection
  • caches:
    • redis
  • opcua:
  • metrics (each may be extended in user service):
    • alive handler
    • ready handler (includes communicate with databases and cache)
    • prometheus metrics (includes communicate with databases and cache, current cache size, database metrics from sql.DBStats)
  • config parser based on vrischmann/envconfig
  • http servers (with swagger):
    • router (labstack/echo)(github.com/labstack/echo)
  • logger based (rs/zerolog)[github.com/rs/zerolog]
  • dictributed mutex:
    • based on postgresql
    • based on redis
  • miniorm for popular actions on the database (simple CRUD)
  • service meta informations
  • utils for strings (also email, phones) and time
  • null types (nullmeta, nullstring, nulltime)

How works with providers

In common case enough few simple steps:

  • create config
  • fill config from envs
  • pass config to provider constructor
  • start provider
  • shutdown provider Also you can get a metrics and handlers for checking that provider alive and ready.
    Consider an example based on a PostgreSQL provider

Create and fill config

go-garage uses the github.com/vrischmann/envconfig for filling the config envs. But go-garage allows to set a default values for config.

type Config struct {
	DB          *pq.Config
}

func NewConfig() (*Config, error) {
	c := &Config{
		DB: &pq.Config{
			DSN: "postgres://postgres:secret@postgres:5432/test",
			Migrate: &migrations.Config{
				Directory: "/internal/db/migrations/pg",
				Action:    "up",
			},
		},
	}

	if err := envconfig.Init(c); err != nil {
		return nil, errors.Wrap(err, "init config")
	}

	return c, nil
}

Pass config to provider constructor

// Create connection to PostgreSQL
pqEnity, err := pq.NewEnity(ctx, "garage_pq", config.DB)
if err != nil {
	return errors.Wrap(err, "pq new enity")
}

Start provider

if err := pqEnity.Start(ctx, errGroup); err != nil {
	return errors.Wrap(err, "start")
}

Shutdown provider

if err := pqEnity.Shutdown(ctx); err != nil {
	return errors.Wrap(err, "shutdown")
}

Metrics and alive/ready handlers

pqEnity.GetMetrics() // get metrics
pqEnity.GetAliveHandlers() // alive handlers
pqEnity.GetReadyHandlers() // ready handlers

Example

go-garage-example

go-garage's People

Contributors

soldatov-s avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.