components

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package components provides methods for initializing the main components of the application with parameters from the config file

Example
package main

import (
	"context"
	"fmt"
	"time"

	"golang.org/x/sync/errgroup"

	"github.com/ncotds/nco-qoordinator/pkg/components"
	"github.com/ncotds/nco-qoordinator/pkg/config"
)

var conf = &config.Config{
	LogLevel: "ERROR",
	HTTPServer: config.HTTPServerConfig{
		Listen: ":8090",
	},
	OMNIbus: config.OMNIbus{
		Clusters: map[string]config.SeedList{"OMNI1": {"localhost:4100"}},
	},
}

func main() {
	logger, err := components.NewLoggerComponent(conf)
	if err != nil {
		fmt.Println("cannot setup logger", err.Error())
	}

	coord, err := components.InitService(conf.OMNIbus, logger.Logger())
	if err != nil {
		fmt.Println("cannot setup service", err.Error())
	}

	restapi, err := components.NewRESTServerComponent(conf.HTTPServer, coord, logger.Logger())
	if err != nil {
		fmt.Println("cannot setup restapi server", err.Error())
	}

	fmt.Printf("%T\n", restapi)

	gr, fail := errgroup.WithContext(context.Background())
	gr.Go(logger.Run)
	gr.Go(restapi.Run)

	ctx, cancel := context.WithCancel(context.Background())
	cancel() // emulate caller interrupt the server

	select {
	case <-fail.Done():
		fmt.Println("error")
	case <-ctx.Done():
		fmt.Println("interrupt")
	}

	fmt.Println("restapi:", restapi.Shutdown(time.Second))
	fmt.Println("logger:", logger.Shutdown(time.Second))
}
Output:

*components.RESTServerComponent
interrupt
restapi: <nil>
logger: <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoggerComponent

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

func NewLoggerComponent

func NewLoggerComponent(conf *config.Config) (*LoggerComponent, error)

NewLoggerComponent creates new logger component based on config.

If Config.LogFile is not empty - logs will be written into the file, otherwise - to stdout.

func (*LoggerComponent) Logger

func (l *LoggerComponent) Logger() *app.Logger

Logger returns wrapped app.Logger

func (*LoggerComponent) Run

func (l *LoggerComponent) Run() error

Run enables DEBUG switching on syscall.SIGUSR1. The second sending of SIGUSR1 will switch back to initial log level

NOTE: does not supported on windows... because of missing support for signals (syscall.SIGUSR1)

func (*LoggerComponent) Shutdown

func (l *LoggerComponent) Shutdown(timeout time.Duration) error

Shutdown stops listening of SIGUSR1 and closes the target file descriptor

NOTE: does not supported on windows... because of missing support for signals (syscall.SIGUSR1)

type QueryCoordinatorService

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

func InitService

func InitService(conf config.OMNIbus, log *app.Logger) (*QueryCoordinatorService, error)

type RESTServerComponent

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

func NewRESTServerComponent

func NewRESTServerComponent(
	conf config.HTTPServerConfig,
	service *QueryCoordinatorService,
	log *app.Logger,
) (*RESTServerComponent, error)

NewRESTServerComponent creates http server that handle service methods

func (*RESTServerComponent) Run

func (s *RESTServerComponent) Run() error

Run starts listening http host:port and blocks until it stopped

func (*RESTServerComponent) Shutdown

func (s *RESTServerComponent) Shutdown(timeout time.Duration) error

Shutdown gracefully stops http server

Jump to

Keyboard shortcuts

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