app

package
v1.15.3 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2025 License: MIT Imports: 7 Imported by: 0

README

app

Start and stop services gracefully, using errgroup to ensure that multiple services are started properly at the same time.


Example of use

import "github.com/go-dev-frame/sponge/pkg/app"

func main() {
    initApp()
    services := CreateServices()
    closes := Close(services)

    a := app.New(services, closes)
    a.Run()
}

func initApp() {
    // get configuration

    // initializing log

    // initializing database

    // ......
}

func CreateServices() []app.IServer {
    var servers []app.IServer

    // create an HTTP service
    httpAddr := ":8080" // or get from configuration
    httpServer := server.NewHTTPServer(
        httpAddr,
        server.WithHTTPIsProd(true), // run in release mode
    )
    servers = append(servers, httpServer)

    // create a gRPC service (optional)
    // grpcServer := server.NewGRPCServer(
    //
    // )
    // servers = append(servers, grpcServer)

    return servers
}

func Close(servers []app.IServer) []app.Close {
    var closes []app.Close

    // close servers
    for _, s := range servers {
        closes = append(closes, s.Stop)
    }

    // close other resources (database, logger, tracing, etc.)
    closes = append(closes, func() error {
        // TODO: call db.Close()
        return nil
    })

    return closes
}

Documentation

Overview

Package app is starting and stopping services gracefully, using golang.org/x/sync/errgroup to ensure that multiple services are started properly at the same time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App servers

func New

func New(servers []IServer, closes []Close) *App

New create an app

func (*App) Run

func (a *App) Run()

Run servers

type Close

type Close func() error

Close app close

type IServer

type IServer interface {
	Start() error
	Stop() error
	String() string
}

IServer server interface

Jump to

Keyboard shortcuts

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